[...]
a ) I have 5 State(s) names. eg..Ohio, Iowa, Michigan,
Illinois,Kentucky
b) For each State, I have a list of cities that I need to store...
eg Illinois -> Chicago, etc..
Michigan -> Dearborn etc...
Is creating a 5x10 array and storing the information, a good way to go
about doing this...or is there a better/more efficient way to do
this...
I have looked at the concept of 'structs'..but I dont seemed to have
fully understood how to go about using them...
All due respect, you need to be reasonably familiar with both structs and
classes before you try to do anything of import in C#. (Likewise any
similar data structures that might exist in any language you might be
using). They are fundamental to the use of the language, and if you don't
understand how a struct or a class works, you're not ready to try to write
a program that does something useful (obviously there are lots of more
basic programs that you might write, and which in fact would be a
worthwhile exercise as you learn about the language).
Now, all that said, it seems to me that a multi-dimensional array isn't
really what you want here. It would work (though it's not clear from your
question that only being able to store 10 city names is acceptable), but
it's somewhat crude compared to the alternatives.
The most basic solution would be to have five named arrays:
string[] rgstrOhio = { "Cincinatti", "Akron", "Cleveland" };
string[] rgstrIowa = { "Iowa City", "Des Moines", "Dubuque" };
// etc.
(of course, you are not limited to three cities for each state...you can
put as many into each array as you like).
Unless you already have a natural mapping from a state name to an index,I
think this would be at least as good as a multi-dimensional array, if not
better. I suppose that if you did already have a direct mapping from
state name to index, you could do something like this:
string[][] rgrgstrCities = { { "Cincinatti", "Akron", "Cleveland"},
{ "Iowa City", "Des Moines", "Dubuque"},
// etc. };
But then you'd have to get the city arrays by indexing the rgrgstrCities
array with an integer derived from the state name. IMHO, it's better if
you can just use the state name directly, at least assuming that you
actually care about the state name at all.
If you want to be able to look up a given list of cities by state name and
don't want a hard-coded switch() statement or require an explicit mapping
from a state name to an integer as above or something like that, you could
use a Dictionary<>:
Dictionary<string, string[]> dictCitiesOfStates = new
Dictionary<string, string>();
dictCitiesOfStates.Add("Ohio", new string[] { "Cincinatti", "Akron",
"Cleveland" });
dictCitiesOfStates.Add("Iowa", new string[] { "Iowa City", "Des
Moines", "Dubuque" });
// etc.
There are other collection types as well. For example, instead of a
string[], you could use a List<string>, which would allow you to add or
remove states dynamically much more simply than would be possible with a
string[]. Or, you could use a Dictionary<> also for that collection,
which would allow you to associate some specific data with each city.
Frankly, how best to store this data has a lot more to do with how you're
going to use the data than it does with what the data actually is. So
far, all you've really told us is the latter, so vague answers like those
above are about as good as we can do, without more information.
Pete