0

I have an array that is created dynamically depending on different parameters. Its items can be (e.g.) Item(0) = Colorado or Item(0) = Arkansas, Item(1) = Utah or Item(0) = Utah, Item(1) = Colorado, Item(2) = Arkansas. But every string occurs only once, so Item(0) = Colorado, Item(1) = Colorado is not possible.

What I try to get is an array with the shortversions of the statenames

Example: Starting Array: Item(0) = Colorado, Item(1) = Arkansas

New Array: Item(0) = Col, Item(1) = Ark

So, I need to store information as follows:

|Shortversion| Fullversion |
|:-----------|------------:|
| Col        |    Colorado |   
| Ark        |    Arkansas | 
| Uth        |        Utah | 

So I was looking for a possibiltiy to store the information and get the full state names into another array.

I accomplished that by creating a datatable with the information. For each arrayelement I could find the shorthand version and return the fullname. But that is not a 'performance-oriented' solution. Could anyone help me finding a solution that has a better performance?

1 Answer 1

1

But that is not a 'performance-oriented' solution

You could use a Dictionary<string, string>:

var states = new Dictionary<string, string>();
states.Add("Colorado", "Col");
// ...

string abbreviation = states["Colorado"]; // Col

In this example the key is the full name and the value is the short-version. Note that the keys must be unique, but that should be the case since you've mentioned that "every string occurs only once".

VB.NET:

Dim states As New Dictionary(Of string, string)
states.Add("Colorado", "Col")

Dim abbreviation As String = states("Colorado")
Sign up to request clarification or add additional context in comments.

4 Comments

but what if one need to have same shortversion for two fullversion, dictionary will not allow it?
@CodingDefined: what's the problem? states.Values can contain dulicates.
@TimSchmelter According to http://msdn.microsoft.com/en-us/library/k7z0zy8k(v=vs.110).aspx if you use same key for two values you will get ArgumentException : An element with the same key already exists in the Dictionary<TKey, TValue> Is there anything i am missing?
@CodingDefined: the key is not the short-version but the value. The key(full-name of state) is unique as stated by OP.

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.