0

This might sound silly. But I'm working on a C# assignment for school right now, and I can't get a simple if-else statement to work. Isn't the syntax the same as in C++ or Java?

I want to do one thing if a boolean variable is true, something else if it's not. The "bool" type exists in C#, right? ;)

9
Contributors
11
Replies
22
Views
14 Years
Discussion Span
Last Post by guitarded
-1
if("a"=="d") 
{
 //do something
} 
else if("a"=="c") 
{
 // do something else
}
else 
{
 //do something if i am crazy
}
0

Thanks, Tek. I fixed the problem. I was out of scope for what I wanted to do.

0

I know you fixed it but I recall that C# doesn't allow for boolean values like true and false I'm thinking that was it am I correct?

0

actually C# depends entirely on bool data types in both selection and loop statements, it does not accept any other types we used to in C++ for example
E.G :
While (1) // it will compile in C++ but it will be a compiler error in C#
{
}

0

All the C++ would be doing is converting 1 into "true"; that's not rocket science. This would work:

while(Convert.ToBoolean(1)) {}

0

This might sound silly. But I'm working on a C# assignment for school right now, and I can't get a simple if-else statement to work. Isn't the syntax the same as in C++ or Java?

I want to do one thing if a boolean variable is true, something else if it's not. The "bool" type exists in C#, right? ;)

bool bFirsttime;

if (bFirsttime)
{
//code for true condition
}
else
{
//code for false condition

}

0

alternate shorthand syntax...

txtTest.Text = (txtName.Text == String.Empty) ? "No name" : txtName.Text;

which translates to...

if(txtName.Text == String.Empty)
txtTest.Text = "No name";
else
txtName.Text;

maybe not as readable, but sure saves typing!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.