12 Replies - 897 Views - Last Post: 19 November 2010 - 06:32 AM Rate Topic: -----

#1 Ringlis  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 38
  • Joined: 29-October 10

Multiple IF statements

Posted 18 November 2010 - 03:32 PM

I've a problem. I've 5 textboxes which must be filled out or an error message will display. The textbox 'txtWeight' must have a value over 1 or also display an error message also. 'txtWeight' is a FLOAT. See below:

{
   if (txtWeight.Text < 1)
   {
     label6.Text = ("please enter valid weight");
   }            
   {
   if (string.IsNullOrEmpty(txtMake.Text.Trim()) || string.IsNullOrEmpty(txtModel.Text.Trim()) || string.IsNullOrEmpty(txtColour.Text.Trim()) || string.IsNullOrEmpty(txtReg.Text.Trim()) || string.IsNullOrEmpty(txtWeight. Text.Trim()))
  {
    MessageBox.Show("All values are required");
    return;
    }
   }
  }  


I get an error message here saying "Operator '<' cannot be applied to operands of type 'string' and 'int' Might be because I have x2 IF statements....?

Can someone rewrite this so it displays an error message if the weight is <1 AND an error message if the textboxs are blank?

Thanks if you can.

This post has been edited by modi123_1: 18 November 2010 - 03:53 PM
Reason for edit:: fixed code tag


Is This A Good Question/Topic? 0
  • +

Replies To: Multiple IF statements

#2 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Multiple IF statements

Posted 18 November 2010 - 03:38 PM

if (Convert.ToInt32(txtWeight.Text) < 1)
            {
                label6.Text = "Please enter a valid weight.";
            }
            else if (String.IsNullOrEmpty(txtMake.Text.Trim()) || String.IsNullOrEmpty(txtModel.Text.Trim()) || String.IsNullOrEmpty(txtColour.text.Trim() String.IsNullOrEmpty(txtReg.Text.Trim()) || String.IsNullOrEmpty(txtWeight.Text.Trim()))
            {
                MessageBox.Show("All values are required.");
            }


You have to convert the txtWeight to a integer BEFORE comparing. Better to use TryParse though
Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4490
  • View blog
  • Posts: 7,822
  • Joined: 08-June 10

Re: Multiple IF statements

Posted 18 November 2010 - 03:41 PM

We're not going to rewrite it for you. That's your work. But we can help you. Well, I guess someone did.

txtWeight.Text < 1


This makes no sense. Ints are not strings, and strings are not ints. That's like saying is "Banana" < 5.

You have to convert your strings to ints. You can try the Convert.ToInt32 method, or the int.Parse or int.TryParse method.

Also, :code:

This post has been edited by insertAlias: 18 November 2010 - 03:42 PM

Was This Post Helpful? 0
  • +
  • -

#4 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Multiple IF statements

Posted 18 November 2010 - 03:51 PM

View PostinsertAlias, on 18 November 2010 - 05:41 PM, said:

We're not going to rewrite it for you. That's your work. But we can help you. Well, I guess someone did.

txtWeight.Text < 1


This makes no sense. Ints are not strings, and strings are not ints. That's like saying is "Banana" < 5.

You have to convert your strings to ints. You can try the Convert.ToInt32 method, or the int.Parse or int.TryParse method.

Also, :code:


If you call rewriting adding an else statement that I guess I did :lol:
Was This Post Helpful? 0
  • +
  • -

#5 Ringlis  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 38
  • Joined: 29-October 10

Re: Multiple IF statements

Posted 18 November 2010 - 03:55 PM

I tried that but got loads of errors, so that didnt work :(
Was This Post Helpful? 0
  • +
  • -

#6 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Multiple IF statements

Posted 18 November 2010 - 04:01 PM

Well let me read your mind using my psychic abilities and see what errors you're getting.

*initiating mind-sync*
Was This Post Helpful? 0
  • +
  • -

#7 karabasf  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 202
  • View blog
  • Posts: 417
  • Joined: 29-August 10

Re: Multiple IF statements

Posted 18 November 2010 - 04:06 PM

It would be helpful if you give us the errors you get.

Is it a compile error? (which I doubt), is it an error during runtime? An error caused, because the user enters a string or a double instead of an integer?

And so on...
Was This Post Helpful? 0
  • +
  • -

#8 Ringlis  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 38
  • Joined: 29-October 10

Re: Multiple IF statements

Posted 19 November 2010 - 02:53 AM

if (Convert.ToInt32(txtWeight.Text) < 1)
            {
                label6.Text = "Please enter a valid weight.";
            }
            else if (String.IsNullOrEmpty(txtMake.Text.Trim()) || String.IsNullOrEmpty(txtModel.Text.Trim()) || String.IsNullOrEmpty(txtColour.text.Trim() String.IsNullOrEmpty(txtReg.Text.Trim()) || String.IsNullOrEmpty(txtWeight.Text.Trim()))
            {
                MessageBox.Show("All values are required.");
            }


This produes several errors including Invalid token error, method must have a return type error & identifier expected errors.
Was This Post Helpful? 0
  • +
  • -

#9 mavarazo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 182
  • Joined: 25-October 10

Re: Multiple IF statements

Posted 19 November 2010 - 04:27 AM

Your error description is very poor :).
Perhaps this will fix your problem:

String.IsNullOrEmpty(txtColour.text.Trim()) || String.IsNullOrEmpty(txtReg.Text.Trim())

This post has been edited by mavarazo: 19 November 2010 - 04:27 AM

Was This Post Helpful? 0
  • +
  • -

#10 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Multiple IF statements

Posted 19 November 2010 - 05:09 AM

You need another else. IF > ELSE IF > ELSE.
Was This Post Helpful? 0
  • +
  • -

#11 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

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

Re: Multiple IF statements

Posted 19 November 2010 - 05:25 AM

Truthfully, you would put the check if it's blank BEFORE the check of the weight, because if the weight is blank, it will throw an exception because you are trying to convert a blank string to an integer.

Also, using TryParse will handle then entering a letter instead of a number.

if (String.IsNullOrEmpty(txtMake.Text.Trim()) || 
    String.IsNullOrEmpty(txtModel.Text.Trim()) || 
    String.IsNullOrEmpty(txtColour.Text.Trim()) || 
    String.IsNullOrEmpty(txtReg.Text.Trim()) || 
    String.IsNullOrEmpty(txtWeight.Text.Trim()))
{
      MessageBox.Show("All values are required.");
      return;
}

int weight = 0;

if (!int.TryParse(txtWeight.Text, out weight))
{
      label6.Text = "Please enter a valid weight.";
      return;
}



This code would also fix your other build errors referring to missing parentheses
Was This Post Helpful? 2
  • +
  • -

#12 karabasf  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 202
  • View blog
  • Posts: 417
  • Joined: 29-August 10

Re: Multiple IF statements

Posted 19 November 2010 - 05:32 AM

Furthermore, I would recommend another 'programflow' than your solution.

Instead of first parsing your txtWeight in an integer, I would first check whether your string is equal to null or an empty space.

Thus, your program should look like this:
if (String.IsNullOrEmpty(txtMake.Text.Trim()) || String.IsNullOrEmpty(txtModel.Text.Trim()) || String.IsNullOrEmpty(txtColour.text.Trim())|| String.IsNullOrEmpty(txtReg.Text.Trim()) || String.IsNullOrEmpty(txtWeight.Text.Trim()))
{
 MessageBox.Show("All values are required.");
}
else if (Convert.ToInt32(txtWeight.Text) < 1)
{
 label6.Text = "Please enter a valid weight.";
}
else
{
//Do something or nothing here. Just taking the tip of Sergio Tapia in the solution ^^/>
}



Though, this is just my point of view.

Edit:
ratz, someone was faster ><

This post has been edited by karabasf: 19 November 2010 - 05:34 AM

Was This Post Helpful? 0
  • +
  • -

#13 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5835
  • View blog
  • Posts: 12,690
  • Joined: 16-October 07

Re: Multiple IF statements

Posted 19 November 2010 - 06:32 AM

Your syntax is buggered. Let's make it more readable:
if (
	String.IsNullOrEmpty(txtMake.Text.Trim()) 
	|| String.IsNullOrEmpty(txtModel.Text.Trim()) 
	|| String.IsNullOrEmpty(txtColour.text.Trim() 
	String.IsNullOrEmpty(txtReg.Text.Trim()) 
	|| String.IsNullOrEmpty(txtWeight.Text.Trim())
) {



I see one missing || and a dropped paren.

Also, Convert.ToInt32 can throw an error. Try something that doesn't:
int weight;
if (!int.TryParse(txtWeight.Text, out weight)) {
	label6.Text = "Weight must be an integer.";
} else if (weight < 1) {
	label6.Text = "Please enter a valid weight.";
} else if ( String.IsNullOrEmpty(txtMake.Text.Trim()) 
		|| String.IsNullOrEmpty(txtModel.Text.Trim()) 
		|| String.IsNullOrEmpty(txtColour.text.Trim()) 
		|| String.IsNullOrEmpty(txtReg.Text.Trim())
		|| String.IsNullOrEmpty(txtWeight.Text.Trim()) ) {
	MessageBox.Show("All values are required.");
}


Still fugly. It it were me, I'd use a few methods:
bool IsEmpty(TextBox ctl) { return String.IsNullOrEmpty(ctl.Text.Trim()); }

bool HasAllValues() {
	if (IsEmpty(txtMake)) { return false; }
	if (IsEmpty(txtColour)) { return false; }
	if (IsEmpty(txtReg)) { return false; }
	if (IsEmpty(txtWeight)) { return false; }
	return true;
}

bool ValidatedEntries() {
	int weight;
	if (!HasAllValues()) {
		MessageBox.Show("All values are required.");
	} else if (!int.TryParse(txtWeight.Text, out weight)) {
		label6.Text = "Weight must be an integer.";
	} else if (weight < 1) {
		label6.Text = "Please enter a valid weight.";
	} else {
		return true;
	}
	return false;
}


Was This Post Helpful? 2
  • +
  • -

Page 1 of 1