Use Dictionary by variable name - vb.net

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.

Related

How to use the Type class as type in variable declaration

I want to make the same operations on different lists of controls of specific types. I could do it one type at a time, but I figured: "There's a bunch of these, so why not do this in a loop?" and now of course I'm wondering where I went wrong. The problem can be reproduced easily:
Dim myType As Type = GetType(TextBox)
Dim newList As New List(Of myType) ' this is not allowed
This is the specific error message: BC30002: Type 'currentType' is not defined
Is there a way to use an object of the Type class as a type, or to do a similar maneuver which would let me do something like this:
For Each currentType As Type In {GetType(myCustomType), GetType(anotherCustomClassType)}
Dim newList As New List(Of currentType)
' do stuff
Next
No there isn't. When you create a generic List, you must specify a data type. A Type object is not a data type. It's an object that contains information about a data type. Basically, when you create a List(Of T) you can only fix T to be something that you could have passed to GetType in the first place. Both require data types.
You can't put data types in a list because they are not objects. What you could do is write a generic method that does what you want for one type, e.g.
Private Sub DoStuff(Of T)()
Dim newList As New List(Of T)
'Do stuff here.
End Sub
and then call that method and specify different generic types, e.g.
DoStuff(Of SomeType)()
DoStuff(Of SomeOtherType)()
'Etc.
You have to make individual calls though, because you need to specify the generic type.

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)

vb.NET Select distinct... how to use it?

Coming from a C# background I am a bit miffed by my inability to get this simple linq query working:
Dim data As List(Of Dictionary(Of String, Object))
Dim dbm As AccessDBManager = GlobalObjectManager.DBManagers("SecondaryAccessDBManager")
data = dbm.Select("*", "T町丁目位置_各務原")
Dim towns As IEnumerable(Of String())
towns = data.Select(Function(d) New String() {d("町名_Trim").ToString(), d("ふりがな").ToString()})
towns = towns.Where(Function(s) s(0).StartsWith(searchTerms) Or s(1).StartsWith(searchTerms)).Distinct()
Call UpdateTownsListView(towns.ToList())
I pasted together the relevant bits, so hopefully there is no error here...
data is loaded from an access database and is a list with the data from each row stored as a dictionary.
In this case element from data has a field containing the name of a Japanese town and its reading and some other stuff like the row ID etc.
I have a form with a textbox. When the user types something in, I would like to retrieve from data the town names corresponding to the search terms without duplicates.
Right now the results contain loads of duplicates> How can I get this sorted to only get distinct results?
I read from some other posts that a key might be needed, but how can I declare this with extension methods?
Distinct uses the default equality comparer to compare values.
Your collection contains arrays of strings, so Distinct won't work the way you expected since two different arrays never equals each other (since ReferenceEquals would be used in the end).
A solution is to use the Distinct overload which takes an IEqualityComparer.
Class TwoStringArrayEqualityComparer
Implements IEqualityComparer(Of String())
Public Function Equals(s1 As String(), s2 As String()) As Boolean Implements IEqualityComparer(Of String()).Equals
' Note that checking for Nothing is missing
Return s1(0).Equals(s2(0)) AndAlso s1(1).Equals(s2(1))
End Function
Public Function GetHashCode(s As String()) As Integer Implements IEqualityComparer(Of String()).GetHashCode
Return (s(0) + s(1)).GetHashCode() ' probably not perfect :-)
End Function
End Class
...
towns = towns.Where(...).Distinct(new TwoStringArrayEqualityComparer())

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.

Iterate through generic list of unknown type at runtime in VB.Net

Does anyone know how to iterate over a generic list if the type of that list isn't known until runtime?
For example, assume obj1 is passed into a function as an Object:
Dim t As Type = obj1.GetType
If t.IsGenericType Then
Dim typeParameters() As Type = t.GetGenericArguments()
Dim typeParam As Type = typeParameters(0)
End If
If obj is passed as a List(Of String) then using the above I can determine that a generic list (t) was passed and that it's of type String (typeParam). I know I am making a big assumption that there is only one generic parameter, but that's fine for this simple example.
What I'd like to know is, based on the above, how do I do something like this:
For Each item As typeParam In obj1
'do something with it here
Next
Or even something as simple as getting obj1.Count().
The method that iterates over your list can specify a generic type:
Public Sub Foo(Of T)(list As List(Of T))
For Each obj As T In list
..do something with obj..
Next
End Sub
So then you can call:
Dim list As New List(Of String)
Foo(Of String)(list)
This method makes the code look a little hairy, at least in VB.NET.
The same thing can be accomplished if you have the objects that are in the list implement a specific interface. That way you can populate the list with any object type as long as they implement the interface, the iteration method would only work on the common values between the object types.
If you know that obj is a Generic List. Then you're in luck.
Generic List implements IList and IEnumerable (both are non-generic). So you could cast to either of those interfaces and then For Each over them.
IList has a count property.
IList also has a Cast method. If you don't know the type to cast to, use object. This will give you an IEnumerable(Of object) that you can then start using Linq against.