?
Solved

Use Class to Get and Set an Array

Posted on 2006-10-20
2
Medium Priority
?
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 3

Accepted Solution

by:
hairyminga earned 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

743 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