Question Beginner needs help with Class and properties.

Fezza

New member
Joined
Aug 21, 2016
Messages
3
Programming Experience
Beginner
Hi,

I am quite new with programming in C# and I have a beginners question.

I am trying to create a simple banking tool with WPF. My aim was to create it by using a seprate Class (bankrek).
For example, i enter an amount in a textbox, click add amount button and the amount will be displayed in a label which represents the total amount. Sounds rather simplistic but believe me, i am fighting with this for days. Why? Everytime when I put an amount in my "add" textbox it does put this amount also in the total amount label. So far so good. But if I try to add a next amount, the total amount label resets and only puts in the amount i just entered. Now, can anyone tell the beginner programmer how this could be possible? How can i just keep adding up my inserted amount to the total label? I would be very happy if someone cann solve this problem for me! Kind regards,
Fezza

I made the following maincode and separate :

maincode

namespace bankrekening
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
int addmoney;



public MainWindow()
{
InitializeComponent();

}

private void stortButton_Click(object sender, RoutedEventArgs e)
{

bankrek bank = new bankrek();
addmoney = Convert.ToInt32(stortTextBox.Text);
bank.Storting(addmoney);

//returns money to total amount
totaalBedragLabel.Content = Convert.ToString(bank.Stort);
}
}
}

now the code for the Class:
namespace bankrekening
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
int addmoney;



public MainWindow()
{
InitializeComponent();

}

private void stortButton_Click(object sender, RoutedEventArgs e)
{

bankrek bank = new bankrek();
addmoney = Convert.ToInt32(stortTextBox.Text);
bank.Storting(addmoney);

//returns money to total amount
totaalBedragLabel.Content = Convert.ToString(bank.Stort);
}
}
}
 
I have reformatted your code using proper code formatting tags. Please do so for us in future. Your code still lacks indenting though, because you posted without it. Please copy and paste code directly from the IDE if possible, otherwise hand-code the appropriate indenting so that the code is easily readable.

Also, you seem to have posted the same code twice and left out the second snippet.
 
Without being able to see all the code, I will just talk in generalities. A Label is not a place to keep data that will be used in calculations. It is for display purposes only. If you're working with money then you should use the Decimal data type. You should have a Decimal variable somewhere that stores the current total. Each time you want to add an amount, you add it to that variable first, then display the value of that variable in the Label. If the input is coming from a TextBox then you should use Decimal.TryParse to validate and convert the input to a number.
 
Thank you for your advice. I am a complete noob and I've should RTFM. Next time I will take your advice into account!
 
Back
Top Bottom