Hi guys new to C# and trying to learn

BigKahuna1971

Member
Joined
Jul 3, 2023
Messages
7
Programming Experience
Beginner
Be greatful for a bit of help as to where iv'e gone wrong here.
C#:
using BankyStuffLibrary;
using System;
using Humanizer;

namespace MySuperBank

{

    class Program
    {


        static void Main(string[] args)
        {
            Console.WriteLine(3501.ToWords());












            var account = new Bank_Account("Shazza", 10000);
            Console.WriteLine($"Account {account.Number} was created for {account.Owner} with {account.Balance}.");

            account.MakeWithdrawal(200, DateTime.Now, "Egg Chair");
            account.MakeWithdrawal(50, DateTime.Now, "Xbox Game");
            account.MakeWithdrawal(5, DateTime.Now, "Coke");
            account.MakeWithdrawal(120, DateTime.Now, "Pc Game");
            account.MakeWithdrawal(7, DateTime.Now, "Bigmac");
            account.MakeWithdrawal(50, DateTime.Now, "Haircut");
            account.MakeWithdrawal(35, DateTime.Now, "Steak");



            Console.WriteLine(account.GetAccountHistory());







            ////hey this is a comment
            //// Test for a negative balance.
            //try
            {
                account.MakeWithdrawal(7500, DateTime.Now, "Attempt to overdraw");
            }
            catch (InvalidOperationException e)
            {
               Console.WriteLine("Exception caught trying to overdraw");
                Console.WriteLine(e.ToString());
            }

            // Test that the initial balances must be positive.

            try
            {
               var  invalidAccount = new Bank_Account("invalid", -55);
            }
            catch (ArgumentOutOfRangeException e)
            {
                Console.WriteLine("Exception caught creating account with negative balance");
                Console.WriteLine(e.ToString());
               
            }

            account.MakeWithdrawal(50, DateTime.Now, "Xbox Game");
            Console.WriteLine(account.Balance);
        }
    }

}
And the next part.
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Humanizer;

namespace BankyStuffLibrary
{
    public class Transaction
    {
        public decimal Amount { get; }

        public string AmountForHumans {
            get; }

        public DateTime Date => ((int)Amount.ToWords();


        public string Notes { get; }

        public Transaction(decimal amount, DateTime date, string note)
        {
            Amount = amount;
            Date = date;
            Notes = note;
        }
    }
}
Be great if anyone could help.
Thanks in advance.
 
Last edited by a moderator:
Welcome to the forum. In the future, please post your code in code tags. It's the button on the toolbar that looks like </>. It helps preserve the formatting of the code to make it easier to read.

So what specific problem are you running into? What have you tried to fix the issue? You can't just dump your code with just a vague description saying something has gone wrong. Well, actually you can, but you aren't really helping people who may want to try to help you. Now you have increased the difficulty of trying to help you by forcing them to have to copy and paste your code into their own editor to try to discover what issue you are having.

Right now, just scanning your code, it look like your line 17 of your "and the next part" looks very wrong. The first part that looks wrong are mismatched parenthesis. The next part that looks wrong is trying to return the transaction amount as a DateTime.

The other part that looks wrong with the "and the next part" is that it seems that you have a whole bunch of getters", but no private seters or initializer's.
 
Last edited:
Line 21 i have a problem not entirely sure how to fix it "Amount" and ";" also line 31"Date"
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Humanizer;

namespace BankyStuffLibrary
{
    public class Transaction
    {
        public decimal Amount { get; }

        public string AmountForHumans {
            get; }

        public DateTime Date {
            get

            {
                 return ((int)Amount.ToWords();
            }
    }


        public string Notes { get; }

        public Transaction(decimal amount, DateTime date, string note)
        {
            Amount = amount;
            Date = date;
            Notes = note;
        }
    }
}
 
Perhaps you mean tthis

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace BankyStuffLibrary
{
    public class Transaction
    {
        public decimal Amount { get; set; }

        public string AmountForHumans => Amount.ToWords();

        public DateTime Date { get; set; }
  


        public string Notes { get; set; }

        public Transaction(decimal amount, DateTime date, string note)
        {
            Amount = amount;
            Date = date;
            Notes = note;
        }
    }
}

Auto props must have get;set;
Cannot return an int from a prop you declared as returning DateTime
Cannot cast a string to an int
Number of ( must match number of ) on a line
 
Line 21 i have a problem not entirely sure how to fix it "Amount" and ";" also line 31"Date"

That's still not an explanation. In future, please explain exactly what you're trying to achieve, how you're trying to achieve it and what happens when you try. Every line of code does what it does so, if you don't tell us what you want it to do, we can only guess at why exactly it's wrong. If there's a compilation error or a run-time exception then point out exactly where and what the error message is.
 
Like i say im new to coding so im not able to get all the terminoligy correct, but would be greatful if you could point me in the right direction of some free learning or perhaps tips as in wht to learn first etc thank you.
 
Perhaps you mean tthis

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace BankyStuffLibrary
{
    public class Transaction
    {
        public decimal Amount { get; set; }

        public string AmountForHumans => Amount.ToWords();

        public DateTime Date { get; set; }
 


        public string Notes { get; set; }

        public Transaction(decimal amount, DateTime date, string note)
        {
            Amount = amount;
            Date = date;
            Notes = note;
        }
    }
}

Auto props must have get;set;
Cannot return an int from a prop you declared as returning DateTime
Cannot cast a string to an int
Number of ( must match number of ) on a line

Thank you buddy i still get this comes up with the squigily red line under "ToWords"
Severity Code Description Project File Line Suppression State
Error CS1061 'decimal' does not contain a definition for 'ToWords' and no accessible extension method 'ToWords' accepting a first argument of type 'decimal' could be found (are you missing a using directive or an assembly reference?) BankyStuffLibrary C:\Users\thepm\source\repos\MySuperBank\BankyStuffLibrary\Transaction.cs 14 N/A
 
Looks like that's what your `using Humanizer;` line was for

1688486786374.png


If it's been removed you'll need to reinstate it. If you click on the red line a light bulb should appear in the margin, and one of the options os to add a `using`, if Humaizr is installed (use Nuget Package Manager?)

The process for using code libraries written by others of 3 step: Install the lib, add a reference to it, add a `using` for it. Nuget Package Manager does the first two, then usually a VS helper or the developing human does the other part
 
Take it from me, another inexperienced programmer, getting to understand these errors is pertinent to be successful. This error is very accurate

C#:
Error CS1061 'decimal' does not contain a definition for 'ToWords' and no accessible extension method 'ToWords' accepting a first argument of type 'decimal' could be found (are you missing a using directive or an assembly reference?)

It's telling you exactly the problem, but how to decipher?
ToWords is a function, see the () parans after the function name, that is being called on the variable Amount.
public string AmountForHumans => Amount.ToWords();
Amount is defined as a variable of type decimal.
public decimal Amount { get; set; }

That is why the error has '' around the words decimal and ToWords. They are the key to why this error exist in the first place. The other key part in this error is the part in parans. As you have the using directive it can only mean that you are missing the assembly reference. That which is installed by nuget as a dll file that your assembly must reference. You are essentially just telling the compiler where the code to ToWords resides and you want to be able to use it in your code.
are you missing a using directive or an assembly reference?

Always take time to read the error and try to decipher why it's complaining. You will be amazed at how accurate they can be for new programmers.
 
I'll also add that syntax errors can be a little odd. If you see a ton of squiggly marks after a certain line in your code, check that line for syntax errors. a missing closing bracket will wreak havoc on the code linter.
 
There are some logical issues with your code, you can try below code to fix these issues.

First section:

C#:
using BankyStuffLibrary;
using System;

namespace MySuperBank
{
    class Program
    {
        static void Main(string[] args)
        {
            var account = new Bank_Account("Shazza", 10000);
            Console.WriteLine($"Account {account.Number} was created for {account.Owner} with {account.Balance}.");

            account.MakeWithdrawal(200, DateTime.Now, "Egg Chair");
            account.MakeWithdrawal(50, DateTime.Now, "Xbox Game");
            account.MakeWithdrawal(5, DateTime.Now, "Coke");
            account.MakeWithdrawal(120, DateTime.Now, "Pc Game");
            account.MakeWithdrawal(7, DateTime.Now, "Bigmac");
            account.MakeWithdrawal(50, DateTime.Now, "Haircut");
            account.MakeWithdrawal(35, DateTime.Now, "Steak");

            Console.WriteLine(account.GetAccountHistory());

            // Test for a negative balance.
            try
            {
                account.MakeWithdrawal(7500, DateTime.Now, "Attempt to overdraw");
            }
            catch (InvalidOperationException e)
            {
                Console.WriteLine("Exception caught trying to overdraw");
                Console.WriteLine(e.ToString());
            }

            // Test that the initial balances must be positive.
            try
            {
                var invalidAccount = new Bank_Account("invalid", -55);
            }
            catch (ArgumentOutOfRangeException e)
            {
                Console.WriteLine("Exception caught creating account with negative balance");
                Console.WriteLine(e.ToString());
            }

            account.MakeWithdrawal(50, DateTime.Now, "Xbox Game");
            Console.WriteLine(account.Balance);
        }
    }
}


Second Section

C#:
using System;
using System.Collections.Generic;
using System.Text;

namespace BankyStuffLibrary
{
    public class Bank_Account
    {
        public string Owner { get; }
        public decimal Balance
        {
            get
            {
                decimal balance = 0;
                foreach (var item in allTransactions)
                {
                    balance += item.Amount;
                }
                return balance;
            }
        }

        private static int accountNumberSeed = 1234567890;

        private List<Transaction> allTransactions = new List<Transaction>();

        public string Number { get; }

        public Bank_Account(string name, decimal initialBalance)
        {
            Owner = name;
            MakeDeposit(initialBalance, DateTime.Now, "Initial balance");
            Number = accountNumberSeed.ToString();
            accountNumberSeed++;
        }

        public void MakeDeposit(decimal amount, DateTime date, string note)
        {
            if (amount <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(amount), "Amount of deposit must be positive");
            }
            var deposit = new Transaction(amount, date, note);
            allTransactions.Add(deposit);
        }

        public void MakeWithdrawal(decimal amount, DateTime date, string note)
        {
            if (amount <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(amount), "Amount of withdrawal must be positive");
            }
            if (Balance - amount < 0)
            {
                throw new InvalidOperationException("Not sufficient funds for this withdrawal");
            }
            var withdrawal = new Transaction(-amount, date, note);
            allTransactions.Add(withdrawal);
        }

        public string GetAccountHistory()
        {
            var report = new StringBuilder();
            report.AppendLine("Date\t\tAmount\tNote");
            foreach (var item in allTransactions)
            {
                report.AppendLine($"{item.Date.ToShortDateString()}\t{item.Amount}\t{item.Notes}");
            }
            return report.ToString();
        }
    }
}

Copy and paste same code, I hope you can get the desire result you are looking for.

Thanks
 
Copy and paste same code, I hope you can get the desire result you are looking for.
While your help is obviously appreciated, just copying and pasting someone else's code is not the best way to learn. If you could explain the logical issues you found and how your code addresses them, that would be appreciated even more.
 
Hmmm...

Doing a diff between the Program.cs of @BigKahuna1971 post #1, and @gulshan212 post #11, is that @gulshan212 cleaned up the formatting, removed the use of the Humanizer library, and deleted line 15 which was testing the Humanizer library. That is not a logic error on the OP's part. That is a logic error on @gulshan212 's part for changing the logic of the original code. The OP wanted to use Humanizer to give user friendly output.

Also in post #11 in looks like @gulshan212 was just trying to supply a Bank_Account class which the OP didn't provide (but was obviously using). I'm still not seeing where the logic error is in the OP's code. His issue had to do with using the Humanizer library, not with a missing class.
 
Hello, @BigKahuna1971 , @Skydiver , @cjard , @jmcilhinney , @gulshan212
I have a doubt: in BigKahuna1971's code, there was a call to ToWords() on a decimal variable; I suppose that it's supposed to be called on an integer, due its output are pure integers (like one hundred and thirty five), not floating point types. I don't know if this helps, but just in case I mark it. Also, as someone says in previous posts, the solution is not getting rid of using that method or some class (or Nuget Package), but using it properly.
Pablo
 
That's correct. A quick scan of the Humanizer source code seems to suggest that it only supports int and long.

Our OP (or a contributor to the Humanizer library) would have to write some code to handle the fractional parts of the number if the fractional part is non-zero. A vaguely recall this kind of homework in my CS class for writing checks, but that was easy because we were guaranteed at that time that the fractional part would always just be shown as "nn/100".
 

Latest posts

Back
Top Bottom