string to object or to a dictionary

elic05

Member
Joined
Nov 2, 2008
Messages
19
Programming Experience
Beginner
i have this string
"a$john--b$jim--c$jane"
I want to convert it to an object
so
obj.a will be john
obj.b will be jim
etc

or to a dictionary

how can I do it in vb.net
thanks
 
If you don't know how many data items there will be and/or what the keys will be then an object with specific properties is impractical. A Dictionary would be the way to go. This is the conventional way:
Dim myDictionary As New Dictionary(Of String, String)

For Each item In myString.Split(New String() {"--"}, StringSplitOptions.None)
    Dim parts = item.Split("$"c)

    myDictionary.Add(parts(0), parts(1))
Next
This is another option using LINQ:
Dim myDictionary = myString.Split(New String() {"--"}, StringSplitOptions.None).
                            Select(Function(s) s.Split("$"c)).
                            ToDictionary(Function(a) a(0), Function(a) a(1))
 
Back
Top