如何在Visual Basic中动态声明对象

时间:2017-08-10 08:13:54

标签: vb.net

如果今天是第3名,我想提出三个问题。

Dim Obj1 As Object

Dim Obj2 As Object

Dim Obj3 As Object

如果今天是第5名,我想提出三个问题。

Dim Obj1 As Object

Dim Obj2 As Object

Dim Obj3 As Object

Dim Obj4 As Object

Dim Obj5 As Object

然后如何像这样动态声明对象? 谢谢你的阅读。

3 个答案:

答案 0 :(得分:2)

你必须将它们保存在数组或列表或其他类型的集合中。以下是如何实现它:

Dim objects = New List(Of Object)

For i = 1 To Date.Today.Day
    objects.Add(New Object())
Next

答案 1 :(得分:0)

创建对象列表可能适用于您的目标:

Dim objs As List(Of Object) = New List(Of [Object])()
Dim day As Integer = DateTime.Today.Day

For i As Integer = 1 To day
    Dim o As Object = New Object()
    objs.Add(o)
Next

答案 2 :(得分:0)

另一种可能性

Dim Objects As New List(Of Object)(Enumerable.Repeat(New Object, Date.Now.Day))