Dictionary containing an array of strings as value - vb.net

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)

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.

Vb Net check if arrayList contains a substring

I am using myArrayList.Contains(myString) and myArrayList.IndexOf(myString) to check if arrayList contains provided string and get its index respectively.
But, How could I check if contains a substring?
Dim myArrayList as New ArrayList()
myArrayList.add("sub1;sub2")
myArrayList.add("sub3;sub4")
so, something like, myArrayList.Contains("sub3") should return True
Well you could use the ArrayList to search for substrings with
Dim result = myArrayList.ToArray().Any(Function(x) x.ToString().Contains("sub3"))
Of course the advice to use a strongly typed List(Of String) is absolutely correct.
As far as your question goes, without discussing why do you need ArrayList, because array list is there only for backwards compatibility - to select indexes of items that contain specific string, the best performance you will get here
Dim indexes As New List(Of Integer)(100)
For i As Integer = 0 to myArrayList.Count - 1
If DirectCast(myArrayList(i), String).Contains("sub3") Then
indexes.Add(i)
End If
Next
Again, this is if you need to get your indexes. In your case, ArrayList.Contains - you testing whole object [string in your case]. While you need to get the string and test it's part using String.Contains
If you want to test in non case-sensitive manner, you can use String.IndexOf

Efficient way to get part of a string in vb.net

I have to parse a string to obtain a specific value in it.
Here's an example of a string I need to parse: "#MSG 12,9: NINJUTSU"
Here I need to obtain the 12 value. The order of the value will not change, meaning that I will always aim the first number in the string; however the length of the string (12, 9, 58) is variable (but never negative) and the message (NINJUTSU) is also changing.
So far I proceed like this:
Dim tempErrorList As List(Of String) = errorMsg.Split(New Char() {":"}).ToList()
Dim listErr As List(Of String) = tempErrorList(0).Split(New Char() {","}).ToList()
Dim errCode As List(Of String) = listErr(0).Split(New Char() {" "}).ToList()
However I don't like it because of the 3 splits needed to obtain the values. I do not know how I could do it in one shot or fewer operations?
Similar to the deleted answer, you could use String.Split like so: errorMsg.Split(" ,:".ToCharArray()), which does what you're doing above but with one function call.
errorMsg.Split(" ,:".ToCharArray())(1) would give you the "12" you need.
Alternatively, you can use combinations of String.SubString() with String.IndexOf(), but the logic can become unwieldy and opaque. String.Split Alternatives (MSDN) gives more details on this approach.

KeyNotFoundException when a Key with the requested value exists in VB.NET ASP.NET 2.0 Dictionary

The ValueList variable is defined as Dictionary(Of String, String)
The watch values captured show the data (tmpData) has a value of "1".
The ValueList is defined such that the replacement value for "1" is "Project Resource" as can be seen form the expansion of the SSGCol.ValueList Watch variable.
However, when I try to access the value I receive a KeyNotFoundException
I have done this sort of thing thousands of times in the past, but for some reason today I cannot get it to work. I must be missing something really obvious and need someone to take a fresh look and give me a slap when they see my obvious mistake.
Perhaps there are some unprintable characters in the string. Try comparing the bytes in the strings, for instance:
Dim bytes() As Byte = System.Text.Encoding.Unicode.GetBytes(tmpData)
This works for me...
Dim lst As New Dictionary(Of String, String)
lst.Add("1", "Test")
Dim tmpS As String = String.Empty
lst.TryGetValue("1", tmpS)

how to read/write from/into a list of dictionary

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.