【问题标题】:Visual Basic Class with 2 dimensional arrays?具有二维数组的 Visual Basic 类?
【发布时间】:2016-10-16 09:06:57
【问题描述】:

刚刚重新开始编程,刚接触类,我正在尝试让二维数组在类中工作。

我需要一个函数来传递函数将返回的类中两个二维数组的大小 (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

【问题讨论】:

  • 您是在学习类、二维数组还是两者兼而有之?

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


【解决方案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

【讨论】:

  • 哇,这看起来很简单! :) 我想我可以将我的代码放在 Public Sub New 例程中并填充数组,而不是一个函数?从技术上讲,数组的大小不是 x 和 y,而是 x 和 y 用于计算数组大小。我今晚试试,非常感谢!!!
猜你喜欢
  • 1970-01-01
  • 2015-01-10
  • 1970-01-01
  • 2021-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-02
  • 2012-10-03
相关资源
最近更新 更多