0

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.

TxRx21
  • 5
  • 2

3 Answers3

0

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.
Selmir
  • 1,136
  • 1
  • 11
  • 21
  • thank you so much. I'd also like to clarify because there is also 1 answer here too that he didn't use the { get; set; }. What are their differences if we dont use them? – TxRx21 Apr 09 '22 at 10:19
0

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)

Reza Heidari
  • 1,192
  • 2
  • 18
  • 23
  • Thank you so much for answering. I'm also curious because 1 comment here says to use the {get; set; }. What are their differences if we use the get set and if we dont use? – TxRx21 Apr 09 '22 at 10:20
  • there is a number of benefits to using properties, yet it is not possible to describe all here in a single comment. the important one is that you can use property setters/getters to apply the logic that a mere public field can't. You google it, I'm sure there are a lot of exciting topics you can find to read and learn. Cheers. – Reza Heidari Apr 09 '22 at 10:24
  • When I first encountered setters/getters I had no idea where to use it. Now I know and will read regarding this. Thank you so much for your advice and clarifications. – TxRx21 Apr 09 '22 at 10:27
  • You may also read this topic too https://stackoverflow.com/questions/295104/what-is-the-difference-between-a-field-and-a-property – Reza Heidari Apr 09 '22 at 10:29
0

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;
}