Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8797031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:45:44+00:00 2026-06-13T23:45:44+00:00

I have this in my main: static void Main(string[] args) { Money m1 =

  • 0

I have this in my main:

static void Main(string[] args)
    {
        Money m1 = new Money(2315.99);

        Money m2 = new Money(4000, 25);

        Console.WriteLine(m1);
        Console.WriteLine(m2);

        Console.WriteLine(m1.IncrementMoney(m2));
    }



    public void IncrementMoney(Money x)
    {
        //what do I put in here?
    }

So

Money m1 = new Money(2315.99);
is supposed to turn 2315.99 into “$2315.99”

and

Money m2 = new Money(4000, 25);
forms “$4000.25”

I have all that done in Money class and it works fine.

Now what I’m supposed to do is add those two together using

m1.IncrementMoney(m2);

This is my “Money” class

class Money
{

    //instance variables
    private int dollars;
    private int cents;


    double amount;


    public int Dollars
    {
        get { return dollars; }
        set
        {
            if (value > 0)
                dollars = value;
        }
    }



    public int Cents
    {
        get { return cents; }
        set
        {
            if (value > 0)
                cents = value;
        }
    }



    public Money(int Dol, int Cen)
    {
        Dollars = Dol;
        Cents = Cen;

        double Dollar = Convert.ToDouble(Dollars);
        double Cent = Convert.ToDouble(Cents);

        amount = Dollar + (Cent / 100);

    }


    public Money(double am)
    {
        int dol = Convert.ToInt32(am);

        if (dol > am)
            Dollars = dol - 1;

        else if (dol < am)
            Dollars = dol;
        //Dollars

        double cen = am % 1;

        cen = cen * 100;

        Cents = Convert.ToInt32(cen);
        //Cents

        double Dollar = Convert.ToDouble(Dollars);
        double Cent = Convert.ToDouble(Cents);

        amount = Dollar + (Cent / 100);
    }

    //override ToString()
    public override string ToString()
    {
        return string.Format("{0:c}", amount);
    }


}//end class Money

But I have no idea what to put into the IncrementMoney method.
Please help?

and if not too much trouble, maybe a little insight to how it works? I’d really like to know.

Sorry if I didn’t give enough info,

If anything else is required please let me know.

and thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T23:45:45+00:00Added an answer on June 13, 2026 at 11:45 pm

    Since IncrementMoney is supposed to add the two money values together, your usage looks like it should instead be an extension method of the Money type. Which is fine, because extension methods are pretty awesome. A common joke on this site is that they’re the solution to everything.

    Your code should look similar to:

    public static Money IncrementMoney(this Money x, Money y)
    {
       var totalCents = x.Cents + y.Cents;
       var retCents = totalCents / 100;
       var retDollars = x.Dollars + y.Dollars + (totalCents % 100)
       return new Money(retDollars, retCents);
    }
    

    As for a quick explanation…

    By adding this to the first argument, we are telling C# that we want to write an extension method. This will mean that this method we’re writing will hang off the type of the this argument (in this case, Money) and allow us to use it exactly like it was always in the .NET framework. Extension methods have to be static, so you can’t not use that modifier. Beyond that, you just write a method to do whatever you have in mind, exactly like you normally would!

    I would suggest you name it something other than IncrementMoney, though. That’s not very descriptive. AddMoney would be better.

    You may also look into operator overloading, by the way. You could adapt what I just wrote to simply use the + operator to do exactly the same thing, like so:

    public static Money operator +(Money x, Money y)
    {
       var totalCents = x.Cents + y.Cents;
       var retCents = totalCents / 100;
       var retDollars = x.Dollars + y.Dollars + (totalCents % 100)
       return new Money(retDollars, retCents);
    }
    

    With this defined, you can simply go m1 + m2 and add them together.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say, I have a code snippet like this: public static void main(String[] args) {
So far I have this public static void main(String[] args) { try { UIManager.setLookAndFeel(com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel);
I have this code: public static void Main(string[] args) { if (string.IsNullOrEmpty(args[0])) // Warning
So I have this code here : int n; public static void Main(string[] args)
I have this class: public class Test { public static void main(String[] args) {
I have this code: class Program { static void Main(string[] args) { Action whatToDo
I have this code: class Program { static void Main(string[] args) { TestClass instanceOfClass
I have this code: import java.util.Scanner; public class Example { public static void main(String[]
Currently I have this program: namespace EmptySiteCollectionRecycleBin { class Program { static void Main(string[]
I have this code: using System; using System.Runtime.Remoting.Messaging; class Program { static void Main(string[]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.