0

I'm looking to define string variables from an array of strings.

x(2) = {"bar","foo"}

How do I create variables out of bar and foo? And then how do I assign them a value?

2
  • Can you explain yourself more clearly? Commented Dec 28, 2011 at 1:31
  • I want to be able to create bar as a string (like you would Dim bar as String) and then assign bar a value, bar = "x" Commented Dec 28, 2011 at 1:32

3 Answers 3

2

Use a Dictionary(Of String,String)

 Dim x() As String = {"bar", "foo"}
 Dim dict As New Dictionary(Of String, String)
 For Each s In x
     dict.Add(s, "your value")
 Next

You can read/write the values very easy and fast:

dict("foo") = "another value"

The array-value is the key for the dictionary entry. Every key must be unique.

Sign up to request clarification or add additional context in comments.

2 Comments

So Dictionary is just a more advanced 2D array, I might as well keep my data in a 2D string array...
It's easy to use and has faster read access via key. You don't need to iterate through the whole array to find it.
0

Use a Dictionary<string, object>

Comments

0

There is no such thing as the associate array in .net Like what joel suggests, you can use a dictionary

//in c#:
Dictionary<string, string> myDictionary = new Dictionary<string, string>();
myDictionary.Add("bar", "x");
myDictionary.Add("foo", "fooValue");
string barValue = myDictionary["bar"];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.