A little background, this began in the question: Custom data type (Structures) vs arrays.
I have managed to create struct State, and the List<State> to go along with it. Originally this was a multidimensional array, the purpose of which was for the 0-index to be an item in a combobox, and idexes 1-3 to then be displayed in 3 text boxes. My question now is, how do I duplicate this functionality by using the struct/List combination. The struct contains the same 4 fields as the original Array did, but I’m having problems understanding how to specify which field goes to which box. From what I’ve read elsewhere it should simply be a matter of using struct.field format, but it does’t seem to be working. As usual it’s probably something simple I’m missing, but I can’t seem to wrap my head around it.
On a side note, this list is meant to be completely immutable, none of this information is going to change/be changed (if it does, it will be at the code level and I’ll redo it myself, but user entry shouldn’t ever affect it).
Any help will be greatly appreciated.
Struct:
struct State
{
private String StateID;
private String TimeZone;
private String CureType;
private int CureTimeframe;
public State(string stateID, string timeZone, string cureType, int cureTimeframe)
{
StateID = stateID;
TimeZone = timeZone;
CureType = cureType;
CureTimeframe = cureTimeframe;
}
public override string ToString()
{
string data = String.Format("{0} {1} {2} {3}", StateID, TimeZone, CureType, CureTimeframe);
return data;
}
}
List<> initializer:
class StatesAndTerritories
{
public List<State> stateData = new List<State>();
public StatesAndTerritories ()
{
stateData.Add(new State("AL", "CST", "None", 0));
stateData.Add(new State("AK", "AST", "COD", 10));
stateData.Add(new State("AR", "CST", "COD", 10));
stateData.Add(new State("AZ", "MST", "COD", 10));
stateData.Add(new State("CA", "PST", "COD/Mandatory", 10));
stateData.Add(new State("CO", "MST", "Mandatory", 20));
stateData.Add(new State("CT", "EST", "Mandatory", 10));
stateData.Add(new State("DC", "EST", "Mandatory", 10));
stateData.Add(new State("DE", "EST", "None", 0));
stateData.Add(new State("FL", "EST", "COD", 10));
stateData.Add(new State("GA", "EST", "COD", 10));
stateData.Add(new State("HI", "HST", "None", 0));
stateData.Add(new State("IA", "CST", "Mandatory", 20));
stateData.Add(new State("ID", "MST", "None", 0));
stateData.Add(new State("IL", "CST", "COD", 10));
stateData.Add(new State("IN", "EST", "None", 0));
stateData.Add(new State("KS", "CST", "Mandatory", 20));
stateData.Add(new State("KY", "EST", "None", 0));
stateData.Add(new State("LA", "CST", "Replevin", 0));
stateData.Add(new State("MA", "EST", "Mandatory", 21));
stateData.Add(new State("MD", "EST", "Mandatory", 10));
stateData.Add(new State("ME", "EST", "Mandatory", 19));
stateData.Add(new State("MI", "EST", "COD/Mandatory", 30));
stateData.Add(new State("MN", "CST", "COD", 10));
stateData.Add(new State("MO", "CST", "Mandatory", 20));
stateData.Add(new State("MS", "CST", "None", 0));
stateData.Add(new State("MT", "MST", "COD", 10));
stateData.Add(new State("NC", "EST", "None", 0));
stateData.Add(new State("ND", "CST", "None", 0));
stateData.Add(new State("NE", "CST", "Mandatory", 20));
stateData.Add(new State("NH", "EST", "None", 0));
stateData.Add(new State("NJ", "EST", "COD", 10));
stateData.Add(new State("NM", "MST", "COD", 10));
stateData.Add(new State("NV", "PST", "COD", 10));
stateData.Add(new State("NY", "EST", "None", 0));
stateData.Add(new State("OH", "EST", "COD", 10));
stateData.Add(new State("OK", "CST", "COD", 10));
stateData.Add(new State("OR", "PST", "COD", 10));
stateData.Add(new State("PA", "EST", "None", 0));
stateData.Add(new State("RI", "EST", "Mandatory", 24));
stateData.Add(new State("SC", "EST", "Mandatory", 20));
stateData.Add(new State("SD", "CST", "COD", 10));
stateData.Add(new State("TN", "CST", "COD", 10));
stateData.Add(new State("TX", "CST", "Mandatory",10));
stateData.Add(new State("UT", "MST", "None", 0));
stateData.Add(new State("VA", "EST", "None", 0));
stateData.Add(new State("VT", "EST", "None", 0));
stateData.Add(new State("WA", "PST", "COD", 10));
stateData.Add(new State("WI", "CST", "Mandatory", 15));
stateData.Add(new State("WV", "EST", "Mandatory", 10));
stateData.Add(new State("WY", "MST", "None", 0));
}
public IEnumerable<State> GetStates (){
return stateData;
}
public IEnumerable<State> GetStatesInTimeZone(string timezone)
{
return stateData;
}
public IEnumerable<State> GetStatesCureType(string curetype){
return stateData;
}
public IEnumerable<State> GetStatesCureTimeframe(int curetimeframe){
return stateData;
}
}
Combobox Population:
public void PopulateStateInfo()
{
StatesAndTerritories states = new StatesAndTerritories();
foreach (State state in states.stateData)
{
//string s = stateData[i, 0];
stateInfoBox.Items.Add(states.stateData);
}
}
ComboBox Selection:
private void stateInfoBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
StatesAndTerritories states = new StatesAndTerritories();
try
{
stateTimeZone.Text = states.stateData.ToString();
stateCureType.Text = states.stateData.ToString();
stateCureTimeframe.Text = states.stateData.ToString();
if (stateCureTimeframe.Text != 0.ToString())
{
stateCureAtDPD.Text = ((55 - int.Parse(stateCureTimeframe.Text)).ToString());
}
}
catch (IndexOutOfRangeException)
{
stateTimeZone.Text = "";
stateCureType.Text = "";
stateCureTimeframe.Text = "";
stateCureAtDPD.Text = "";
}
}
Firstly, you can not reach the content of the struct,
These are private hence cannot be accessed from outside of the struct.
You need to define public readonly properties for them:
Next, I think you only want to display the StateID in the combobox. If so, you need to write the ToString() accordingly:
In
PopulateStateInfomethod you are not adding each and every item in the list but adding the whole collection which is unusual! A proper implementation would be like:Lastly, a proper way of handling the SelectedIndexChanged event would be like:
One last point I would like to mension is, please do not make the stateData element of the StatesAndTerritories class public. You would benefit from it, if it is mutable only from inside of the class.