2017-08-10 58 views
0

如果今天是第3天,我想做三個對象。如何在Visual Basic中動態聲明對象

Dim Obj1 As Object 

Dim Obj2 As Object 

Dim Obj3 As Object 

如果今天是5號,我想做3個obejects。

Dim Obj1 As Object 

Dim Obj2 As Object 

Dim Obj3 As Object 

Dim Obj4 As Object 

Dim Obj5 As Object 

那麼我該如何動態地聲明對象呢? 謝謝您的閱讀。

+1

如果你想你會希望他們保持在一個數組或對象列表的一個不確定的數字。 – ADyson

回答

2

你必須讓他們在一個數組或列表或其他一些類型的集合。這裏是你如何能實現它:

Dim objects = New List(Of Object) 

For i = 1 To Date.Today.Day 
    objects.Add(New Object()) 
Next 
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 
0

另一種可能性

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