简体   繁体   中英

vb.net Dictionary of Dictionary

Good Evening,

Does anyone have any idea I can do something like this in VB.net?

The problem I have is with declaring iDatabase as a Dictionary of iTable as it wants a class type.

Any help would be greatly appreciated.

Regards, Colin

Module Module1
    Sub Main()
        Dim Database As New iDatabase
        Dim Users = Database.Add("User", User)
        Users.Add(Guid.NewGuid, New User() With {.Username = "username"})
    End Sub
End Module

Public Class iDatabase
    Inherits Dictionary(Of String, iTable)

    Public Class iTable(Of Entity As Class)
        Inherits Dictionary(Of Guid, Entity)

    End Class
End Class

Public Class User
    Public Property Username As String
End Class

I think you have over-complicated this a bit. It looks like you want a "dictionary of dictionaries", where you have "User" as a key, and then under that key, another dictionary entry with a Guid key that maps to a User . In that case, you probably don't need to subclass Dictionary(Of TKey, TValue) at all.

If you are going to have multiple classes (eg Admin , User ), then you should use a common interface. Let's look at a complete example:

Public Interface IUser
    Property Username As String
End Interface

Public Class User Implements IUser
    Public Property Username As String Implements IUser.Username
End Class

Public Class Admin Implements IUser
    Public Property Username As String Implements IUser.Username
End Class

Sub Main()
    Dim Database As New Dictionary(Of String, Dictionary(Guid, IUser))
    Database.Add("User", New Dictionary(Of Guid, IUser))
    Database("User").Add(Guid.NewGuid, New User() With {.Username = "username"})
    Database("User").Add(Guid.NewGuid, New Admin() With {.Username = "username"})
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM