Home > Back-end >  How do I access a variable from a class then use it in another class for my if statement in c#?
How do I access a variable from a class then use it in another class for my if statement in c#?

Time:04-10

Im a complete beginner to C# and trying to make a project by learning class. But Im stuck with this problem. I'm trying to pass a variable from this class

class UserAccount
{
    decimal balance = 0;
    public void PrintBalance()
    {
        Console.WriteLine("\nYour balance is: {0}", balance);
    }
    
}

to another class and want to use it in my if statement and possibly edit the variable balance

class UserInterface
{
    public UserInterface()
    {
        UserAccount userAccount = new UserAccount();

        Console.WriteLine("~~~Welcome to ATM Service~~~");
        Console.WriteLine("\nPlease select from the following:\n");
        Console.WriteLine("[1] Check Balance");
        Console.WriteLine("[2] Withdraw Cash");
        Console.WriteLine("[3] Deposit Cash");
        Console.WriteLine("[4] Quit ATM Service");
        Console.WriteLine("");
        int userChoice = Convert.ToInt32(Console.ReadLine());

        switch (userChoice)
        {
            case 1:
                userAccount.PrintBalance();
                break;
            case 2:
                Console.WriteLine("Enter amount you wish to withdraw.");
                int withdrawAmount = Convert.ToInt32(Console.ReadLine());
                if(withdrawAmount <= 0)
                {
                    Console.WriteLine("Amount must not be negative number or 0.");
                    break;
                }
                if(withdrawAmount < userAccount.balance)
                {

                }

I'm stuck with userAccount.balance in the if statement. Any help would be greatly appreciated.

CodePudding user response:

Whenever you want to access a member from outside of a Class you have to use the proper access modifier.

In a simple way, you have to use as per below:

public decimal balance = 0;

Although it is not wrong, another approach is to use Property Members inside a class:

public decimal Balance { get; set; }

For more information and a better understanding of different access modifiers you may visit the following reference:

Access Modifiers (C# Programming Guide)

CodePudding user response:

By default, in C#, visibility for a class property like your balance is set to private. You have to make it public to access it from an instance of the class.

public decimal Balance { get; set; } = 0;

Advices :

  1. 0 is already de default value for decimal type in C# so you don't have to set it.
  2. In C#, generally, we use PascalCase for public properties/methods.
  3. A getter/setter ({ get; set; }) is generally better for public properties.

CodePudding user response:

If you don't specify an access modifier the compiler set it as private by default. Class variables should always be private. You can use Properties or methods to access them.

private decimal balance;

by Propertie

public decimal Balance
{
    return balance;
}

by Method

public decimal getBalance()
{
    return balance;
}
  •  Tags:  
  • c#
  • Related