Konrad
Konrad

Reputation: 63

Visual Basic Class with 2 dimensional arrays?

Just picking up programming again and new to Classes and I'm trying to make 2 dimensional arrays work in a class.

I need a function to be passed the size (x,y) of two 2dimensional arrays in a Class that the function will return.

Is this possible, if so how do I Dim the ReturnVar

This isn't working code of course, just a skeleton to show what I'm after.

Public Class TestClass
    Public Array1(,) As Integer
    Public Array2(,) As Integer
End Class

Function MyFunc1(ByVal x as Integer, y as Integer) as TestClass
    'x and y will define the size of the two arrays in the TestClass

    Dim ReturnVar ??? As New TestClass
    .
    do some code
    .
    Return ReturnVar
End Function

Upvotes: 0

Views: 647

Answers (1)

Matt Wilko
Matt Wilko

Reputation: 27322

Something like this should do it if I have understood correctly:

Function MyFunc1(ByVal x As Integer, y As Integer) As TestClass
    Dim ReturnVar As New TestClass
    ReDim ReturnVar.Array1(x, y)
    ReDim ReturnVar.Array2(x, y)
    Return ReturnVar
End Function

It would be a better idea to pass these values into the constrcutor of the TestClass I think, then it makes it obvious and you can't forget it:

Public Class TestClass
    Public Array1(,) As Integer
    Public Array2(,) As Integer

    Public Sub New(x1 As Integer, y1 As Integer, x2 As Integer, y2 As Integer)
        ReDim Array1(x1, y1)
        ReDim Array2(x2, y2)
    End Sub
End Class

Your function is now so simple it doesn't need to be a function:

Function MyFunc1(ByVal x As Integer, y As Integer) As TestClass
    Return New TestClass(x, y, x, y)
End Function

Upvotes: 1

Related Questions