Click here to Skip to main content
15,904,926 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello;
I want to refactor this method by extracting:
InfoOrdreDirect_5013 directOrder = new InfoOrdreDirect_5013();
directOrder.put_AIS(0, e_Type_Lecteur_CB.Type_Lecteur_CB_Satellite);
as a parameter, so I started with a delegate but it does not work
C#
        /// Autozero Methode
        /// 
        private void AutozeroPosition()
        {
            InfoOrdreDirect_5013 directOrder = new InfoOrdreDirect_5013();
            directOrder.put_AIS(0, e_Type_Lecteur_CB.Type_Lecteur_CB_Satellite);
            IInfoReponse_CRO reponseCro = null;

            try
            {
                reponseCro = this._espCom.SendCmd(directOrder);
                if (reponseCro != null && true/*== e_Succes_Echec.Succes_Echec_Succes*/)
                {

                }
               
            }
            catch (Exception ex)
            {
                Thread.Sleep(1000);
                throw new System.Runtime.InteropServices.COMException(ex.Message);
            }
        }



My refactoring:
private delegate IInfoOrdreDirect InfoOrderDelegate(int a, Enum pegase);
        //public void InvokeInfoOrderDelegate(Delegate method, object[] param)
        //{
        //   method.DynamicInvoke(param);
        //}


        /// <summary>
        /// Autozero Methode
        /// </summary>
        private void AutozeroPosition(InfoOrdreDirect info, Delegate method, object[] param)
        {
            //InfoOrdreDirect_5013 directOrder = new InfoOrdreDirect_5013();
            //info.put_AIS(0, e_Type_Lecteur_CB.Type_Lecteur_CB_Satellite);
            method.DynamicInvoke(param);
            object infoReponseCro;
            try
            {
                infoReponseCro = _espCom.SendCmd(info) as InfoReponse_CRO;
                if (infoReponseCro != null && true/*== e_Succes_Echec.Succes_Echec_Succes*/)
                {

                }
            }
            catch (Exception ex)
            {
                Thread.Sleep(1000);
                throw new System.Runtime.InteropServices.COMException(ex.Message);
            }


        }

My invoking:

C#
InfoOrdreDirect_5013 info = new InfoOrdreDirect_5013();

           AutozeroPosition(info, new InfoOrderDelegate(info.put_AIS),new object[] { 0, e_Type_Lecteur_CB.Type_Lecteur_CB_Satellite });

My error:
On put_ais:
Expected a method with infoOrderDirect put_ais(int, enum) signature.

Any Idea how to refactor the AutozeroPosition() ?
Thanks.

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 29-Nov-12 23:30pm
v2

C#
public class Class1
{
    public int Method1(string input)
    {
        //... do something
        return 0;
    }

    public int Method2(string input)
    {
        //... do something different
        return 1;
    }

    public bool RunTheMethod(Func<string,> myMethodName)
    {
        //... do stuff
        int i = myMethodName("My String");
        //... do more stuff
        return true;
    }

    public bool Test()
    {
        return RunTheMethod(Method1);
    }
}
 
Share this answer
 
The problem was a simple casting problem from int32 to int 16, I added (short)0 casting.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900