How Do I pass a @SimpleEvent name as a parameter to a private Function?

I have a simple function, an event that returns the output, and a private function that runs the code.

I would like to pass the name of the event using the simple function (which calls the private function), so that the private function calls the event. easy enough if it was just strings, but because it is a method it will not work.

@SimpleEvent(description = "Event raised when output returned")
  public void GotOutput(final String output) {
    EventDispatcher.dispatchEvent(this, "GotOutput", output);
  }

@SimpleFunction(description = "set parameters to get output")
  public void GetOutput() {
      String parameters = "some parameters";
      GetFunction(parameters, <GotOutput>);
  }

private void GetFunction(String parameters, <event parameter (GotOutput) here>) {
//do something
//output to event:
<event parameter (GotOutput)>(output);
}

In above, the event GotOutput. I want to send GotOutput using the function GetOutput to the function GetFunction, so that it outputs to GotOutput.

It all works if I hard code GotOutput to GetFunction, but i want to be able to apply many simple functions to GetFunction to keep things DRY

Hope that makes sense...:wink:

1 Like

Did you try the return statement? did it work for you?

Where would I put this in my code ?

you may put it here, and if it does not work for you, then reply me

I presume your mean here:

return <event parameter (GotOutput)>(output);

A method call is expected, using return just adds to the errors in the code.

Why would you return something in a void function? It should be something else.

Did you try this?

GotOutput(output);

Or do you mean something else?

Yes, that works, but I want to apply many different functions/events to GetFunction.

not sure if this works, but do you mean parameters(output) ?

It is confusing, what does the private function do?

1 Like

I guess you have to hard code it using if (something == something) { GetSomething(output);}?

1 Like

As previously indicated, I need to be able to pass a method. As I understand this is possible using a Runnable....

Ok, are you trying to mean that you want to send the parameter(s) to GetFunction void?

It makes an http call, but this is mostly irrelevant to the initial question of how to pass a method as a parameter

Oh I understand, so you want to be able to take a method, that you would call later right?

3 Likes

try GetOutput(output);

i am confused this time

In here, the event name is unknown. For example, in App Inventor, you can track the user input, which is strings, and open another screen with that string because App Inventor opening screens accept strings. But in Java, they cannot do something like

"GetOutput"(output);

and it must be

GetOutput(output);

. Probably this.

1 Like

Here I have simplified to working code. You can see GotOutput is hard coded in GetFunction and will therefore run the event. But i will have many simpleevents and simplefunctions, so i want to be able to substitute GotOutput for whatever simpleevent is needed for the desired simplefunction. All simplefunctions will call GetFunction.

@SimpleEvent(description = "Event raised when output returned")
  public void GotOutput(final String output) {
    EventDispatcher.dispatchEvent(this, "GotOutput", output);
  }

@SimpleFunction(description = "set parameters to get output")
  public void GetOutput() {
      String parameters = "some parameters";
      GetFunction(parameters);
  }

private void GetFunction(String parameters) {
//do something
//output to event:
GotOutput(output);
}
1 Like

only have one GotOutput event, which will fire for all those many simple functions
Taifun

2 Likes

Yes, I have done this, but it generates work elsewhere to identify which simplefunction was called. Could be the easiest route though :slight_smile:

1 Like