7 Replies - 1728 Views - Last Post: 25 April 2010 - 02:00 PM Rate Topic: -----

#1 Crazydave1990   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 28-September 09

if statement.

Posted 13 December 2009 - 04:22 AM

Hi guys;

I've been looking into if statements in c#, and quite frankly, im at boiling point now.

how would I go about creating a if statement comparing two strings...

lets say we have two variables.

string usersname;
string password;

each of these variables are assigned to the value of a textbox

so how do I do this in C#

if username = "administrator" and password = "password" then
do this
else
do this
end if.

please and thanks for the help guys!

Is This A Good Question/Topic? 0
  • +

Replies To: if statement.

#2 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6820
  • View blog
  • Posts: 31,435
  • Joined: 10-May 07

Re: if statement.

Posted 13 December 2009 - 04:26 AM

Via this site

if(username=="administrator" && password=="password") {
  //do this
}
else {
  //do this
}


I'm sure that any number of snippets & tutorials here could have shown you the same.

Just remember that if(variable==value) checks to see if the value of the variable is equal to the value & if(variable=value) checks to see if the variable can be set to the value, which will always return true.
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: if statement.

Posted 13 December 2009 - 07:42 AM

One thing to remember, double equal signs (==) checks for equality. Single equal sign (=) is for assignment of value.

in an IF statement, you are wanting to check for equality, so you would use the double equal signs
Was This Post Helpful? 0
  • +
  • -

#4 Macjohn   User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 407
  • Joined: 10-April 09

Re: if statement.

Posted 25 April 2010 - 12:53 PM

Hey guys, I'm kinda using the same method but my problem is that I what the "if" statement to check the value of the label.
I mean, if the the label as something written n it or not.

if ( label1.Text == null) // or "")
label1.Text = textBox.Text;



but what i can't do is something like this

if ( label1.Text == true)
label1.Text = textBox.Text;
//or something
if ( label1.TextValue == True)
label2.Text = textBox.Text;




don't even know if that is possible
thanks in advanceed

This post has been edited by Macjohn: 25 April 2010 - 12:54 PM

Was This Post Helpful? 0
  • +
  • -

#5 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: if statement.

Posted 25 April 2010 - 01:22 PM

Best way to test if a string has something in it is to use the IsNullOrEmpty method:
if (String.IsNullOrEmpty(label1.Text)) {
    // It's null or empty
}


Not sure what you are trying to do with the true/True statements. True is a boolean, and you can't compare a string to a boolean. Explain more, please :)
Was This Post Helpful? 0
  • +
  • -

#6 Macjohn   User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 407
  • Joined: 10-April 09

Re: if statement.

Posted 25 April 2010 - 01:39 PM

I know that that doens't exist I was just trying to explain what i'm trying to do and i can't.

I'm gonna try to be more clear than before.

What I'm trying is to do the inverse of what you just did.

Thanks in advanced
Was This Post Helpful? 0
  • +
  • -

#7 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: if statement.

Posted 25 April 2010 - 01:53 PM

The inverse is easy, and can be done it two ways:
if (!String.IsNullOrEmpty(Label1.Text)) {
    // It's not null or empty


The other way (and the one I prefer, as I don't like to use not logic (it's easy to miss that little ! in there:
if (String.IsNullOrEmpty(Label1.Text) == False) {
    // It's not null or empty



Both these result in machine code that is identical, so there is no performance difference.
Was This Post Helpful? 1
  • +
  • -

#8 Macjohn   User is offline

  • D.I.C Regular
  • member icon

Reputation: 80
  • View blog
  • Posts: 407
  • Joined: 10-April 09

Re: if statement.

Posted 25 April 2010 - 02:00 PM

Thanks a lot!
:blush:

you really helped me.

Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1