StringCollection or ArrayList within Class - vb.net

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

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.

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)

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.

List with different object types?

Can I have a List containing one string and two numbers? Or I can only have one type of element?
If that's the kind of functionality you want, then I would look at the non-generic System.Collections.ArrayList class.
Update
For those of you who aren't going to read the huge comment chain...it looks like Adam Robinson is on to something using List<object> over ArrayList. Both will work but on large collections it seems like List<object> is measurably faster than ArrayList.
You can. A list of Objects can do that. But, you lose type safety with that and also design time intelliSense.
What do you want to do? You could also use a class with 3 members.
No, containers like List(Of T) store exactly one type T of elements. You can, though, make this one type consist of one string and two numbers.
Structure Foo
Public Desc As String
Public x As Integer, y As Integer
End Structure
Dim List = New List(Of Foo)
Yes, you can.
dim myVehicles as new list(of object)
dim myCar as new car
dim myBike as new bike
dim mySecondCar as new car
myVehicles.add(myCar)
myVehicles.add(myBike)
myVehicles.add(mySecondCar)

VB.NET List(of X).Contains Behavior

I have a custom class set up as a key that has two properties, X and Y
I have something similar to this:
Dim test As New List(of TestClass)
Dim key as New TestData
key._a = A
key._b = B
For Each a As TestClass In SomeCollection
If Not test.Contains(key) Then
'Do Stuff
End If
Next
My question is this: How does the .Contains on the List(of T) behave? Does it look for an identical data structure, or does it simply match on one of the properties of my key?
If you can, please include a link where I can look at some documentation regarding this.
EDIT
Is the Contains method Typesafe?
It uses the Equals method to check for identity.
By default (if not overridden) Equals returns true if two references are identical or two structures are equal memberwise.
I just asked the same question yesterday :)
Your TestClass should implement IEquatable(of T) http://msdn.microsoft.com/en-us/library/ms131187(VS.80).aspx interface to make it type safe