blah blah blah is here! blah blah » Close

up0down
link

I am a newbie to C# and I am getting error messages because of a simple if statement. I am trying out my skills with a simple alarm clock, and I have 3 nested If Statments and it is giving me an error. here is the code I am using. It is making sure that three variables, hour, minute, second are all valid (between 1-12 for hour, 1-59 for minute). Here is the code I used:

private void SetButton_Click(object sender, EventArgs e)
{
hour = Convert.ToInt32(textBox1.Text);
hour = int.Parse(textBox1.Text);

if (hour <= 12 && hour < 0){
minute = Convert.ToInt32(textBox2.Text);
minute = int.Parse(textBox2.Text);
if(minute <= 59){
second = Convert.ToInt32(textBox3.Text);
second = int.Parse(textBox3.Text);
if(second >= 0 && <= 59){
goto timer;

else if(second >= 60 || second < 0){
textBox3.Text = "Between 0 and 59";
}
}

else if(minute >= 60 || minute < 0) {
textBox2.Text = "Between 0 and 59";
}
}

else if(hour < 1 || hour > 12) {
textBox1.Text = "between 1 and 12";
}




}
}

I have included a link to the ZIP file of my project so you can debug it and run it in context:
Click Here

theodoric
107

I made the changes you asked for but I still am getting errors. The updated zip file is here (https://docs.google.com/leaf?id=0B89GBk8q-FkkNzIyYjY2OTQtNWU2MC00ZWFjLTk5NzctZjgxYzYzOWZiMWI3&hl=en)

theodoric
107

If you fix it I would like you to send the entire code of the program.cs file or zip it for me.

last answered one year ago

2 answers

up0down
link

Try the following:

private void SetButton_Click(object sender, EventArgs e)
{
hour = int.Parse(textBox1.Text);

if (hour >= 1 && hour <= 12)
{
minute = int.Parse(textBox2.Text);
if (minute >= 0 && minute <= 59)
{
second = int.Parse(textBox3.Text);
if (second >= 0 && second <= 59)
{
// code to set timer
}
else
{
textBox3.Text = "Between 0 and 59";
}
}
else
{
textBox2.Text = "Between 0 and 59";
}
}
else
{
textBox1.Text = "between 1 and 12";
}
}

However, this code will still throw an exception if any of the textboxes don't contain an integer.

You can deal with this by replacing this line:
hour = int.Parse(textBox1.Text);

with this:
if (!int.TryParse(textBox1.Text, out hour))
{
textBox1.Text = "Between 1 and 12";
return;
}

and with similar changes to the code which parses out minutes and seconds.

The int.TryParse method returns false if it's first argument (a string) is not an integer and sets the second argument (preceded by 'out') to zero.

Otherwise, it returns true and sets the second argument to the integer which is arrived at when the first argument is parsed.

theodoric
107

I made the changes you asked for but I still am getting errors. The updated zip file is here (https://docs.google.com/leaf?id=0B89GBk8q-FkkNzIyYjY2OTQtNWU2MC00ZWFjLTk5NzctZjgxYzYzOWZiMWI3&hl=en)

theodoric
107

If you fix it I would like you to send the entire code of the program.cs file or zip it for me.

up0down
link

All that appears to be wrong is that you have some brackets in the wrong place.

The Form class should look like this:

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
int hour;
int minute;
int second;

public Form1()
{
InitializeComponent();
}

private void SetButton_Click(object sender, EventArgs e)
{
hour = int.Parse(textBox1.Text);

if (hour >= 1 && hour <= 12)
{
minute = int.Parse(textBox2.Text);
if (minute >= 0 && minute <= 59)
{
second = int.Parse(textBox3.Text);
if (second >= 0 && second <= 59)
{
// code to set timer
}
else
{
textBox3.Text = "Between 0 and 59";
}
}
else
{
textBox2.Text = "Between 0 and 59";
}
}
else
{
textBox1.Text = "between 1 and 12";
}
}

private void Form1_Load(object sender, EventArgs e)
{

}
}
}

Feedback