5 Replies - 463 Views - Last Post: 09 June 2009 - 08:52 AM Rate Topic: -----

#1 tutumlu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-June 09

I have a problem in If

Posted 09 June 2009 - 08:02 AM

I am 15 years old. I am from Turkey. I can speak very little English. I have just started programming. I am very new into this subject.

I have a problem in If


			string ad;
			int sifre;
			ad = textBox1.Text;
			sifre = Convert.ToInt32(textBox2.Text);
			if(sifre==123&& ad=="Erol")
			{
				label4.Text = "Başarılı.."; 
			}
			else
			{ 
				label4.Text = "Başarısız."; 
			}




Help me please .

Is This A Good Question/Topic? 0
  • +

Replies To: I have a problem in If

#2 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1002
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: I have a problem in If

Posted 09 June 2009 - 08:05 AM

Hmmmm.... What is the problem?
Was This Post Helpful? 0
  • +
  • -

#3 tutumlu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 05-June 09

Re: I have a problem in If

Posted 09 June 2009 - 08:27 AM

Posted Image

Then

I have error.

Posted Image


I hope , have understood ..
Was This Post Helpful? 0
  • +
  • -

#4 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1002
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: I have a problem in If

Posted 09 June 2009 - 08:32 AM

"Erol" isn't a number, you would have to enter "123" into the text box to have your if() statement work.

Convert.ToInt32() won't work with something like "one", "two", etc. It needs to actually be a number.

You could also use Int32.Parse(textBox2.Text), I don't like using the Convert class for some reason :)
Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2240
  • View blog
  • Posts: 9,411
  • Joined: 29-May 08

Re: I have a problem in If

Posted 09 June 2009 - 08:51 AM

tryparse would be better.
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Re: I have a problem in If

Posted 09 June 2009 - 08:52 AM

change your code to...

string ad;
int sifre;
ad = textBox1.Text;

if (!int.TryParse(textBox2.Text, out sifre))
{
     MessageBox.Show("Enter a valid number");
     return;
}

if(sifre==123&& ad=="Erol")
{
      label4.Text = "Başarılı.."; 
}
else
{ 
      label4.Text = "Başarısız."; 
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1