具有2维数组的Visual Basic类?

时间:2016-06-15 10:03:56

标签: .net arrays vb.net class multidimensional-array

再次选择编程和新的Classes,我试图让二维数组在一个类中工作。

我需要一个函数来传递函数将返回的两个2维数组的大小(x,y)。

这是否可行,如果是这样,我如何调暗ReturnVar

这当然不是代码,只是一个显示我所追求的骨架。

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

1 个答案:

答案 0 :(得分:1)

如果我理解正确的话,应该这样做:

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

将这些值传递给我认为的TestClass的构造函数会更好一点,然后它会让它变得明显,你不能忘记它:

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

您的功能现在非常简单,不需要是一个功能:

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