Using Else with blank If
-
Well, if the condition is to test the equality of two variables,
bool b;
if (x = y)
{
b = true;
}
else
{
b = false;
}will give you a compiler warning, and
bool b = (x = y);
will not. (Note the assignment vs equality test bug)
That's a good point and is actually something that had never occurred to me. However, whether or not you get a warning depends upon the types of x, y and b is not as simple as it seems. It is probably also language and compiler dependant. e.g in Visual C++
bool x = true; bool y = true; bool b = (x = y);
gives no warning, as you say, but
int x = true; int y = true; bool b = (x = y);
does generate a warning (forcing an int to be a bool) and
int x = true; int y = true; BOOL b = (x = y);
doesn't.
Regards - Roger
-
This is so minor it probably is a non issue but it bugs me.
If blnFlag = False Then
'Good
Else
Continue
End IfThis happens alot but I believe it is better to say
If blnFlag = True Then
Continue
End If
CleaKO
"I think you'll be okay here, they have a thin candy shell. 'Surprised you didn't know that." - Tommy Boy
"Fill it up again! Fill it up again! Once it hits your lips, it's so good!" - Frank the Tank (Old School)If there is a complicated method and I want to indicate that I did indeed accomodate both possibilities I will, ocassionaly, leave a blank if. However, I almost never check a boolean variable against a boolean and instead prefer :
if(isFlagSet){ //Some commented out code or not about //Why not used } else if(!isFlagSet){ //Some Actual code ... } else{ //File Not Found }
Just kidding with the File not found!
File Not Found
-
I think it is just easier to read when you say say what you want it to equal.
If boolean = True
instead of just saying
If boolean
I just couldnt think of a good example but what I was getting at is really saying something like
If strType = "GOOD" OrElse strType = "COOL" Then
'Good Record
Else
blnError = True
End Ifinstead of saying
If strType <> "GOOD" AndAlso strType <> "COOL" Then
blnError = True
End If
CleaKO
"I think you'll be okay here, they have a thin candy shell. 'Surprised you didn't know that." - Tommy Boy
"Fill it up again! Fill it up again! Once it hits your lips, it's so good!" - Frank the Tank (Old School)boolean = True
but that's a boolean expression again, which could be true.. or false. So to make absoultely clear you want b = true to be true (not false), you writeIf (boolean = True) = True
lather, rinse, repeat :rolleyes:
Developers, Developers, Developers, Developers, Developers, Developers, Velopers, Develprs, Developers!
We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
Linkify!|Fold With Us! -
Well, if the condition is to test the equality of two variables,
bool b;
if (x = y)
{
b = true;
}
else
{
b = false;
}will give you a compiler warning, and
bool b = (x = y);
will not. (Note the assignment vs equality test bug)