VB.net错开了不同类型的数组

时间:2015-02-05 00:29:00

标签: arrays vb.net dictionary

我正在VB.NET中创建一个Windows应用程序,其中需要存储大量关于不同应用程序的关联信息。我试图找出存储这些数据的最佳方法。

在做了一些研究之后,我发现的最佳解决方案是使用对象数组或对象字典。

这是我需要存储的数据结构:

var Info[AppID]['AppName']-> String (returns the app name as String)
               ['Exes'] -> Array of Strings
               ['RegKeys'] -> Array of Strings
               ['Versions'][VersionID]['PCID'] -> String
                                      ['Date'] -> DateTime
                                      ['Size'] -> Integer

没有引号的键是动态的,代表实际的AppID / VersionID,引号中的键是静态的(所以第二个键总是有'AppName''Exes'等。

例如:

Info[123ab]['Name'] = 'Internet Exploder'
           ['Exes'] = {'iexplore.exe', 'whatever.exe'}
           ['RegKeys'] = {'hkey/local machine .....'}
           ['Versions'][1]['PCID'] = 'My Desktop'
                          ['Date'] = Jan 1 1960
                          ['Size'] = 9001

将被声明并设置为

    Dim appinfo As New Dictionary(Of String, Object)
    Dim Lv2 As New Dictionary(Of String, Object)

    Dim Exes As New List(Of String)(New String() {"iexplorer.exe", "whatever.exe"})
    Dim RegKeys As New List(Of String)(New String() {"blah"})
    Dim Directories As New List(Of String)(New String() {"c:\program files\internet explorer"})
    Dim Name As String = "Internet Exploder"
    Dim Versions As New Dictionary(Of String, Object)
    Dim VersionsLv2 As New Dictionary(Of String, Object)

    Dim VersionID As String = "1"
    Dim PCID As String = "My Desktop"
    Dim TheDate As Date = Now
    Dim Size As Integer = 9001

    VersionsLv2.Add("PCID", PCID)
    VersionsLv2.Add("Date", TheDate)
    VersionsLv2.Add("Size", Size)

    Versions.Add("VersionID", VersionsLv2)

    Lv2.Add("Name", Name)
    Lv2.Add("RegKeys", RegKeys)
    Lv2.Add("Directories", Directories)
    Lv2.Add("Versions", Versions)

    appinfo.Add("abc12", Lv2)

我只是想知道是否有人知道更好的方法吗?我讨厌为了初始化变量而不得不从树的顶部开始工作,但这似乎工作正常。

非常感谢您的投入!

1 个答案:

答案 0 :(得分:1)

看起来你应该定义两种类型,例如

Class App

    Private ReadOnly _exes As New List(Of String)
    Private ReadOnly _regKeys As New List(Of String)
    Private ReadOnly _versions As New List(Of Version)

    Public Property AppId() As String
    Public Property AppName() As String

    Public ReadOnly Property Exes() As List(Of String)
        Get
            Return _exes
        End Get
    End Property

    Public ReadOnly Property RegKeys() As List(Of String)
        Get
            Return _regKeys
        End Get
    End Property

    Public ReadOnly Property Versions() As List(Of Version)
        Get
            Return _versions
        End Get
    End Property

End Class

Class Version

    Public Property VersionId() As String
    Public Property PcId() As String
    Public Property [Date]() As Date
    Public Property Size() As Integer

End Class

在该示例中,Version对象存储在简单集合中的App对象中,您可以通过索引获取它们并使用LINQ通过ID获取一个。如果您愿意,可以将Versions属性改为Dictionary。然后,您可以创建一个数组ListDictionary来存储App个对象。