Denis
Denis

Reputation: 12087

What am I doing wrong: Structure cannot be indexed because it has no default property (WITH CLAUSE)

Am getting: "Structure cannot be indexed because it has no default property". What am I doing wrong?

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

And Note looks like:

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

Upvotes: 2

Views: 10567

Answers (2)

Hand-E-Food
Hand-E-Food

Reputation: 12814

The problem is with !SetRefNum.Value. You use the ! operator to reference an index in a collection, usually a dictionary. The Note class requires a Default property to use this terminology.

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

So the term !SetRefNum.Value will translate to note.Item("SetRefNum.Value").

I'm going to take a guess you're after something completely different.

See the Member Access Operators section of the page Special Characters in Code (Visual Basic) for more information.

Edit: Ammended answer for ammended question.

I suggest the following approach:

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

Somehow, I doubt that will work either. You might need something like:

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

Upvotes: 2

CoderDennis
CoderDennis

Reputation: 13837

The ! character is used in C#. For VB, you'll want to use the Not keyword. So, it should look like this:

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

Also, assuming you're using Visual Studio 2010 because of the auto properties in your Structure, you don't need the trailing underscores. I would write it like this:

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

Edit: Well, your ReferenceKey field is a String, so I'm not sure you were going for the boolean value. Maybe @Hand-E-Food was closer to the right answer.

Upvotes: 1

Related Questions