I have many scenarios during my development where I want to do something such as
try
{
long presult = EvalInner(eqtn,new Tuple<int, int>(++begidx,curidx-1),eqtnArgs);
}
catch ( Exception e )
{
throw e;
}
result = evalNewResult(result,lastop,presult,ref negateNextNum,ref negateNextOp);
// ...
return presult;
but then my compiler flags the presult
on the line
result = evalNewResult(result,lastop,presult,ref negateNextNum,ref negateNextOp);
saying
The name 'presult' does not exist in the current context
If it were smart, it would understand that presult
is either initialized in the try
block, or the procedure is exited before presult
is ever used.
Possible workarounds (none of them good):
- Declare
long presult;
right before thetry
statement. This makes the compiler mad because it wrongly thinks there's a possibility of returning an unintialized variable. - Initialize it with
long presult = default(long)
. This works, but it's bad practice because someone reading the code doesn't know whether intializing it to the default value is to work around the problem described in 1. or is because the valuepresult
because set to the defaultlong
has some real meaning in the context of the program. - Initialize it with
long? presult = null
. This is semantically better because it's clear that it means "presult is meant to have no value at this point" whereas in 2. the reader has to figure out that presult has a meaningless value. The problem here is that, not only does it take extra memory tonull
ify a value, but I then have to change the functionEvalInner
to return along?
and this results in a chain of needing to change many morelong
s tolong?
s and my program ends up splattered withnull
ified variables; it's a complete mess of question marks haha.
Anyways, how should I be handling a case like this?