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());
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.
Related
I have the following Code down here, but when I try to add a child to parent it causes an error when parentID is bigger than 4.
public void LoadNodes()
{
ConnectionShorten("TreeViewTable");
int H = MyNodes.Tables["TreeViewTable"].Rows.Count;
for (int i = 0; i < H; i++)
{
int PID = MyNodes.Tables["TreeViewTable"].Rows[i].Field<int>("ParentID");
string Name = MyNodes.Tables["TreeViewTable"].Rows[i].Field<string>("RootName");
int Level = MyNodes.Tables["TreeViewTable"].Rows[i].Field<int>("Level");
int UID = MyNodes.Tables["TreeViewTable"].Rows[i].Field<int>("UID");
switch (Level)
{
case 0:
treeView1.Nodes.Add(Name.ToString());
break;
case 1:
treeView1.Nodes[0].Nodes.Add(Name.ToString());
break;
case 2:
switch (PID)
{
case 1:
treeView1.Nodes[0].Nodes[0].Nodes.Add(Name.ToString());
break;
case 2:
treeView1.Nodes[0].Nodes[1].Nodes.Add(Name.ToString());
break;
case 3:
treeView1.Nodes[0].Nodes[2].Nodes.Add(Name.ToString());
break;
case 4:
treeView1.Nodes[0].Nodes[3].Nodes.Add(Name.ToString());
break;
case 5:
treeView1.Nodes[0].Nodes[4].Nodes.Add(Name.ToString());
break;
case 6:
treeView1.Nodes[0].Nodes[5].Nodes.Add(Name.ToString());
break;
case 7:
treeView1.Nodes[0].Nodes[6].Nodes.Add(Name.ToString());
break;
case 8:
treeView1.Nodes[0].Nodes[7].Nodes.Add(Name.ToString());
break;
case 9:
treeView1.Nodes[0].Nodes[8].Nodes.Add(Name.ToString());
break;
case 10:
treeView1.Nodes[0].Nodes[9].Nodes.Add(Name.ToString());
break;
case 11:
treeView1.Nodes[0].Nodes[10].Nodes.Add(Name.ToString());
break;
default:
break;
}
break;
case 3:
break;
default:
break;
}
}
}
It does load 1 main node and 10 parents, then when it comes to child it says a negative index, but I'm sure the parent does exist!
Also how can I make more levels with no difficulty like this, because I think it's very difficult to make levels > 3.
You can close it, I just found that problem and solved it.
It was because there is a jump on ID on the DB. So I try to add child before I even add the parent. my program is read by ID so thats it ... sorry for this.
I'm creating n 8th ball and I'm trying to randomly generate one of the eight phrases into a textbox once a button is tapped and can't get my head around how to get my phrases in a textbox when a button is tapped.
private void Button1_Tapped(object sender, TappedRoutedEventArgs e)
{
Random num = new Random();
int a = num.Next(9);
switch (a)
{
case 0:
Console.;
break;
case 1:
Console.WriteLine.TextBox("Yes");
break;
case 2:
Console.WriteLine.TextBox("No");
break;
case 3:
Console.WriteLine.TextBox("Maybe");
break;
case 4:
Console.WriteLine.TextBox("You could say that");
break;
case 5:
Console.WriteLine.TextBox("Most certain");
break;
case 6:
Console.WriteLine.TextBox("Dont even try");
break;
case 7:
Console.WriteLine.TextBox("Full steam ahead");
break;
}
}
Console.WriteLine() normally writes to the Console output in a Console App. So it seems you are mixing something up...
Your textbox in the wpf app needs to have a variable name e.g. theTextBox and then you assign the string to the .Text property.
private void Button1_Tapped(object sender, TappedRoutedEventArgs e)
{
Random num = new Random();
int a = num.Next(9);
switch (a)
{
case 0:
theTextBox.Text = "";
break;
case 1:
theTextBox.Text = "Yes";
break;
case 2:
theTextBox.Text = "No";
break;
case 3:
theTextBox.Text = "Maybe";
break;
case 4:
theTextBox.Text = "You could say that";
break;
case 5:
theTextBox.Text = "Most certain";
break;
case 6:
theTextBox.Text = "Dont even try";
break;
case 7:
theTextBox.Text = "Full steam ahead";
break;
}
}
This question already has answers here:
Unreachable code detected in case statement
(14 answers)
Closed 7 years ago.
I'm trying to convert string to int. I think I have it right, but what else do I need to do? Under "Break" I get a green line saying "unreachable code detected". Also, what do I tell it to return? I put a random number after return, because I have blanked on what I should ask it to return.
namespace BattleShip.UI
{
class TranslateNumberToLetter
{
public int NumberToLetter(string Letter)
{
switch (Letter)
{
case "A":return 1;
break;
case "B": return 2;
break;
case "C": return 3;
break;
case "D": return 4;
break;
case "E": return 5;
break;
case "F": return 6;
break;
case "G": return 7;
break;
case "H": return 8;
break;
case "I": return 9;
break;
case "J": return 10;
break;
default: return -100;
}
}
}
}
public int NumberToLetter(string Letter)
{
if ("ABCEDFGHIJ".Contains(Letter))
return "ABCEDFGHIJ".IndexOf(Letter) + 1;
return -100;
}
i have problems when adding mullti values into same case:
this is my c# code
string input = combobox1.selectedvalue.ToString();
switch(input)
{
case "one";
return 1;
break;
case "two";
return 2;
break;
case "three" , "four": // error here
return 34;
break;
default:
return 0;
}
need your help for
Just use separate labels:
string input = combobox1.selectedvalue.ToString();
switch(input)
{
case "one":
return 1;
break;
case "two":
return 2;
break;
case "three":
case "four":
return 34;
break;
default:
return 0;
}
See switch:
Each switch section contains one or more case labels followed by one or more statements
You can you the fall though, read this for more information
so it's look like this
switch(input)
{
case "one":
return 1;
break;
case "two":
return 2;
break;
case "three":
case "four":
return 34;
break;
default:
return 0;
}
Right syntax is
case "three":
case "four":
return 34;
break;
instead
case "three" , "four":
return 34;
break;
From switch (C# Reference)
A switch statement includes one or more switch sections. Each switch
section contains one or more case labels followed by one or more
statements.
I am developing a console application, that should listen for digits from a numpad keyboard in both num lock states - on and off. The application is running on Raspberry Pi with Arch Linux and Mono. Since I did not found a way, that is compiling under Mono, to permanently turn numlock on, I am using the following method to convert num pad commands to digits:
private string ReadNumPadSymbol(ConsoleKeyInfo keyInfo)
{
char editedSymbol;
switch (keyInfo.Key)
{
case ConsoleKey.Insert:
editedSymbol = '0';
break;
case ConsoleKey.End:
editedSymbol = '1';
break;
case ConsoleKey.DownArrow:
editedSymbol = '2';
break;
case ConsoleKey.PageDown:
editedSymbol = '3';
break;
case ConsoleKey.LeftArrow:
editedSymbol = '4';
break;
case ConsoleKey.Clear:
editedSymbol = '5';
break;
case ConsoleKey.RightArrow:
editedSymbol = '6';
break;
case ConsoleKey.Home:
editedSymbol = '7';
break;
case ConsoleKey.UpArrow:
editedSymbol = '8';
break;
case ConsoleKey.PageUp:
editedSymbol = '9';
break;
default:
return String.Empty;
}
return editedSymbol.ToString();
}
It works as expected under Windows, but under Linux, the method returns empty string, when the "5" button is pressed. For some reason it does not enters the ConsoleKey.Clear case. Is there a fix for this?
Thanks!