0

Firstly I would like to thank anyone taking the time to look over this.

My issue as I am quite new to C# is that i need to declare a class variable that will store the current balance of the user.

The reason that it is a class variable, is that it needs to be accessed by many methods.

It is my first time having to deal with storing variables in other classes. If anyone knows how to change a variable that exists in a different class that would fix my issue!

private class Balance
{
    // (1) I'm not sure what to put here
}

private void buttonDeposit_Click(object sender, EventArgs e)
{
    try
    {
        userInput = Convert.ToDecimal(textBoxUserAmount.Text);
    }
    catch
    {
        MessageBox.Show("Numbers only Please");
        return;
    }
    //CheckDeposit is a boolean method checking if the users input is
    //between certain numbers
    if (CheckDeposit(check) == true)
    {
        // (2) here I want it to call Balance and += userInput but I
        // have no idea how to 
    }
    else
    {
        MessageBox.Show("Incorrect amount, make sure the input is between 20 and 200 inclusive");
        return;
    }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • You need a [property or a field](https://stackoverflow.com/questions/295104/what-is-the-difference-between-a-field-and-a-property) in your class. – ProgrammingLlama Apr 23 '18 at 06:00

1 Answers1

0
public class Balance
    {
    //Create variable with private access modifier
    private int _currentBalance;
    //Access it through property
    public int CurrentBalance{
    get
     {
       return _currentBalance;
     }
     set
      {
        _currentBalance = value;
      }
    }
}

    //Use it like

    balanceInstance.CurrentBalance += userInput

You can restrict user by providing correct access to property as well. You can make property as readable or writable or both.

Update as per the comments:

Public class is needed to access public property through out your project. In your case if you want to access CurrentBalance anywhere in your project, you can create instance of class and use your property.

Access modifiers in C#

Prasad Telkikar
  • 15,207
  • 5
  • 21
  • 44
  • You might want to explain why `public class Balance` is necessary –  Apr 23 '18 at 06:07
  • Thank you a lot Prasad. I would have marked your answer as the solving thing but currently Im not sure how to properly use the balanceInstance part currently looking online but thought id ask you aswel – Reef Proctor Apr 23 '18 at 06:23
  • 1
    @ReefProctor: No offense intended, but using `balanceInstance` is the basics of OOP programming. Explaining OOP from scratch is not suitable for StackOverflow, there's no point to keeping this question unanswered. I suggest you accept the answer (it is correct) and read up on OOP basics. [Here's a start reference](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/object-oriented-programming). – Flater Apr 23 '18 at 06:28
  • What I would like to suggest is, Create a `Balance` class, create properties related to balance in it. e.g. `TransactionDate`,`Balance`, `Credit`,`Debit` and so on. If your function `Button_click ()` in same class , then **No need to create instance of Balance class**. You can use it directly. If you want to access those properties outside of `Balance` class, then create instance like `Balance bal = new Balance()` . I usually create instance in constructor – Prasad Telkikar Apr 23 '18 at 06:28
  • @Flater: No offense taken! I do agree with you. – Reef Proctor Apr 23 '18 at 06:35
  • @Prasadtelkikar: Thank you plenty for your help! really have made this a lot clearer for me – Reef Proctor Apr 23 '18 at 06:35
  • @ReefProctor it's my pleasure. Happy coding – Prasad Telkikar Apr 23 '18 at 06:38