Matt Farrell
Matt Farrell

Reputation: 199

Value of items in a c# listbox

I am sure if i look hard enough i can find my answer but so far i can't find a clear cut answer.

What i am trying to do is use the value of an item in a listbox which contains 7 items as a numerical identifier for a specific array element. (all items in the list are strings)

array[listbox.value] = my new data for that array element

i know i can pull the string of the item out but and that i can identify a specific item in the list with.

list1.Items[value].ToString();

i just want to know if i can do reverse the alternative is a pain to code as its a lot more lines of code comparing the string in the list to each item in my array until i find a match while i know all items in the list are the same order as the array.

Upvotes: 0

Views: 114

Answers (1)

Ron Beyer
Ron Beyer

Reputation: 11273

Instead of using an array, you can use a dictionary.

Dictionary<string, valueType> myArray = new Dictionary<string, valueType>();
myArray["Item1"] = some value
myArray["Item2"] = some value

... etc

Then later

myArray[listbox.Value.ToString()] = my new value

Thats really the best way to refer to an array index by a string value. The type for the indexer does not necessarily need to be a string, it can be any type that is uniquely identifiable.

Upvotes: 2

Related Questions