Solved

Use Class to Get and Set an Array

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 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

23 Experts available now in Live!

Get 1:1 Help Now