Click here to Skip to main content
15,896,479 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I've another challenge ahead.

I want to save a "dictionary" in a database in a efficient way. PHP uses a serialize() function, is there something similar available in vb.net?

The idea is to have a dictionary containing a string and a boolean defined like:
VB
Dim user_settings As New Dictionary(Of String, Boolean)


I've searched the internet to find some information but I can't find a good solution.

One approach is to make a foreach loop converting each item into a string. (and vice versa). But it's a quite inefficient way I think?

Thanks!
Posted

Take a look at my article fastJSON for serializing data like Dictionary of strings etc.

Also you can use RaptorDB which is a disk based Dictionary of anything.
 
Share this answer
 
After a few more hours I made this:

VB
' convert Dictionary to a string
Dim arr1() = dictionary1.ToArray()
Dim dataString As String = String.Join("", arr1).Replace(", ", "=")


' convert string to a Dictionary
Dim arr2() As String = dataString.Split({"["c, "]"c}, _
      StringSplitOptions.RemoveEmptyEntries)
Dim dictionary2 = arr2.Select(Function(x) x.Split("="c)).ToDictionary(Function(i) _
      i(0), Function(i) Boolean.Parse(i(1)))


For me it's working...

Thanks!
 
Share this answer
 
v2
A database table essentially IS a dictionary. Create a table with one column, SettingName (perhaps use a type of varchar(100)), and another column, SettingValue (maybe use a bit field). Add an index to SettingName. Insert each key/value pair in the dictionary as a row in the table.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900