0

Do you know why this can't run?

can anyone tell me why this code can't run? namespace SoloLearn { class BankAccount { private double balance=0; public void Deposit(double n) { balance += n; sb.append = n; } public void Withdraw(double n) { balance -= n; sb.append = n; } public double GetBalance() { return balance; } } class Program { static void Main(string[] args) { StringBuilder sb

21st May 2017, 6:00 AM
SeMsEm
11 ответов
+ 4
It was sb.Append not sb.append 😜 https://code.sololearn.com/c26yYLU7PPT3/?ref=app
21st May 2017, 8:44 AM
Cyrus Ornob Corraya
Cyrus Ornob Corraya - avatar
+ 3
Just send the StringBuilder sb as another parameter.
21st May 2017, 7:40 AM
Cyrus Ornob Corraya
Cyrus Ornob Corraya - avatar
+ 3
// Your project but Improved. 😊😉😊 namespace SoloLearn { class BankAccount { private double balance=0; public void Deposit(double n, StringBuilder sb) { balance += n; sb.append("Deposit : " + n.ToString() + "\n"); } public void Withdraw(double n, StringBuilder sb) { balance -= n; sb.append("Withdraw : " + n.ToString() + "\n"); } public double GetBalance() { return balance; } } class Program { static void Main(string[] args) { StringBuilder sb = new StringBuilder(); BankAccount b = new BankAccount(); b.Deposit(199, sb); b.Withdraw(42, sb); string sb1 = sb.ToString(); Console.WriteLine(sb1); Console.WriteLine(b.GetBalance()); } } }
21st May 2017, 7:44 AM
Cyrus Ornob Corraya
Cyrus Ornob Corraya - avatar
+ 2
you are declaring stringbuilder sb inside the main fix it.
21st May 2017, 7:01 AM
MR Programmer
MR Programmer - avatar
+ 1
how do you know your code is not working? write it in code playground and insert your code in this question.
21st May 2017, 6:10 AM
Setiawan Next
Setiawan Next - avatar
+ 1
sorry here is the full code.. some text were missing from question I wrote it in code playground and it didn't work namespace SoloLearn { class BankAccount { private double balance=0; public void Deposit(double n) { balance += n; sb.append = n; } public void Withdraw(double n) { balance -= n; sb.append = n; } public double GetBalance() { return balance; } } class Program { static void Main(string[] args) { StringBuilder sb = new StringBuilder(); BankAccount b = new BankAccount(); b.Deposit(199); b.Withdraw(42); string sb1 = sb.ToString; console.writeline(sb1); Console.WriteLine(b.GetBalance()); } } }
21st May 2017, 7:03 AM
SeMsEm
+ 1
..\Playground\(34,26): error CS0428: Cannot convert method group 'ToString' to non-delegate type 'string'. Did you intend to invoke the method? ..\Playground\(35,10): error CS0103: The name 'console' does not exist in the current context
21st May 2017, 7:17 AM
SeMsEm
+ 1
thanks i have learnt a lot there were multiple mistakes in my code 1. send sb as parameter 2. sb.Append not append 3. sb.ToString(); needed the () 4. Console.WriteLine not console.writeline it works as this https://code.sololearn.com/c48N8ZA0FI2f/?ref=app with output /* balance =0 +199 -42 balance =157 */
21st May 2017, 9:12 AM
SeMsEm
0
I'm working with sb coz I want to display a string at the end... showing all steps and it will include all deposit and withdrawal records
21st May 2017, 7:29 AM
SeMsEm
0
I tried to run your code and got this error ..\Playground\(47,16): error CS1061: 'StringBuilder' does not contain a definition for 'append' and no extension method 'append' accepting a first argument of type 'StringBuilder' could be found (are you missing a using directive or an assembly reference?) ..\Playground\(51,16): error CS1061: 'StringBuilder' does not contain a definition for 'append' and no extension method 'append' accepting a first argument of type 'StringBuilder' could be found (are you missing a using directive or an assembly reference?)
21st May 2017, 8:36 AM
SeMsEm
0
luka your code works and it is the default code included in sololearn course, however it doesn't do what I need.. which is writing every transaction done on this bank account.
21st May 2017, 8:38 AM
SeMsEm