The background of this program is simple enough: I want a user to be able to enter any letter (A, B, C, etc.) into a textbox, and with the click of a button, have the program return how many U.S. states begin with that letter (e.g. letter A entered and result is 4).
Here is my code as of now...
private void btnClick_Click(object sender, EventArgs e)
{
string[] States = new String[50] {"Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado",
"Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas",
"Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri",
"Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina",
"North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota",
"Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming"};
string myLetter = txtboxEnter.Text;
int result;
result = 0;
for (int i = 0; i <= States.Length - 1; i++)
{
if (States[i].Substring(0, 1) == myLetter)
{
result = result + i;
}
else
{
result = 0;
}
}
lblDisplay.Text = Convert.ToString(result);
}
As you see, I have the States declared in an array.
The issue I am having is with the for loop and the If statement inside it.
The value always returned is 0. I feel as though I need another line of code directly for the loop to total values. Am I correct?