Return array of doubles from a registered delegate method of other class

StackOverflow https://stackoverflow.com/questions/20980909

  •  25-09-2022
  •  | 
  •  

Domanda

Supossing I have button, when is pressed then It registers methods from other previously created class, and I am interested on result from OtherClassLeftReleasedCb from other method, so:

private void MainClassBtn_Click(object sender, RoutedEventArgs e)
        {
            vtkInteractorObserver istyle = goPreviewer.ImageViewer.GetRenderWindow().GetInteractor().GetInteractorStyle();

            istyle.LeftButtonReleaseEvt += new vtkObject.vtkObjectEventHandler(OtherClass.OtherClassLeftReleasedCb);

        }

the method of other class works fine, however I want to return array like:

public void OtherClassLeftReleasedCb(vtkObject sender, vtkObjectEventArgs e)
        {
            //do stuff
            myresult = new double[4];
            myresult [0] = stuffcomputedbefore[0];
            myresult [1] = stuffcomputedbefore[0] - stuffcomputedbefore[0];
            myresult [2] = stuffcomputedbefore2[1];
            myresult [3] = stuffcomputedbefore2[1] - stuffcomputedbefore[1];
         }

If I change return type to double[] to OtherClassLeftReleasedCb, then I would get error because when doing

istyle.LeftButtonReleaseEvt += new vtkObject.vtkObjectEventHandler(OtherClass.OtherClassLeftReleasedCb);

I am not telling that it should get array od doubles. I do not know how to get myresult from extern method, Is this even possible?

È stato utile?

Soluzione

Rather than having OtherClassLeftReleasedCb conform to the signature of the event that you have, write it using the signature that you want it to be:

public double[] OtherClassLeftReleasedCb()
{
    //do stuff
    myresult = new double[4];
    myresult [0] = stuffcomputedbefore[0];
    myresult [1] = stuffcomputedbefore[0] - stuffcomputedbefore[0];
    myresult [2] = stuffcomputedbefore2[1];
    myresult [3] = stuffcomputedbefore2[1] - stuffcomputedbefore[1];
    return myresult ;
 }

Then have your other class create another method (possibly an anonymous method, if you want) that calls this other method and then possibly does something with the result:

private void MainClassBtn_Click(object sender, RoutedEventArgs e)
{
    vtkInteractorObserver istyle = goPreviewer.ImageViewer.GetRenderWindow()
        .GetInteractor().GetInteractorStyle();

    istyle.LeftButtonReleaseEvt += (s,args) =>
        DoSomethingWithArray(OtherClass.OtherClassLeftReleasedCb());
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top