Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Use Class to Get and Set an Array

Posted on 2006-10-20
2
Medium Priority
?
215 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

618 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