0

Good evening all,

I've just started my second year at University and I'm learning C# for the first time. I've had some coding experience with Java and Javascript before but never c#.

I'm having problems with a basic credit/debit program. See code below:

private double balance = 0;
private string creditamount;
private string debitamount;
private double currentamount = 0;

private void CreditButton_Click(object sender, RoutedEventArgs e)
{
   AmountField.Text = creditamount;
   Convert.ToDouble(creditamount);
   CurrentBalance.Text = "Your current balance is £" + (creditamount + balance);
}

private void DebitButton_Click(object sender, RoutedEventArgs e)
{
   AmountField.Text = debitamount;
   double.Parse(debitamount);
   CurrentBalance.Text = "Your current balance is £" + (debitamount - balance);
}

The user should enter an amount into the AmountField and press either the Credit or Debit button. Upon clicking either button, the amount should be converted from a string into a double and then shown in the CurrrentBalance .Text

It apppears that my strings aren't being converted into doubles.

I've tried using Convert.ToDouble(); and double.Parse(); but Visual Studio keeps giving me errors.

Does anyone have any suggestions?

1
  • You are over writing AmountField.Text in your button calls with empty debit and credit string. It should be debitamount = AmountField.Text I believe and same for your creditamount = AmountField.Text Commented Sep 16, 2016 at 20:15

2 Answers 2

0

The = assignment goes from Right to Left. Also all Convert.ToStuff return the result, so you should be expecting it (assign it to some variable).

Therefore, your event handlers (and all your future routine) should look similar to this:

private void DebitButton_Click(object sender, RoutedEventArgs e)
{
   debitamount= AmountField.Text ; // Take the Right and assign it to the Left
   currentamount = Convert.ToDouble(debitamount); // convert + assign
   CurrentBalance.Text = "Your current balance is £" + (debitamount - balance); // Again take the Right and assign it to the Left
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your quick reply! This was my first ever post on stack overflow and I'm amazed how quickly I got some help. Thanks again!
You will like it over here and Stack Overflow is useful :) Please take your time to improve your code and mark / up-vote as an answer if you found it helpful and it fixed your issue.
0

First of all if you are using wpf try to avoid this code behind thing. Secondly try to take advantage of MVVM. What is MVVM. Go through this link.

http://www.codeproject.com/Articles/165368/WPF-MVVM-Quick-Start-Tutorial

So in short i would suggest create a Viewmodel class and make it implement INotifyPropertyChanged. Create your credit and debit amount properties bind it in the UI and don't forget to use OnPropertyChanged on these properties. So when ever user enters any text in the UI i.e when ever there is a change in the UI state your set block of the variables will be called and you can proceed further. Also in mvvm you use commands not button clicks. Also set the data context of you xaml to this view model.

1 Comment

I modified my answer. Please remove the negative vote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.