I'm a PHP guy, and in PHP I would do something like the following:
$variable = array('0001'=>'value1', '0010'=>'value2');
I'm pretty new to VB.NET, so how do I translate the above code to VB.NET?
I think I have to use a dictionary:
Dim variable As New Dictionary(Of String, String)
variable.Add("0001", "value1")
variable.Add("0010", "value2")
Is this the correct way of doing it or should I use something else for this?
It is a way, or if you prefer a one-liner way of initialization, you can do this:
Dim variable As New Dictionary(Of String, String) From { {"0001", "value1"}, {"0010", "value2"} }
As far as which is the better one, it's more a matter of coding standard, and/or personal preference.
Considering what kind of container to use, you should use only those from System.Collection.Generics in .NET unless you are forced otherwise. And Dictionary is the default associative container. You can see the alternatives (SortedDictionary for example), if it matches more your use case.
You can use Collection:
Dim var As New Collection
var.Add("vakue1", "0001")
var.Add("value2", "0010")
Loop through all with:
For Each v As String In var
Next
For other ways of treating items, look at the sample in Collection(Of T) Class (MSDN).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With