Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass value/parameter into a "public delegate void"?

Please help me with this. Let's say I have this code here...

    void IScanSuccessCallback.barcodeDetected(MWResult result)
    {
        if (result != null)
        {
            try
            {
                var scan = Element as BarcodeScannerModal;

                if (scan == null)
                    return;
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
        }
    }

...and I want that value of MWResult result be passed to this one...

[System.Runtime.InteropServices.ComVisible(true)]
        public delegate void EventScanHandler(MWResult result);

I'm really having trouble with this one.

like image 962
JHON CARL IGNORO Avatar asked Apr 27 '26 05:04

JHON CARL IGNORO


2 Answers

How a basic delegate void is used. A delegate void is a method on where an object or a void is referenced. Its almost like a place holder if you have two of the same function and you need to switch the function, not so much the numbers (swith the + in 5+5 into a *, like 5*5, the example below shows how to do this. It doesent also have to be an int as a return! You can also swith it out for a void in order to do functions). In your case you want to scan the bar-code then check if it is null so I'm confused on why you would want to use a delegate void.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    //delegates are mostly defined outside of the class
    public delegate int addn(int num1, int num2);

    //the main class

    class Program
    {
        //delegate functions
        public static int add1(int num1, int num2)
        {
            return num1 + num2;
        }

        public static int add2(int num1, int num2)
        {
            return num1 * num2;
        }

        //entry
        public static void Main()
        {
            //here we can reference delegates as a class. its
            //called addf in this case.

            //first we init add1
            addn addf = new addn(add1);
            Console.WriteLine(addf(5, 5));

            //then we innit add2. note we dont add /*addn*/ in the beginning because
            //its already defined
            addf = new addn(add2);
            Console.WriteLine(addf(5, 5));

            //loop

            while (true) ;
        }
    }
}

Unless you are using different versions of IScanSuccessCallback.barcodeDetected I would just call it directly, store the value in a class, then use [System.Runtime.InteropServices.ComVisible(true)] public delegate void EventScanHandler(Class name result);

like image 132
SM GamingCoder Avatar answered Apr 29 '26 18:04

SM GamingCoder


A delegate is a type that represents references to methods.

ScanSuccessCallbackImpl t = new ScanSuccessCallbackImpl();
EventScanHandler method = t.barcodeDetected;
method(new MWResult());

Does this answers your question?

like image 28
Reno Avatar answered Apr 29 '26 17:04

Reno



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!