Ok that title is bad, please feel free to suggest something better, I just don't know what to call it.
Suppose I have an object a
, and I want to assign some value to it, check that this value meets some condition, then do something if the condition is met. Here is one way of doing this:
int a = 1234;
if (a > 1)
{
someFunction(a);
}
But what if I don't need a
outside of this test? If I try to do if(int a = 1234 > 1)
then I get a = 1
because it solves the right hand side first, and if I put the definition in a bracket I get this:
if ((int a = 1234) > 1) // about 3 different errors
{
someFunction(a);
}
Obviously this isn't a deal-breaker, but since you can declare objects inside for
loops I thought it might be possible to achieve something similar with if
conditions?