-2

I want to assign output values on Data class after create object with ref constructor to State class in this code. How can I solve this?

class Program
{
    static void Main(string[] args)
    {
        Data data = new Data();
        State stateA = new State("A", ref data.outputA);
        State stateB = new State("B", ref data.outputB);
        stateA.output = 10;
        stateB.output = 8;
        Console.WriteLine(data.outputA); //10
        Console.WriteLine(data.outputB); //8
        Console.ReadLine();
    }
}
public class Data
{
    public int outputA;
    public int outputB;
}
public class State
{
    public string name;
    public ref int address;
        
    public State(string _name, ref int _output)
    {
        this.name = _name;
        address = _output;
    }
}
Emre
  • 1
  • 2
  • Solve *what* exactly? Does this compile and run? If so, what’s the output? If not, what is the error? – stuartd Feb 27 '22 at 22:38
  • My purpose is changing data class value with assigning state class object, but ref value is not assignable on constructor, so this code can not run and I do not know how to solve this. – Emre Feb 27 '22 at 22:41
  • Suggest you read the `ref` [docs](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref) and look at the examples there – stuartd Feb 27 '22 at 22:43

2 Answers2

0

In c# the ref keyword is far from the concept of a C-like pointer. This is the closest thing you can do in that direction (using safe code):

class Program
{
     static void Main(string[] args)
    {
        Data data = new Data();
        State stateA = new State("A", value => data.outputA = value, () => data.outputA);
        State stateB = new State("B", value => data.outputB = value, () => data.outputB);
        stateA.Output = 10;
        stateB.Output = 8;
        Console.WriteLine(data.outputA); //10
        Console.WriteLine(data.outputB); //8
        Console.ReadLine();
    }
}


public class Data
{
    public int outputA;
    public int outputB;
}
public class State
{
    public string name;
    private Action<int> _outputSetter;
    private Func<int> _outputGetter;

    public State(string _name, Action<int> _outputSetter, Func<int> _outputGetter)
    {
        this.name = _name;
        this._outputSetter = _outputSetter;
        this._outputGetter = _outputGetter;
    }


    public int Output
    {
        get => _outputGetter();
        set => _outputSetter(value);
    }
}
0

you will have to correct State class a little

  Data data = new Data();
    State stateA = new State("A", ref data);
    State stateB = new State("B", ref data);
    stateA.output = 10;
    stateB.output = 8;
    Console.WriteLine(data.outputA); //10
    Console.WriteLine(data.outputB); //8
    Console.ReadLine();

public class State
{
    public int output
    {
        get { return name == "A" ? address.outputA : address.outputB; }
        set { if(name == "A") address.outputA = value; else   address.outputB = value; }
    }

    public string name;
    public Data address;

    public State(string _name, ref Data _output)
    {
        this.name = _name;
        address = _output;
    }
}
Serge
  • 40,935
  • 4
  • 18
  • 45