Power-Mosfet Power-Mosfet - 1 year ago 34
C# Question

String to event

I'm trying to programmatically call a function with event.

How to convert string to a event in general? My problem is actually not knowing How to do this?

How to convert str to event?

str = "test1";

// UserControlsBackgroundEventArgs = EventArgs
EventArgs arg = (EventArgs)str; --> ?
UserControlsBackgroundOutput(str);



//function
private string CLICKNAME = "test0";
private void UserControlsBackgroundOutput(EventArgs e)
{
if (CLICKNAME == e.output)
return;

if (e.output == "test1"){}
}


Error solved:
I had to do

UserControlsBackgroundEventArgs arg = new UserControlsBackgroundEventArgs(CLICKNAME);


instead of

UserControlsBackgroundEventArgs arg = new (UserControlsBackgroundEventArgs)(CLICKNAME);

Answer Source

i've written a code that mimic you code, hopefully you will find it useful:

public class UserControlsBackgroundEventArgs
{
  public string output;

  public UserControlsBackgroundEventArgs(string up)
  {
     output = up;
  }
}

public delegate void UserControlsBackgroundOutputHandle(UserControlsBackgroundEventArgs e);

public class testEvent
{
  public event UserControlsBackgroundOutputHandle UserControlsBackgroundOutput;

  public void DoSomeThings()
  {
     // do some things

     if (UserControlsBackgroundOutput != null)
     {
        string str = "test1";

        UserControlsBackgroundEventArgs arg = new UserControlsBackgroundEventArgs(str);
        UserControlsBackgroundOutput(arg); // you've done that with str, whitch makes me
                                          // you don't know what the event param is
     }
  }
}

public class test
{
  private testEvent myTest;
  private const string CLICKNAME = "whatever"; // i don't know what you want here

  public test()
  {
     myTest = new testEvent();
     myTest.UserControlsBackgroundOutput += UserControlsBackgroundOutput;
  }

  void UserControlsBackgroundOutput(UserControlsBackgroundEventArgs e)
  {
     if (CLICKNAME == e.output)
        return;

     if (e.output == "test1")
     {
     }
  }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download