how to read/write from/into a list of dictionary - vb.net

i want to know how to manipulate the data from a
Public ListD As New List(Of Dictionary(Of String, String))
meaning read/write. Can you help me please?
I've tried with
ListD.Add("string_as_key", var_as_value)
but it haven't worked
Thanks!

In the case of
("string_as_key", var_as_value)
To add that to your ListD you would first have to either create a dictionary and add that item to it, or add that item to one of the existing dictionaries.
For example:
'Create dictionary
Dim dic as new Dictionary(Of String, String)
dic.Add("string_as_key", var_as_value)
'Add it to list
ListD.Add(dic)
To read a single item from your ListD would look something like:
Dim dic As Dictionary(Of String, String) = ListD(0)
Dim var_as_value As String = dic("string_as_key")
A dictionary is a group of multiple items in itself. So if you have several pairs of things with unique items you can use as a key, a dictionary is a good choice.
I may be way off here since I don't know what you are using it for, but I get the impression that rather than a list of Dictionary you may be better of with just a dictionary(Of String, String)

Your question can't really be 'answered' but to give you a start have a look at the online documentation, specifically:
List(Of T) Class
Dictionary(Of TKey, TValue) Class
How to loop in VB.NET <- Start here and work upwards when you understand each one.

Related

Use Dictionary by variable name

I have several dictionary types assigned like this
Public aryAAA As New Dictionary(Of Integer, Dictionary(Of String, String))
Public aryBBB As New Dictionary(Of Integer, Dictionary(Of String, String))
Public aryCCC As New Dictionary(Of String, String)
Public aryDDD As New Dictionary(Of String, String)
In a database I stored the names of aryAAA, aryBBB, aryCCC, and aryDDD. In my program if I read a database record and it has aryCCC returned, I then want to be able to use that dictionary.
I was thinking that I would have to assign an object to the aryCCC by iterating through the system.collecton.generic.dictionary and then use that object to retrieve the data stored. Not sure how I would do this or if there is a better way to use the dictionary by a variable returned name?
Thanks for any help on this.
You'll need a Select or If/Else for this. We could do a little better within the type system if all the dictionaries held the same type of object, but since there are differences this is the best we can do:
If dbValue = "aryCCC" Then
'Do stuff with aryCCC
Else If dbValue = "aryAAA" Then
'Do stuff with aryAAA
'etc
End If
Even reflection won't help much here, since those variables aren't members and we'd still have the different types to deal with.
Ultimately, you have a run-time value from the database you want to match up to compile-time variable names, and that never goes well.

check if key exists in Icollection using vb.net

Is there an easy way to check if a particular key exists in a keyvalue collection without ofcourse looping through the collection ?
Like an alternative for Dictionary.ContainsKey() ?
Public mod_filters As ICollection(Of KeyValuePair(Of String, String)) = New Dictionary(Of String, String)
this is my collection . I need to find if a particular key exists in the collection. If it doesnt then add that key and a corresponding value.

Dictionary containing an array of strings as value

Is it possible to have a string array as the value in a dictionary? I need to save the description (Hour2) as the key and as value being able to access both the price (elements_PR(4)) and the time offset (1). Is there a good way to do that?
Dim pr_val(2) As String
Dim PR As New Dictionary(Of String, pr_val)
PR.Add("Hour2", {elements_PR(4), "1"})
There is no reason why you can't do it - you could also use values of type Tuple(Of String, String), if the number of elements is fixed in advance - in this case, 2. It'd be easier to perform comparisons on, and would also be immutable, which is often a good idea.
You sure can. Try something like this:
Dim PR As New Dictionary(Of String, IEnumerable(Of String))
PR.Add("Hour2", {elements_PR(4), "1"})
It seems to me that you could create a class representing the price and the time offset. Therefore, you could do something like PR.Add("Hour2", instanceOfClass).
Depending on the meaning of the description in your situation, you could even include it in your class. It would allow you to use another approach with a List(Of YourClass) containing a list of items with the description, the price and the time offset. To retrieve an item by "key", you could then use some Linq.
Short answer - yes. Assuming the following was declared somewhere:
Dim elements_PR(4) As String : elements_PR(4) = "Hello"
Dim PR As New Dictionary(Of String, String())
You can either do:
PR.Add("Hour2", {elements_PR(4), "1"})
or
Dim pr_val() As String = {elements_PR(4), "1"}
PR.Add("Hour1", pr_val)

Is a hashset needed for contains with List(of String) Vb.net

Would the following:
Dim stringlist As List(Of String)
Dim stringlisthas = stringlist.Contains("thing1")
be any slower than
Dim stringlist As List(Of String)
Dim stringlisthash As New HashSet(Of String)(stringlist)
Dim stringlisthas = stringlisthash.Contains("thing1")
Is a hashset needed for contains?
Is a hashset needed for contains?
Needed? No.
Would [List<T>.Contains] be any slower than [HashSet<T>.Contains]?
Probably. It depends on how List<T>.Contains is implemented (its probably a linear search).
I'll answer a question you didn't ask.
Does it matter?
It depends. You have to code up both, profile it, and see if it's a bottleneck in your application. If it's not, just stick with List<T>.Contains.

StringCollection or ArrayList within Class

I have a class called CookieMonster, its objective is to simply create a cookie based on 3 parameters passed to it. The cookie name, the cookie name-value pairs and the cookie expiry date.
I have experimented with List(of T) and Array and StringCollection, but I'm unsure which is the best for passing the name-value pairs and providing that information to the class.
Ideally, I'd like to be able to do something like this:
Dim l As New List(Of String)
l.Add("name", "value")
l.Add("name", "value")
Dim c as New CookieMonster()
c.Name = "My New Cookie"
c.Values = l
c.Expires = Date.Now()
Has anyone got any suggestion or code snippets to send me on my way?
Help appreciated and welcomed.
Thanks
Use a Dictionary, its made for Key Value Pairs.
Dim values As New Dictionary(Of String, String)
values.Add("name1", "value1")
values.Add("name2", "value2")
Personally, I would use the Dictionary class for this type of scenario.
Example is within the documentation here:
http://msdn.microsoft.com/en-us/library/xfhwa508.aspx