-2
ArrayList stateList, gdpList, rankList;
public void Page_Load(object sender, EventArgs e)
{

    stateList = new ArrayList(); //The state array list
    stateList.Add("Delaware"); 
    stateList.Add("Alaska");
    stateList.Add("North Dakota");
    stateList.Add("Connecticut");
    stateList.Add("Wyoming");
    stateList.Add("Massachusetts");
    stateList.Add("New York");
    stateList.Add("New Jersey");
    stateList.Add("Oregon");
    stateList.Add("Washington");
    stateList.Add("Virginia");
    stateList.Add("Minnesota");

    rankList = new ArrayList(); //The ranking array list for each state
    rankList.Add(1);
    rankList.Add(2);
    rankList.Add(3);
    rankList.Add(4);
    rankList.Add(5);
    rankList.Add(6);
    rankList.Add(7);
    rankList.Add(8);
    rankList.Add(9);
    rankList.Add(10);
    rankList.Add(11);
    rankList.Add(12);

    gdpList = new ArrayList(); //The GDP array list for each state
    gdpList.Add("61,183");
    gdpList.Add("61,156");
    gdpList.Add("55,250");
    gdpList.Add("54,925");
    gdpList.Add("54,305");
    gdpList.Add("53,221");
    gdpList.Add("53,067");
    gdpList.Add("49,430");
    gdpList.Add("48,069");
    gdpList.Add("47,146");
    gdpList.Add("47,127");
    gdpList.Add("47,028");

}

void GDP_Click(object sender, EventArgs e)
{
    string  state1 = State.Text;
    for (int i = 0; i < stateList.Count; i++ )
    {
        if (state1 == stateList[i])
        {
            Response.Write("The " + stateList[i] + " state GDP is " + gdpList[i] + " and the rank is " + rankList[i]);
        }
        else if (state1 == stateList[i])
        {
            Response.Write("The state that you entered is not a part of our state list");
        }
    }
}

So I have these three arrays. One with twelve states, one with ranking of 1-12, and their GDP in the other array. There is a text box and a button. If you enter say.. Delaware in the text box, then click the button it will generate a label that says: the state, their rank, and their GDP. However, regardless of what state I put in it will always return "the state is not listed" even if it matches the array. I'm assuming that the loop keeps running. So I tried, adding break after each response, that didn't work. Then I tried, adding return after each response, that didn't work either. I tried boolean but couldn't exactly figure it out.

4 Answers4

0

Your conditions within your loop check for the same thing. And, if the second condition executed when there was no match, you'd still be writing to the response for every item within your stateList collection.

Instead, Try:

void GDP_Click(object sender, EventArgs e)
{
    string  state1 = State.Text;
    bool foundMatch = false;

    for (int i = 0; i < stateList.Count; i++ )
    {
        if (state1 == stateList[i])
        {
            Response.Write("The " + stateList[i] + " state GDP is " + gdpList[i] + " and the rank is " + rankList[i]);
            foundMatch = true;
            break;
        }
    }

    if (!foundMatch)
    {
        Response.Write("The state that you entered is not a part of our state list");
    }
}
Ryan Nigro
  • 4,389
  • 2
  • 17
  • 23
  • Hey, thanks for the response! I tried the code you posted, and every time I enter say "Delaware" again, it still gives me that "the state you entered is not a part" – user3869900 Nov 07 '14 at 22:27
0

Your condition for the else is exactly the same as the if condition.

I think you refactor your code like

void GDP_Click(object sender, EventArgs e)
{
    string  state1 = State.Text;
    for (int i = 0; i < stateList.Count; i++ )
    {
        if (state1 == stateList[i])
        {
            Response.Write("The " + stateList[i] + 
               " state GDP is " + gdpList[i] + " and the rank is " + rankList[i]);
            return;
        }
    }

    Response.Write("The state that you entered is not a part of our state list");
}
Ric .Net
  • 5,540
  • 1
  • 20
  • 39
0

Here is a much easier way of doing what you are trying to do notice the different way that I have Initialized the List and List now you can use this as a learning tool as a more efficient way do doing what you have coded.. if you want to break out of the code when it's not in the list then you will need to add the break; after this line

Response.Write(string.Format("The {0} state GDP is {1} and the rank is {2} " ,stateList[i], gdpList[i], rankList[i])); if you are expected to return a boolean I would either alter the method or I would create a Property that you can access outside of the method you're currently in

var stateList = new List<string>
{
 "Delaware",
 "Alaska",
 "North Dakota",
 "Connecticut",
 "Wyoming",
 "Massachusetts",
 "New York",
 "New Jersey",
 "Oregon",
 "Washington",
 "Virginia",
 "Minnesota"
};

var rankList = new List<int>
{
    1,2,3,4,5,6,7,8,9,10,11,12
};

var gdpList = new List<string>
{
    "61,183",
    "61,156",
    "55,250",
    "54,925",
    "54,305",
    "53,221",
    "53,067",
    "49,430",
    "48,069",
    "47,146",
    "47,127",
    "47,028"
};
var bFound = false;
string state1 = State.Text;
for (int i = 0; i < stateList.Count; i++)
{
    if (state1.Contains(stateList[i]))
    {
        Response.Write(string.Format("The {0} state GDP is {1} and the rank is {2} " ,stateList[i], gdpList[i], rankList[i]));
     bFound = true;
     break;
    }
}
if (!bFound)
{
    Response.Write("The state that you entered is not a part of our state list");
    return;
}
MethodMan
  • 18,625
  • 6
  • 34
  • 52
0

Change the code inside GDP_Click method into:

string state1 = State.Text;
for (int i = 0; i < stateList.Count; i++)
{
    if (state1.Equals(stateList[i]))
    {
        Response.Write("The " + stateList[i] + " state GDP is " + gdpList[i] + " and the rank is " + rankList[i]);
        return;
    }
}
Response.Write("The state that you entered is not a part of our state list");

EXPLANATION:

Here's the important part: state1.Equals(stateList[i])

Why == doesn't work?

ArrayList is a non-generic collection that returning an boxed Object type when you retrieve element(s) from it. So when performing state1 == stateList[i] that means you are comparing a String type against Object type, not String against String anymore, so the comparison will be performed as Object reference comparison (whether two operands refer to the same object)

Keep in mind that == operator and Equals methods are different. If you sure that two instance that you will compare are the same specific type, that it's safe to use == operator. Otherwise, if one of the instance is boxed into Object type, than I suggest you to use Equals method instead.

*) futher reading:

Consider this code:

object a = 123;
object b = 123;

a == b will always return false, even when they contain the same integer. Because comparing two Object type will do a reference comparison, instead of the content of object itself.

but a.Equals(b) or b.Equals(a) will able to return true. Or even better to use Equals(a, b) (shortcut from Object.Equals(a, b)), the latest form will avoid the NullReferenceException if a or b is null.

Thariq Nugrohotomo
  • 733
  • 1
  • 6
  • 12