Solved

Use Class to Get and Set an Array

Posted on 2006-10-20
2
206 Views
Last Modified: 2010-04-23
I have an array in some code that I want to move into a class so that I use it almost like normal.

Sort of like this:

Dim MyArray(5) As String
MyArray(0) = "Hello, world."
MessageBox.Show MyArray(0)

To something like this:

'create instance of class
MyClass.MyArray(0) = "Hello, world."
MessageBox.Show MyClass.MyArray(0)

The "get" should work fine, but I don't know about the "set". The only thing I can think of is a helper method that set it using the index and string, something like this:

MyClass.SetMyArray(0, "Hello, world.")

Is there a better way?
0
Comment
Question by:GivenRandy
2 Comments
 
LVL 3

Accepted Solution

by:
hairyminga earned 500 total points
ID: 17774357
Surely, if you make the array public in the class definition, then you can use it as you normally would in VB.NET.

Either that, or you could do something like the following:

Public Class ArrayClass
    Private myArray(5) As String

    Public Property theArray() As String()
        Get
            Return myArray
        End Get
        Set(ByVal value As String())
            myArray = value
        End Set
    End Property
End Class


That way you could refer to your array as follows:

    Sub Main()
        Dim x As New ArrayClass
        x.theArray(3) = "Hello, World"
        MsgBox(x.theArray(3))
    End Sub

Hope that helps :)
0
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17774971
It would be better to make it into an array list or a string collection

Public Class StringsClass
    Private myArray As new Collections.Specialized.StringCollection

    Public readonly Property Strings() As Collections.Specialized.StringCollection
        Get
            Return myArray
        End Get
    End Property
End Class

this way you could use it as follows

 Sub Main()
        Dim x As New stringsClass
        x.Strings.Add("string1")
        messagebox.Show(x.strings(0))

        'or if you have an array of strings you could do this
        Dim y as new stringsClass
        dim z as string() = {"0","1","2","3"}
        x.strings.addrange(z)
        messagebox.Show(x.strings(2))
End Sub


It's always easier to use collections than arrays.
but it is faster to use arrays than collections.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now