Reputation: 317
It's for my irc bot, and I am trying to change the message receiver event to be linked to the method in my other class.
private static void client_Connected(object sender, EventArgs e)
{
gamebot.LocalUser.JoinedChannel += LocalUser_JoinedChannel;
gamebot.LocalUser.MessageReceived += LocalUser_MessageReceived;
}
// private static void newmessage(object sender, IrcChannelEventArgs e)
// {
// e.Channel.MessageReceived += Hangman.MessageReceivedHangman;
// }
private static void LocalUser_JoinedChannel(object sender, IrcChannelEventArgs e)
{
e.Channel.MessageReceived += Channel_MessageReceived;
Console.WriteLine("Joined " + e.Channel + "\n");
}
Just not sure how to get the channeleventargs outside of a method, so I can change the event. The commented method shows sort of what i need.
public static void MessageReceivedHangman(object sender, IrcMessageEventArgs e)
{
That is the method in a different class i would like to have execute when a message is received.
Upvotes: 0
Views: 110
Reputation: 70701
It's hard to know what would be best here, as you have provided so little context. All we really know is that you have one class (call it class A
) handling specific events, and another class (call it class B
) that wants to be able to handle events the first class already knows about.
Based on that, there are at least a couple of possibilities that might work for you.
Option #1:
Expose the "joined" event so that the second class can receive the same notifications and subscribe to the channel's event:
class JoinedChannelEventArgs : EventArgs
{
public Channel Channel { get; private set; }
public JoinedChannelEventArgs(Channel channel) { Channel = channel; }
}
class A
{
public static event EventHandler<JoinedChannelEventArgs> JoinedChannel;
private static void LocalUser_JoinedChannel(object sender, IrcChannelEventArgs e)
{
e.Channel.MessageReceived += Channel_MessageReceived;
Console.WriteLine("Joined " + e.Channel + "\n");
EventHandler<JoinedChannelEventArgs> handler = JoinedChannel;
if (handler != null)
{
handler(null, new JoinedChannelEventArgs(e.Channel);
}
}
}
class B
{
static void SomeMethod()
{
A.JoinedChannel += A_JoinedChannel;
}
private static void A_JoinedChannel(object sender, JoinedChannelEventArgs e)
{
e.Channel += MessageReceivedHangman;
}
}
Option #2:
Expose the "message received" event instead:
class A
{
public static event EventHandler<IrcMessageEventArgs> AnyChannelMessageReceived;
public static void Channel_MessageReceived(object sender, IrcMessageEventArgs e)
{
// Whatever other code you had here, would remain
EventHandler<IrcMessageEventArgs> handler = AnyChannelMessageReceived;
if (handler != null)
{
handler(null, e);
}
}
}
class B
{
static void SomeMethod()
{
A.AnyChannelMessageReceived += MessageReceivedHangman;
}
}
It's not clear from your post whether having the sender of the original event is important. If it is, then IMHO Option #1
is better, as it provides direct access to the event. However, you could modify Option #2
so that it passed sender
to the handler (in Channel_MessageReceived()
), instead of the null
that's in the example (the null
is more idiomatic for a static event
, but not mandatory).
Upvotes: 0