user5561715
user5561715

Reputation: 23

Access object of a class from other classes

I realise this is probably a very simple question and will be answered in no time. But wondering what to do. I have a class called Budget with an object named 'user01' I would basically like to be able to access that object across multiple classes, similar to the code below.

Main

static void Main(string[] args)
    {
        Budget user01 = new Budget(1000);
    }

Budget Class

class Budget
{
    private int _budget;

    public Budget(int budget)
    {
        _budget = budget;
    }

    public int UserBudget
    {
        get { return _budget; }
        set { _budget = value; }
    }
}

Expense Class

class Expenses
{
    // What I've been trying to do...
    public int Something(user01.budget)
    {
        user01.budget - 100;
        return user01.budget;
    }
}

I'm not really sure where to go from here, and am hoping to a little help/explanation. Many thanks

Upvotes: 1

Views: 1642

Answers (2)

Ron Beyer
Ron Beyer

Reputation: 11273

Its a pretty simple change to your Expenses class:

class Expenses
{
    // What I've been trying to do...
    public int Something(Budget userBudget)
    {
        userBudget.UserBudget -= 100;
        return userBudget.UserBudget;
    }
}

Which you then call like this from your main class:

static void Main(string[] args)
{
    Budget user01 = new Budget(1000);
    Expenses expenses = new Expenses();
    var result = expenses.Something(user01);
}

Or, if you make your Something method static you can call it without an instance:

class Expenses
{
    // What I've been trying to do...
    public static int Something(Budget userBudget)
    {
        userBudget.UserBudget -= 100;
        return userBudget.UserBudget;
    }
}

Which you call like this:

static void Main(string[] args)
{
    Budget user01 = new Budget(1000);
    var result = Expenses.Something(user01);
}

Its important when designing methods to remember that a method takes in a general argument and its the caller that passes in something specific.

Upvotes: 1

David
David

Reputation: 219096

This is invalid:

public int Something(user01.budget)

But you can supply an instance of a Budget object to that method:

public int Something(Budget budget)
{
    budget.UserBudget -= 100;
    return budget.UserBudget;
}

Then you can invoke that method from your consuming code:

Budget user01 = new Budget(1000);
Expenses myExpenses = new Expenses();
int updatedBudget = myExpenses.Something(user01);

The method doesn't "access the variable user01". However, when you call the method, you supply it with your user01 instance. Inside of the method, the supplied instance in this case is referenced by the local budget variable. Any time you call the method and give it any instance of a Budget, for that one time that instance will be referenced by that local variable.

Go ahead and step through this using your debugger and you should get a much clearer picture of what's going on when you call a method.

(Note that your naming here is a bit unintuitive, which is probably adding to the confusion. Is your object a "budget" or is it a "user"? Clearly defining and naming your types and variables goes a long way to making code easier to write.)

Upvotes: 1

Related Questions