VB.NET Property as StringDictionary?

764 Views Asked by At

I'm trying to create a new property with a type of StringDictionary but when I use the property like a stringdictionary variable I am getting an error. The goal is to NOT use a variable, I need to use a property for this. Behind the scenes I am saving the stringdictionary value to a global user-ID indexed collection. Here's the code that creates the property and attempts to get and set:

Public Property MaxLenDict As StringDictionary()
    Get
        Return GetFromCollection("MaxLenDict")
    End Get
    Set(Value As StringDictionary())
        SaveToCollection("MaxLenDict", Value)
    End Set
End Property

Public Sub ExampleSub()
    If MaxLenDict("hello world") = "" Then MaxLenDict.Add("Hello World", "I'm Here")
End Sub

Get this error in ExampleSub "StringDictionary cannot be converted to string" in the IF statement on this code:

MaxLenDict("hello world")=""

So how do I successfully make and use a property as a stringdictionary?

1

There are 1 best solutions below

0
Konrad Rudolph On BEST ANSWER

Your property is of type StringDictionary() (an array!), not StringDictionary.

I’m not sure that using StringDictionary is advised in the first place, though. The class is simply a remnant from pre-generics versions of .NET. Use Dictionary(Of String, String) instead.