ruedi
ruedi

Reputation: 5545

Find arrayelement and return associated value

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?

Upvotes: 0

Views: 42

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460158

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")

Upvotes: 1

Related Questions