Using a array of classes? (Or something like that)

Okay, I do this very easily in Java and Javascript, but cannot seem to get it working in Visual Basic.

Here is how I'm current doing it:

Public Class extList
Private name As String
Private dir As String
Private propertyValues As String()


Public Property extName() As String
Get
Return name
End Get
Set(ByVal value As String)
name = value
End Set
End Property
Public Property extDir() As String
Get
Return dir
End Get
Set(ByVal value As String)
dir = value
End Set
End Property
End Class

That's the class that I want, and I declare it like:

dim extensionList as new extList

That all is fine and dandy, but like I do in other languages, I want it to act like as array.

Normally in Javascript, I do it like this:

var listOfStuff = new Array();

and then a declare each new element of the array as a reference to the class, like:

listOfStuff [0] = new extList('value','value');

Then I can simply access each element like:

listOfStuff[0].extName = 'value';
listOfStuff[0].extDir = 'value';

How the heck can I accomplish this in VB

Thanks




Answer this question

Using a array of classes? (Or something like that)

  • DevboyX

    I've just had the same problem and found something along the following lines works - it's very similar to what you've tried but you can't declare an array of classes with the New keyword.

    Public Class Form1

    Dim extensionList() As extList

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim iX As Integer

    For iX = 1 To 3
    ReDim Preserve extensionList(iX)
    extensionList(iX) = New extList
    extensionList(iX).extName = "Name: " & iX.ToString
    Next

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim iX As Integer

    For iX = 1 To 3
    MsgBox(extensionList(iX).extName)
    Next

    End Sub

    End Class


  • Using a array of classes? (Or something like that)