简体   繁体   English

vb.net词典词典

[英]vb.net Dictionary of Dictionary

Good Evening, 晚上好,

Does anyone have any idea I can do something like this in VB.net? 有谁有任何想法我可以在VB.net中做这样的事情?

The problem I have is with declaring iDatabase as a Dictionary of iTable as it wants a class type. 我遇到的问题是将iDatabase声明为iTable的字典,因为它需要类类型。

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 . 看起来你想要一个“字典词典”,你有"User"作为键,然后在该键下,另一个字典条目带有映射到UserGuid键。 In that case, you probably don't need to subclass Dictionary(Of TKey, TValue) at all. 在这种情况下,您可能根本不需要子类Dictionary(Of TKey, TValue)

If you are going to have multiple classes (eg Admin , User ), then you should use a common interface. 如果您要有多个类(例如AdminUser ),那么您应该使用通用接口。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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