Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing dynamically casted instance to overloaded method c#
    primarykey
    data
    text
    <p>I have c# application. I have 2 different classes (<strong>Step1.State</strong> and <strong>Step2.State</strong>) (they implement same interface called <strong>IMDP.IState</strong>) which represents application state in 2 different steps of execution of my application. I have 2 overloaded methods to update application state.</p> <p>I have following code in one of my method:</p> <pre><code>Step1.State s1s = null; Step2.State s2s = null; </code></pre> <p>Then I am casting current state depending on its type. <strong>currentState</strong> is instance of implemented interface which is received as parameter. First I am storing it in a <strong>dynamic</strong> type.</p> <pre><code> //currentState is instance of IMDP.IState interface which is //implemented by the two states dynamic curState=currentState; if(curState is Step1.State) { s1s = (Step1.State)currentState; curState = s1s; } else if (curState is Step2.State) { s2as = (Step2.State)currentState; curState = s2s; } </code></pre> <p>Then I am calling my overloaded method inside the same method.</p> <pre><code> currentState = myAgent.UserStateUpdate(prevAction, curState, e.Result); </code></pre> <p><strong>UserStateUpdate</strong> method has 2 overloaded versions. First one gets <strong>Step1.State</strong> and second one gets <strong>Step2.State</strong> as differentiating parameter.. Like following;</p> <pre><code>IMDP.IState UserStateUpdate(IMDP.IAction act, Step1.State st, RecogResult rr) IMDP.IState UserStateUpdate(Step2.Abuse.Action act, Step2.State st, RecogResult rr) </code></pre> <p>Application calls correct method for <strong>Step1.State</strong> but when application move to Step2 which uses <strong>Step2.State</strong> for state representation. It throws the following exception. Note, when i check what is stored in <strong>curState</strong> (typed dynamic) before calling the overloaded method, I am seeing a correct state which is typed <strong>Step2.State</strong>.</p> <p>"<em>The best overloaded method match for 'BI4A.Agent.Agent.UserStateUpdate(BI4A.IMDP.IAction, BI4A.Step1.State, System.Speech.Recognition.RecognitionResult)' has some invalid arguments</em>"</p> <p>Which basically states that the system tries to call overloaded method which accept Step1.State instead of Step2.State. I could not figure out, how to make it call the right method. Thanks for any help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload