-1

I have created a class that needs to alter a variable's value when it is instantiated.

Example: In my LrgDialogBox class I might have:

public LrgDialogBox(ref oldResult)
{
    // bunch of code    
    UserInput();
}

public UserInput()
{
    newResult=false;
}

In my main class I create an object of my LrgDialogBox called lrgDia then I type:

lrgDia = new LrgDialogBox(ref result);
if (result==true) this.exit;

I basically need to know how to make the reference variable "oldResult" private in my LrgDialogBox class, so that any method can alter its value so it can be used in my main class. Hopefully without changing the parameters of my other methods. Please help.

Kris

User
  • 1,118
  • 7
  • 23
Kris
  • 1

2 Answers2

1

There isn't any way for you to meaningfully store the reference parameter that is passed in and be able to modify its value later. What you need to do is add in another layer of indirection; create a reference type that holds onto the value that you really care about. Pass around references to that type, and then all of those references are indirectly pointing to a single value.

The implementation of such a wrapper is simple:

public class Wrapper<T>
{
    public T Value { get; set; }
}

You can now create a class that accepts a Wrapper<bool> in the constructor, and then modifies the value within that wrapper at a later point in time.

public class Foo
{
    private Wrapper<bool> flag;
    public Foo(Wrapper<bool> flag)
    {
        this.flag = flag;
    }

    public void Bar()
    {
        flag.Value = false;
    }
}

The other option available to you, since you are, in this case, only calling the method from within the constructor, is to simply have your other method return its value, rather than setting a private field. This would be the preferred design:

public class LrgDialogBox
{
    public LrgDialogBox(ref bool oldResult)
    {
        // bunch of code    
        oldResult = UserInput();
    }

    public bool UserInput()
    {
        return false;
    }
}
Servy
  • 202,030
  • 26
  • 332
  • 449
  • @Steve If you're *mutating* the object referred to by a reference type, then it's not necessary. If you need to return a new reference, then it is necessary. In short, if you wish to change the value in the variable, this is necessary, regardless of whether it's a reference or value type. – Servy Sep 05 '13 at 20:43
  • @Steve No, it's not. If it were a reference type and you wished to mutate the object referred to by the reference then you wouldn't need to pass it by `ref` at all, you could just pass the value. You would only pass there reference type by reference if you wished to change the value of the variable, that is to have the variable refer to an entirely different object. – Servy Sep 05 '13 at 20:45
-1

Just use a private variable to work with during the processing.

    private bool _newResult;

    public LrgDialogBox(ref bool oldResult)
    {
        // bunch of code
        _newResult = oldResult;
        UserInput();
        oldResult = _newResult;
    }

    private void UserInput()
    {
        _newResult = false;
    }
aleppke
  • 496
  • 4
  • 13