0
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?

6
  • @DavidHeffernan Method overloading. Commented Jan 9, 2012 at 17:18
  • 2
    The ToString is not a property its a method hence should be ToString(); You are missing the opening and closing braces. Commented Jan 9, 2012 at 17:18
  • 3
    Darn. I clicked on this question thinking I was going to learn about some programming concept that I'd never heard of before. Commented Jan 9, 2012 at 17:20
  • @ean5533 you and me both man. Commented Jan 9, 2012 at 17:25
  • 1
    @Dan1676, did you edit the question wit the "catch seems to be trippping[sic]" part? You're going to have to add more details, because I for one have no idea what that means. Better yet, ask a new question, since people have already answered for your first problem. Commented Jan 9, 2012 at 17:37

9 Answers 9

4

Try to add brackets

lblMilesShow.Text = carry.ToString();
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

2

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

Comments

2

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....

1 Comment

oh... its homework. that or you have the worst employer ever.
1
lblMilesShow.Text = carry.ToString();

Comments

1

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

lblMilesShow.Text = carry.ToString();

Comments

1

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

lblMilesShow.Text = carry.ToString();

Comments

1

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??

Comments

1

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.)

Comments

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.