0

I have a lstYourHand that has two cards in it, I loop through the listbox to get the values of both cards. I take the string value of the listbox item (strCardVal) and use a switch to give it an integer value (intCardVal). For some reason, when I run the code, the message Box at the end gives me the value 0 as a result, it does not register me giving it a value in the switch statement. My code is below:

Int32 intCardVal = 0;
String strCardVal;


Int32 intLoopCounter1;

for (intLoopCounter1 = 0; intLoopCounter1 == 1; intLoopCounter1++)
{
    strCardVal = lstYourHand.SelectedItem.ToString();

    switch (strCardVal)
    {
        case "2":
            intCardVal = 2;
            break;
        case "3":
            intCardVal = 3;
            break;
        case "4":
            intCardVal = 4;
            break;
        case "5":
            intCardVal = 5;
            break;
        case "6":
            intCardVal = 6;
            break;
        case "7":
            intCardVal = 7;
            break;
        case "8":
            intCardVal = 8;
            break;
        case "9":
            intCardVal = 9;
            break;
        case "10":
        case "J":
        case "Q":
        case "K":
            intCardVal = 10;
            break;
        case "A":
            intCardVal = 11;
            break;
    }
}

MessageBox.Show(intCardVal.ToString());
5
  • 1
    Have you debugged your code? Commented Apr 3, 2015 at 15:32
  • 1
    This problen could easily fixed using the debugger. Try to use it and check what is the value of the strCardVal before entering the switch Commented Apr 3, 2015 at 15:32
  • set a breakpoint and step thru it; learn how code executes; learn about the power of the debugger; Profit Commented Apr 3, 2015 at 15:32
  • I know that I am only getting the value of the last looped selected item but right now I am just trying to see if a value is passed, which is not the case because it gives me a value of 0. Commented Apr 3, 2015 at 15:36
  • As an aside remove the case statements for 1-10 and replace with this one statement default : intCardVal = int.Parse(strCardVal); break; Commented Apr 3, 2015 at 16:08

1 Answer 1

2

Have a look at this part of your code:

for (intLoopCounter1 = 0; intLoopCounter1 == 1; intLoopCounter1++)

In fact this loop body never will be executed because of loop execution condition intLoopCounter1 == 1 (which is false at the very first iteration since intLoopCounter1 == 0 in the beginning) - so your intCardVal will not be modified.

I think you've kept in mind intLoopCounter1 <= 1 here.

Also note (as it was mentioned in comments) - this kind of errors is pretty easy can be found by using debugger.

Sign up to request clarification or add additional context in comments.

2 Comments

if I change my loop to for (intLoopCounter1 = 0; intLoopCounter1 <= 1; intLoopCounter1++), I then get a Null reference error here strCardVal = lstYourHand.SelectedItem.ToString();
@Frank this is another problem not related to the loop. The reason of it: lstYourHand.SelectedItem is null (probably nothing is selected in the list) at the moment of your code execution and you can't call method of null object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.