首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >访问字典中的类变量的VB.NET

访问字典中的类变量的VB.NET
EN

Stack Overflow用户
提问于 2012-03-26 23:15:41
回答 2查看 1.2K关注 0票数 1

我不确定在Dictionary声明中访问类的属性所需的语法。

代码语言:javascript
复制
Public food As New Dictionary(Of String, cheese) From
{
    {"cheese1", New cheese},
    {"cheese2", New cheese},
    {"cheese3", New cheese}
}

Public Class cheese
    Public info As New Dictionary(Of String, Array) From
    {
        {"attributes1",
            {New Dictionary(Of String, String) From
                {
                    {"name", "test"},
                    {"taste", vbNullString},
                    {"color", vbNullString}
                }
            }
        },
        {"attributes2",
            {New Dictionary(Of String, String) From
                {
                    {"name", "test"},
                    {"taste", vbNullString},
                    {"color", vbNullString}
                }
            }
        }
    }
End Class

那么,如果我想测试它并使用MsgBox(),我该如何向下拉取,比方说food > cheese1 > info > attributes2 > name中的name

编辑:我刚刚意识到info中的Array需要是一个关联数组的字典,所以为了这个问题,请忽略这个错误并假设它是一个字典。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-03-26 23:20:55

好吧,下面是如何做到这一点的(考虑到你的Array注释):

代码语言:javascript
复制
Dim name As String = food("cheese1").info("attributes2")("name")

如果将内部字典保留为<String, Array>,那么将返回第0个字典"name“值:

代码语言:javascript
复制
Dim name As String = food("cheese1").info("attributes2")(0)("name")

但正如我在我的评论中所暗示的那样,这真的是一个糟糕的设计。以下是重做此操作的一种方法:

代码语言:javascript
复制
Dim food As New Food()
food.CheeseAttributes.Add(New Cheese("Cheddar", "Awesome", "Yellow"))
food.CheeseAttributes.Add(New Cheese("Pepperjack", "Spicy", "White"))

这可以通过将您的类重构为以下内容来实现:

代码语言:javascript
复制
Public Class Food

    Private _cheeseAttributes As IList(Of Cheese)

    Public Sub New()

    End Sub

    Public ReadOnly Property CheeseAttributes() As IList(Of Cheese)
        Get
            If _cheeseAttributes Is Nothing Then
                _cheeseAttributes = new List(Of Cheese)()
            End If
            Return _cheeseAttributes
        End Get
   End Property

End Class

Public Class Cheese

    Private _name As String
    Private _taste As String
    Private _color As String

    Public Sub New (ByVal name As String, ByVal taste As String, ByVal color As String)
        Me.Name = name
        Me.Taste = taste
        Me.Color = color
    End Sub

    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property

    Public Property Taste() As String
        Get
            Return _taste
        End Get
        Set(ByVal value As String)
            _taste = value
        End Set
    End Property

    Public Property Color() As String
        Get
            Return _color
        End Get
        Set(ByVal value As String)
            _color = value
        End Set
    End Property
End Class

可能还有更好的方法,但这里只是为了说明。

票数 2
EN

Stack Overflow用户

发布于 2012-03-26 23:22:55

提供一些方法来帮助提取条目会有所帮助,但我相信您现在使用的语法会是这样的

food.Item("cheese1").info.Item("attributes2")(0).Item("name")

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9874791

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档