Switch statement using text box within forms - c#

I am building a form that takes an input number from a text box and then will take the number that was input, and display the roman numeral equivalent in another text box.
My Form:
private void convertButton_Click(object sender, EventArgs e)
{
int numberInput;
switch (numberInput)
This is where I keep getting an error code. The "switch (numberInput)" is seen as and unassigned local variable. How do I assign it so that it will be able to access all of the case integers?
{
case 1:
outputTextBox.Text = "I";
break;
case 2:
outputTextBox.Text = "II";
break;
case 3:
outputTextBox.Text = "III";
break;
case 4:
outputTextBox.Text = "IV";
break;
case 5:
outputTextBox.Text = "V";
break;
case 6:
outputTextBox.Text = "VI";
break;
case 7:
outputTextBox.Text = "VII";
break;
case 8:
outputTextBox.Text = "VIII";
break;
case 9:
outputTextBox.Text = "IX";
break;
case 10:
outputTextBox.Text = "X";
break;
default:
MessageBox.Show("Please enter and number between 1 and 10. Thank you!");
break;
}

Cause your variable is not assigned yet int numberInput; and so the error. You said input is coming from a TextBox, in that case do like below assuming textbox1 is your TextBox control instance name
int numberInput = Convert.ToInt32(this.textbox1.Text.Trim());

Convert.ToInt32 may throw an exception if the parsing is unsuccessful. Another method is to Int.Parse:
int numberInput = int.Parse(textbox1.Text.Trim());
or better yet
int numberInput;
if(int.TryParse(textbox1.Text.Trim(), out numberInput))
{
switch (numberInput)
...
}

Related

Can't show all nodes of TreeView

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.

If-statement into the switch-method with boolean

Problem: How to write this if-codeblock into a switch case? I have only a problem with the boolean.
int Note = 0;
Console.WriteLine("What is your note?: ");
Note = Convert.ToInt32(Console.ReadLine());
if ((Note < 1) || (Note > 6))
{
Console.WriteLine("Your input is wrong!");
}
else
{
if (Note <= 4)
{
Console.WriteLine("Passed");
}
else
{
Console.WriteLine("Failed");
}
}
I tried to write this codeblock into a switch-method.
int Note = 0;
Console.WriteLine("What ist your note?: ");
Note = Convert.ToInt32(Console.ReadLine());
switch (Note)
{
case Note < 1 || Note > 6:
Console.WriteLine("Your input is wrong!");
break;
case Note <= 4:
Console.WriteLine("Passed");
break;
}
Error Message: I can't convert the string into int.
C#7+ supports range-based switching:
switch (Note)
{
case int n when n< 1 || Note > 6:
Console.WriteLine("Your input is wrong!");
break;
case int n when n <= 4:
Console.WriteLine("Passed");
break;
}
You can do it like that if you really need a switch
switch (Note)
{
case 1:
case 2:
case 3:
case 4:
Console.WriteLine("Passed");
break;
case 5:
case 6:
Console.WriteLine("Failed");
break;
default:
Console.WriteLine("Your input is wrong!");
break;
}
This is not where switch statements are for. You should keep using if.
I will proof that very easily, using a switch, as you asked:
switch (Note)
{
case 1:
case 2:
case 3:
case 4:
Console.WriteLine("Passed");
break;
case 5:
case 6:
Console.WriteLine("Failed");
break;
default:
Console.WriteLine("Your input is wrong!");
break;
}
This is just way too verbatim. You don't want to write down every value. if is more appropriate.

Write random text into Textbox

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;
}
}

C# Case Sensitivity in Switch-statement

I'm working a little with switch statements and want to know how to ignore the case sensitivity when it comes to input values.
Here is my code:
using System;
namespace SwitchStatements
{
class MainClass
{
public static void Main(string[] args)
{
Start:
Console.WriteLine("Please Input the Grade");
char grade = Convert.ToChar(Console.ReadLine());
switch (grade)
{
case 'A':
Console.WriteLine("Excellent Work!");
break;
case 'B':
Console.WriteLine("Very Good Effort! Just a couple of Errors =)");
break;
case 'C':
Console.WriteLine("You Passed. Push Yourself Next Time");
break;
case 'D':
Console.WriteLine("Better put in more effort next time. I know you can do better");
break;
default:
Console.WriteLine("Invalid Grade.");
break;
}
Console.ReadKey();
goto Start;
}
}
}
If I put 'a' in instead of 'A' it returns the default response.
Can I use perhaps a .Comparison of some sort? If so where would I put it?
You can use ConsoleKey as condition for switch, the code will be like the following.
var grade =Console.ReadKey().Key;
switch (grade)
{
case ConsoleKey.A:
Console.WriteLine("Excellent Work!");
break;
case ConsoleKey.B:
// Something here
break;
case ConsoleKey.C:
// Something here
break;
case ConsoleKey.D:
// Something here
break;
case ConsoleKey.E:
// Something here
break;
default:
// Something here
break;
}
So that you can avoid converting the input to uppercase/Lower case, and then it goes for another conversion To Char. Simply use ConsoleKey Enumeration inside the switch.
You can use ToUpper(); Like
Convert.ToChar(Console.ReadLine().ToUpper());
and to get saved from the error of getting more charaters with Console.ReadLine() you can use
char grd = Convert.ToChar(Console.ReadKey().KeyChar.ToString().ToUpper());
you can use like following also
char grade = Convert.ToChar(Console.ReadLine().ToUpperInvariant());
https://msdn.microsoft.com/en-us/library/system.string.toupperinvariant.aspx
Convert to uppercase before switch like below,
grade = Char.ToUpper(grade);
Change
char grade = Convert.ToChar(Console.ReadLine());
To
char grade = Convert.ToChar(Console.ReadLine().ToUpper());
https://msdn.microsoft.com/en-us/library/system.string.toupper(v=vs.110).aspx
Write Switch on grade.ToUpper() like this and don't change change it's value, may be you will need it after
char grade = Convert.ToChar(Console.ReadLine());
switch (grade.ToUpper())
{
case 'A':
Console.WriteLine("Excellent Work!");
break;
case 'B':
Console.WriteLine("Very Good Effort! Just a couple of Errors =)");
break;
case 'C':
Console.WriteLine("You Passed. Push Yourself Next Time");
break;
case 'D':
Console.WriteLine("Better put in more effort next time. I know you can do better");
break;
default:
Console.WriteLine("Invalid Grade.");
break;
}
You may fall from one case to another like this
public static void Main(string[] args)
{
Boolean validInputRead = false;
Char grade;
while(!validInputRead)
{
validInputRead = true;
Console.WriteLine("Please Input the Grade");
grade = Convert.ToChar(Console.Read());
switch (grade)
{
case 'A':
case 'a':
Console.WriteLine("Excellent Work!");
break;
case 'B':
case 'b':
Console.WriteLine("Very Good Effort! Just a couple of Errors =)");
break;
case 'C':
case 'c':
Console.WriteLine("You Passed. Push Yourself Next Time");
break;
case 'D':
case 'd':
Console.WriteLine("Better put in more effort next time. I know you can do better");
break;
default:
Console.WriteLine("Invalid Grade.");
validInputRead = false;
break;
}
Console.ReadKey();
}
}
EDIT
Changed from Console.ReadLine() to Console.Read() as suggested
Added while(!validInputRead) as requested
string letterGrade;
int grade = 0;
// This will hold the final letter grade
Console.Write("Input the grade :");
switch (grade)
{
case 1:
// 90-100 is an A
letterGrade = "A";
Console.WriteLine("grade b/n 90-100");
break;
case 2:
// 80-89 is a B
letterGrade = "B";
Console.WriteLine("grade b/n 80-89");
break;
case 3:
// 70-79 is a C
letterGrade = "C";
Console.WriteLine("grade b/n 70-79");
break;
case 4:
// 60-69 is a D
letterGrade = "D";
Console.WriteLine(" grade b/n 60-69 ");
break;
default:
// point whic is less than 59
Console.WriteLine("Invalid grade");
break;
}

image not displaying

I hope you can help me! I am trying to make a game similiar to candyland. I want the die to spin when the user clicks the button. A random number is chosen and based on that number, the dice displays the image for that number. That works! Then, I want our user to be able to move forward on our board- based on the spot that they're on, it adds whatever they spinned and the image on that spot becomes visible. When in debug mode, everything works perfectly but for some reason, the pawn never moves! Can you please tell me why. I am attaching my code below. Thank you so much!
protected void btnSpin_Click(object sender, EventArgs e)
{
Random randomNumber = new Random();
int x = randomNumber.Next(1, 6);
switch (x)
{
case 1:
//imgDie.ImageUrl = "~/Images/dice1.jpg";
Session["Die"] = "~/Images/dice1.jpg";
break;
case 2:
Session["Die"] = "~/Images/dice2.jpg";
break;
case 3:
Session["Die"] = "~/Images/dice3.jpg";
break;
case 4:
Session["Die"] = "~/Images/dice4.jpg";
break;
case 5:
Session["Die"] = "~/Images/dice5.jpg";
break;
case 6:
Session["Die"] = "~/Images/dice6.jpg";
break;
}
imgDie.ImageUrl = (string)Session["Die"];
place = place + x;
switch (place)
{
case 2:
img2.Visible = true;
img2.ImageUrl = (string)Session["Imagesrc"];
break;
case 3:
img3.Visible = true;
img3.ImageUrl = (string)Session["Imagesrc"];
break;
case 4:
img4.Visible = true;
img4.ImageUrl = (string)Session["Imagesrc"];
break;
case 5:
img5.Visible = true;
img5.ImageUrl = (string)Session["Imagesrc"];
break;
case 6:
img6.Visible = true;
img6.ImageUrl = (string)Session["Imagesrc"];
break;
case 7:
img7.Visible = true;
img7.ImageUrl = (string)Session["Imagesrc"];
break;
case 8:
img8.ImageUrl = (string)Session["Imagesrc"];
img8.Visible = true;
break;
my guess is your 'place' variable is a member field and it's being reinitialized with each page construction. chnge your place variable to be viewstate or session state like your other stuff.

Categories