How to tell if an out parameter was set already?
Asked Answered
R

2

7

Is there a way to know if an out parameter was set already or not. This is the pseudocode for what I am looking for:

public virtual string blabla(long num, out bool bval)
    {
        if (!bval.HasValue)
            {
            //Do some default logic
            bval = defaultValue;
            }

        return blabla2(num, bval);
    }
Rebuff answered 28/4, 2012 at 18:31 Comment(2)
If you expect something to come in the method, not only to go out, then use 'ref' instead of 'out'.Surgeonfish
Looks like you need a nullable type : ref bool? bvalHenrieta
Y
12

You can't - you can't read the variable until it's been definitely assigned within your method. You should think of it as being like a local variable, declared but not assigned any value at the start of the method - but which you must assign a value to before you return. (It's okay not to have assigned a value to it if an exception is thrown.)

If you want a parameter which carries information as input to the method as well as propagating information out, you should use ref instead of out.

See my article on parameter passing for more information.

Yingling answered 28/4, 2012 at 18:34 Comment(4)
An out parameter is more suitable in my case. But I was just checking to see if there is some way to check if a value has been assigned. Thanks anyway.Rebuff
@Omtara: What exactly do you mean by "if a value has been assigned"? Do you mean outside the method, or within your own method? If you mean within your own method, you could track it with a separate bool local variable yourself. It's odd not to know just by where you are in the code in most places though.Yingling
@Rebuff - how is an out parameter more suitable when in the comment you're saying that an outer method from where the current method is called is using the same out parameter ?Clung
@Joanna: I ended up changing the logic a bit to make it work with the out parameter. However, as @ henk-holterman suggested, a ref Nullable<bool> is a good way of knowing if it was set or not.Rebuff
U
2

In addition to Jon's excellent answer, if you want the parameter to still be out, but need to see if it has been assigned a value at some place inside the method, you could use a local nullable type like follows:

public virtual string blabla(long num, out bool bval)
{
    bool? bvalLocal;

    ... //I'm assuming there is some code here that may or 
        //may not assign bvalLocal?

    // This whole if block may not be needed if the default
    // value is the default for the type (i.e. false) as
    // GetValueOrDefualt() will take care of that (see 
    // second to last line).
    if (!bvalLocal.HasValue)
    {
        //Do some default logic
        bvalLocal = defaultValue;
    }

    bval = bvalLocal.GetValueOrDefault();
    return blabla2(num, bval);
}
Uppercut answered 28/4, 2012 at 18:34 Comment(2)
The method that needs to check an out parameter is called from another method that uses the same out parameter. So using a local nullable variable would not work.Rebuff
@Rebuff - it might help if you posted the code (or pseudocode) of those method callsClung

© 2022 - 2024 — McMap. All rights reserved.