0

I need help to use the below piece of code to call Method1. There are no compilation issues but while running the code Method1 is not invoked. I searched the net a lot but didn't find a solution.

public static class Test12

{

public static Test12.ByteDelegate PropertyValue { get; set; }

public delegate byte[] ByteDelegate(byte p1, byte[] p2);        

}

The above class has to be used like this:

class Abc

{

internal void Stat()        
{              
    Test12.Propertyvalue = Method1;                       
}
private byte[] Method1(byte p1, byte[] p2)
{
    byte[] abc = ...;
    return abc;
}
}

If I am creating an instance of the delegate in stat method and using it like:

Method1(param1,param2);

Then the Method1 in invoked, but if I use it like this:

Test12.PropertyValue = Method1(param1,param2);

compiler is throwing an error saying missing typecast. Can anybody please tell me how to invoke Method1 using Test12.PropertyValue = Method1;?

2
  • It would really help if you'd follow normal .NET naming conventions, format your code more readably, and provide a minimal reproducible example rather than bits and pieces.
    – Jon Skeet
    Jul 6, 2016 at 19:31
  • But fundamentally, if you call the method, that will return a byte[]... and your property isn't of type byte[].
    – Jon Skeet
    Jul 6, 2016 at 19:32

1 Answer 1

0

You receive an error message because when you try to assign the method to the delegate you are using the following line of code:

 test12.propertyvalue = method1(param1, param2);

Which firstly calls the method1 and then what it returns it's trying to assign to the delegate, and the error message occurs saying that you can't assign a byte array to a delegate, as @Jon said in the comment. They are different types so you can't assign one to another, at least not without a cast or a conversion method.

In order to invoke the method1 from the delegate, after you do the assignation like this:

 test12.propertyvalue = method1;

you can simply call the delegate, as it would be your method:

 test12.propertyvalue(param1 , param2);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.