Dan1676
Dan1676

Reputation: 1725

C# Method Overflow

private void btnMiles_Click(object sender, EventArgs e)
{
    try
    {
        int milesless200 = int.Parse(txtMiles.Text);
        int milesmore200 = int.Parse(txtMiles200.Text);

        MilesCal workingoutmilescost = new MilesCal();
        if (milesless200 > 200)
        {
            lblMilesMorethan200.Text = "You can't enter more then 200 in the first box";
        }
        else
        {
            if (milesmore200 == 0)
            {
                 int carry = workingoutmilescost.MilesRepay(milesless200);
                 lblMilesShow.Text = carry.ToString();

            }
            else
            {
                int carry = workingoutmilescost.MilesRepay(milesless200, milesmore200);
                lblMilesShow.Text = carry.ToString();
            }
        }
        lblMilesError.Text = "No Error";
    }
    catch (FormatException fEx)
    {
        lblMilesError.Text = fEx.Message;
    }
}

My Defined class of MilesCal

 class MilesCal
    {
        public int MilesRepay(int a)
        {
            int x;
            return x = (a*5)/100;
        }
        public int MilesRepay(int a, int b)
        {
            int y;
            return y = (a*5)/100 + (b*2)/100;
        }
    }

This code is suppose to display in a label the cost a driver gets back from their driving at 5p for the first 200 miles then 2p after that. I have got the code working but then discovered that it has to be done using Method overflow. at the minute am getting an error Error 1 Cannot convert method group 'ToString' to non-delegate type 'string'. Did you intend to invoke the method? at

lblMilesShow.Text = carry.ToString;

Added the ().

Now the catch seems to be trippping, can i ask you guys again to work this out because its probably a simple fix again?

Upvotes: 0

Views: 482

Answers (9)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112602

As others have already reported, you have to use parentheses.

The explanation of the error message "Cannot convert method group 'ToString' to non-delegate type" is, that a method name without parentheses designates the method itself and interprets it as a delegate. It does not call the method. If you want to call the method and use its return value, always use the parentheses!

You can think of a delegate as being the memory address of a method. (In reality, it is an object, which encapsulates this address.)

Upvotes: 1

Michael Banzon
Michael Banzon

Reputation: 4967

I assume that by

the catch seems to be tripping

you mean that you catch the exception in the catch-block in the bottom. If this is the case you I assume it can only be a parse exception from one of the int.Parse-calls. You are trying to parse something that is not an int - maybe an empty string??

Upvotes: 1

J. Holmes
J. Holmes

Reputation: 18546

The other answers are all right. .ToString is a method. So using the name of the method without invoking it means you are passing a reference to the method group, not doing the work of the method itself.

carry.ToString is a reference to the function ToString

carry.ToString() invokes the ToString method on the instance carry.


But, really, I think you are going about this all wrong, instead of having two text boxes you should just have the person enter the number of miles they drove. And then have a function that does this work for you:

    public int MilesRepay(int miles)
    {
        return Math.Min(200, miles) * 0.05 + Math.Max(0, miles-200) * 0.02;
    }

Or something like that....

Upvotes: 2

NominSim
NominSim

Reputation: 8511

ToString is not a property, but a method, invoke with brackets

lblMilesShow.Text = carry.ToString();

Upvotes: 1

Jason
Jason

Reputation: 89129

ToString is a method. You need to add parentheses after the name of the method.

lblMilesShow.Text = carry.ToString();

Upvotes: 1

Massimiliano Peluso
Massimiliano Peluso

Reputation: 26737

lblMilesShow.Text = carry.ToString();

Upvotes: 1

Bartosz Wójtowicz
Bartosz Wójtowicz

Reputation: 1401

ToString is a method. You need to use brackets. Try carry.ToString();

Upvotes: 2

Roy Dictus
Roy Dictus

Reputation: 33139

You need to use carry.ToString(), including the parentheses.

Upvotes: 2

rekire
rekire

Reputation: 47965

Try to add brackets

lblMilesShow.Text = carry.ToString();

Upvotes: 4

Related Questions