Solved

Use Class to Get and Set an Array

Posted on 2006-10-20
2
204 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

707 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

11 Experts available now in Live!

Get 1:1 Help Now