I would like to know how to pass object array (object[]) between classes with the code bellow, which now sends string only:

public delegate void MessageDelegate(Object sender, MessageEventArgs e);

  public class MessageEventArgs : EventArgs
  {
    public string Message;
    public MessageEventArgs(string msg)
    {
      this.Message = msg;
    }
  }
  public interface IController
  {
    event MessageDelegate ControllerEvent;
    void InvokeControllerEvent(string msg);
  }
  public class Controller : IController
  {
    public event MessageDelegate ControllerEvent;

    public void InvokeControllerEvent(string msg)
    {
      this.OnControllerEvent(new MessageEventArgs(msg));
    }

    protected virtual void OnControllerEvent(MessageEventArgs mea)
    {
      if (this.ControllerEvent != null)
      {
        this.ControllerEvent.Invoke(this, mea);
      }
    }
  }

Recommended Answers

All 2 Replies

I would like to know how to pass object array (object[]) between classes with the code bellow, which now sends string only:

static void Sample(object[] ar)
        {
            if(ar is string[])
                Console.WriteLine( ((string[])ar)[0]);
        }
        static void Sample1(Array ar)
        {
            if (ar is string[])
                Console.WriteLine(((string[])ar)[0]);
        }
        static void Main(string[] args)
        {
            string[] ar = { "AA", "BB" };
            Sample(ar);
            Sample1(ar);
        }

As long as it is within you own app and not a remote app or other process you can just add an object[] as an additional parameter to your MessageEventArgs .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.