Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# method group to bool?

Tags:

methods

c#

I have created some code, but I can't find what's wrong with it. Here's the code:

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (MyMethod) //<==underlines this and gives the error
        {
            //code
        }
        else
        {
            //code
        }
     }

    public bool MyMethod()
    {
        if (Form1.f >= 0.001)
            return true;
        else
            return false;
    }

The error message I get:

Cannot convert method group 'MyMethod' to non-delegate type 'bool'. Did you intend to invoke the method?

Can someone help me with this problem? I can't seem to get it right, I've tried several things... I have also tried to change it to string and the return values to strings but that gives the same error (except the bool, that's changed into string). Thanks in advance!

like image 592
MieskeB Avatar asked May 01 '26 23:05

MieskeB


1 Answers

MyMethod is not a field or property but a method. A method must be called with ():

if (MyMethod()){ ... }

You could make it a property, then you don't need them, f.e. as expression-body:

public bool MyMethod => Form1.f >= 0.001;

Now you can use (choose more meaningful names):

if (MyMethod) { ... }
like image 70
Tim Schmelter Avatar answered May 04 '26 13:05

Tim Schmelter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!