-3
private void blokace(object sender, EventArgs e) 
{
// ... 
}

Then using it in an if like this

if (blokace)

But I get a compiler error, "Cannot convert method group 'blokace' to non-delegate type 'bool'. Did you intend to invoke this method?"

Can someone help me please? It always did it like that and it worked, I don't know, what's the problem now.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
tnXe
  • 1
  • 2
  • I've taken the relevant code from your image and added it to your question. Feel free to revert my edit if what I left isn't your intent. – jdphenix Dec 03 '16 at 21:12

2 Answers2

2

You just can not do

if(blokace)

because blokace is not a boolean expression, is a method

you could do

if(blokace())

if and only if the method returns a boolean value, but in your case blokace is returning nothing (void)

furthermore

blokace looks like a eventMethod so is something you normally dont invoke but wait for it to be invoke e.g. a button is pressed, a socket is connected, a list has changed etc

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

When you use an if statement, the result should be a boolean (true, false). Your method returns a void. You are basically saying if (void) which does not evaluate to true or false.

Perhaps you should return a true or false from your method and then it will work. Or you can return something else from your method and then do a check on the value returned. For example:

private string Blockace(object sender, EventArgs e)
{
    // do some work
    return "yes";
}

Then you can check against the returned value. But you will need to send the 2 arguments as well: sender and e

if (Blockace(sender, e) == "yes")
{
   // do whatever you need
}

I can see from blockace method signature it is an event handler. You need to reuse the code inside it from UI control (click or something) but you also need to use it from your code. Therefore create a method and put the common code there. Then you can call the method containing the common code from the event handler and also from elsewhere.

CodingYoshi
  • 25,467
  • 4
  • 62
  • 64