2013-03-20 97 views
0

我有很多陣列在一個網絡模塊,我寫,隨着時間的推移,我會增加更多。我想動態枚舉所有數組來搜索和更新它們。這可能嗎?下面的代碼片段:是否可以動態枚舉VB .NET中的所有數組?

Private EMASCFee() As String = {0, 1, "12345679", "MASC Debt SetOff Fees"} 
Private EMSPen() As String = {2, 3, "987654312", "Medicare EMS Late Penalties"} 
Private Beach() As String = {4, 5, "110022233", "Beach Services"} 

Private allCodes() As Array 'Holds all Codes Arrays 

Private Sub addArrays()  
    'Adds all Codes String Arrays together into 1 Array *** Add NEW string arrays to the end of allCodes *** 
    allCodes = {EMASCFee, EMSPen, Beach} 
End Sub 

我想要做的是一樣的東西:

Private EMASCFee() As String = {0, 1, "12345679", "MASC Debt SetOff Fees"} 
Private EMSPen() As String = {2, 3, "987654312", "Medicare EMS Late Penalties"} 
Private Beach() As String = {4, 5, "110022233", "Beach Services"} 

Private allCodes() As Array 'Holds all Codes Arrays 

Private Sub addArrays()  
    For each myArray as Array in ModuleName 
     allCodes = {allCodes, myArray} 
    Next 
End Sub 
+1

爲什麼使用畢竟一個數組? Struct對我來說似乎更合理。你已經有了'allCodes'數組。你還想找什麼? – 2013-03-20 12:17:16

+0

你的意思是使用反射? – Jodrell 2013-03-20 12:17:25

+0

@NicoSchertler,我試圖通過在自己的字符串數組中添加每個代碼來簡化新代碼的輸入,並且我已經用這種方式添加了45+代碼。我能否將結構添加到allCodes數組並搜索/更新每個數組? – JFV 2013-03-21 12:18:42

回答

1

如果你真的想堅持到陣列,這裏是一個辦法做到這一點:

Module Codes 
    Private EMASCFee() As String = {0, 1, "12345679", "MASC Debt SetOff Fees"} 
    Private EMSPen() As String = {2, 3, "987654312", "Medicare EMS Late Penalties"} 
    Private Beach() As String = {4, 5, "110022233", "Beach Services"} 

    Public Function GetAllCodes() As String()() 
     Dim t = Type.GetType("WindowsApplication1.Codes") 'Change this to your project namespace 
     Dim members = t.GetFields(Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Static) 
     Dim result As New List(Of String()) 
     For Each m In members 
      If m.FieldType = GetType(String()) Then 
       result.Add(m.GetValue(Nothing)) 
      End If 
     Next 
     Return result.ToArray() 
    End Function 
End Module 

可是,我真的建議使用struct和泛型列表,而不是數組。整體概念將是相同的。

+0

我確實有一個類似於結構的格式(雖然不是結構體),但是我的管理員想要添加每組值的簡化方式,這就是爲什麼我要使用數組。這樣他就可以添加一行所有的值,現在(感謝你)他不需要記住將字符串數組添加到allCodes數組。謝謝您的幫助! – JFV 2013-03-21 14:23:41

相關問題