[Solved] C# how to access variables without creating an intance?


If you want more than one economy, then you need a property that keeps track of which economy each worker belongs to. Then you can just use that reference to subtract the wage from the correct economy:

public class Worker {

    public Economy InEconomy { get; private set; }
    public int Wage { get; private set; }

    // set the econdomy and wage in the constructor
    public Worker(Economy economy, int wage) {
        this.Wage = wage;
        this.InEconomy = economy;
    }

    public void Pay() {
        InEconomy.money -= this.Wage;
    }
}

public class Economy {
    public int money;
}

solved C# how to access variables without creating an intance?