Jump to content
AutoDesSys

What is wrong with this Statement? Code: beware..


Recommended Posts

if (topoType == (FZ_OBJT_TOPO_OWRE || FZ_OBJT_TOPO_CWRE || FZ_OBJT_TOPO_SURF) && LengthAbs_v_Perc== 0)

 

the Compiler says it is an invalid cast.    But doesn't everything cast to a boolean any way??

 

I played a little bit with O-o-Ops but get the same result.

 

I end up having to be more explicit

if ((topoType == FZ_OBJT_TOPO_OWRE || topoType == FZ_OBJT_TOPO_CWRE ||topoType == FZ_OBJT_TOPO_SURF ) && LengthAbs_v_Perc== 0)

 

Is there a shorter way?

 

Thanks!

Link to comment
Share on other sites

  • 2 months later...

Surely what you're writing here first would be invalid in any programming language?

 

Basically you are comparing a bunch of values, resulting in true if they are and false if you don't. In your case they are topotypes but it could be anything really. As a thought experiment when you substitute integers for topotypes, I find it becomes easier to see that there is something strange going on:

 

if ( 1 == ( 2 || 3 || 4) && LengthAbs_v_Perc == 0)

 

The compiler will try to evaluate the ( 2 || 3 || 4) first as it lives between brackets but there isn't really an operation to evaluate there.

 

The other one you write makes more sense

if ((1 == 2 || 1 == 3 || 1 == 4) && LengthAbs_v_Perc == 0)

 

I don't think there is any way of writing it shorter. There is no shorter syntax and you can't shorten the variable names as they are predefined. If it's just about readability you could consider doing the comparisons before the if statement (but you already knew that):

 

in pseudocode:

bool comparison1 = topoType == FZ_OBJT_TOPO_OWRE

bool comparison2 = topoType == FZ_OBJT_TOPO_CWRE

bool comparison3 = topoType == FZ_OBJT_TOPO_SURF

 

if ((comparison1 || comparison2 || comparison3) && LengthAbs_v_Perc == 0)

{

//

}

 

 

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...