Vb.net 我做错了什么:结构无法索引,因为它没有默认属性(WITH子句)

Vb.net 我做错了什么:结构无法索引,因为它没有默认属性(WITH子句),vb.net,Vb.net,我得到:“结构无法索引,因为它没有默认属性”。我做错了什么 With grid.Rows(10).Cells Dim note As New Note With {.ID = "Blah", _ .Date = "1/1/2011", _ .Message = "AAA", _ .Type = "ABC"

我得到:“结构无法索引,因为它没有默认属性”。我做错了什么

With grid.Rows(10).Cells
    Dim note As New Note With {.ID = "Blah", _
                               .Date = "1/1/2011", _
                               .Message = "AAA", _
                               .Type = "ABC", _
                               .SubType = "DEF", _
                               .ReferenceKey = !SetRefNum.Value}
End With
注意如下所示:

Public Structure Note
    Public Property ID As String
    Public Property [Date] As Date
    Public Property Message As String

    Public Property Type As String
    Public Property SubType As String
    Public Property ReferenceKey As String
 End Structure

问题出在
上!SetRefNum.Value
。您可以使用
运算符引用集合(通常是字典)中的索引。
Note
类需要一个
Default
属性来使用此术语

Class Note
    Public ReadOnly Default Property Item(Key As String) As String
        Get
            Return Settings(Key)
        End Get
    End Property
    Private Settings As New Dictionary(Of String, String)
End Class
所以术语
!SetRefNum.Value
将转换为
note.Item(“SetRefNum.Value”)

我猜你在追求完全不同的东西

有关更多信息,请参阅页面的成员访问操作员部分

编辑:编辑已编辑问题的已编辑答案

我建议采取以下办法:

With grid.Rows(10).Cells
    Dim ReferenceKey As String = !SetRefNum.Value
    Dim note As New Note With {.ID = "Blah", _
                               .Date = "1/1/2011", _
                               .Message = "AAA", _
                               .Type = "ABC", _
                               .SubType = "DEF", _
                               .ReferenceKey = ReferenceKey}
End With
不知何故,我怀疑这也行得通。您可能需要以下内容:

With grid.Rows(10).Cells
    Dim ReferenceKey As String = .Item(SetRefNum.Value)

字符用于C#。对于VB,您需要使用
而不是
关键字。所以,它应该是这样的:

Dim note As New Note With {.ID = "Blah", _
                           .Date = "1/1/2011", _
                           .Message = "AAA", _
                           .Type = "ABC", _
                           .SubType = "DEF", _
                           .ReferenceKey = Not SetRefNum.Value}
Dim note = New Note With {.ID = "Blah", 
                          .Date = "1/1/2011", 
                          .Message = "AAA", 
                          .Type = "ABC", 
                          .SubType = "DEF", 
                          .ReferenceKey = Not SetRefNum.Value}
此外,假设由于结构中的自动属性而使用Visual Studio 2010,则不需要尾随下划线。我会这样写:

Dim note As New Note With {.ID = "Blah", _
                           .Date = "1/1/2011", _
                           .Message = "AAA", _
                           .Type = "ABC", _
                           .SubType = "DEF", _
                           .ReferenceKey = Not SetRefNum.Value}
Dim note = New Note With {.ID = "Blah", 
                          .Date = "1/1/2011", 
                          .Message = "AAA", 
                          .Type = "ABC", 
                          .SubType = "DEF", 
                          .ReferenceKey = Not SetRefNum.Value}

编辑:好吧,您的ReferenceKey字段是一个字符串,所以我不确定您要的是布尔值。也许@Hand-E-Food更接近正确答案。

我遗漏了我的部分代码。。。你说得对,这是在一个“带”的街区里。我改变了原来的帖子。问题是你有两个
块:
和grid.Rows(10)单元格
和note
。有关建议,请参见编辑后的答案。