2012-12-07 112 views
0

我不知道我正在嘗試這樣做的確切名稱,因此忍受着我...基本上我試圖創建一個對象,多個屬性..每一個都是獨一無二的。下面是我迄今爲止..創建一個對象或屬性的多個實例來存儲屬性值

Public Class TestingProperties 

    Private m_TestId As Integer 
    Public Property TestId() As Integer 
     Get 
      Return m_TestId 
     End Get 
     Set(ByVal value As Integer) 
      m_TestId = value 
     End Set 
    End Property 

    Private m_TestName As String 
    Public Property TestName() As String 
     Get 
      Return m_TestName 
     End Get 
     Set(ByVal value As String) 
      m_TestName = value 
     End Set 
    End Property 

End Class 

然後我下面將主要是什麼是包含在以上爲屬性的每個對象......

Dim x As TestingProperties 
    x = New TestingProperties 
    x.TestName = "N/A" 
    x.TestName = "Name1" 
    x.TestName = "Name2" 
    x.TestId = "0" 
    x.TestId = "1" 
    x.TestId = "2" 

這顯然行不通因爲最後一個testId和最後一個TestName會覆蓋對象屬性中包含的所有內容。一旦我明白了這一點,數據將通過一個SQL存儲過程來提供,但是格式應該仍然是類似的......這個壯舉中的任何想法都是我早就應該學會的。

回答

4
Dim x As New List(Of TestingProperties) 

    For i = 0 To 9 
     Dim newx As New TestingProperties 
     newx.TestId = i 
     newx.TestName = "Name " & i 
     x.add(newx) 
    Next 

現在你有10個獨特的屬性。你可能會訪問它們,如:

Console.WriteLine(x(0).TestId) 
+0

Woot ...認爲這是一些瘋狂的簡單.. – Skindeep2366

相關問題