By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,200 Members | 1,568 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,200 IT Pros & Developers. It's quick & easy.

Problem with deriving System.EventArgs

P: n/a
Hello,

I am failing to understand why I get an invalid cast exception with
this very simple code.

I have derived the EventArgs class as follow:

public class StringArgs : System.EventArgs
{
public string Msg;
public StringArgs(string aMsg)
{
Msg = aMsg;
}
}
and I fire the event as shown below

private void EventLog(string Msg)
{
System.EventArgs e = new StringArgs(Msg);
object[] pList = { this, e };

BeginInvoke(new System.EventHandler(UpdateRTBLog), pList);
}
now when I receive the event in this method

private void UpdateRTBLog(object o, System.EventArgs e)
{
StringArgs se = (StringArgs) e;
RTBLog.Text = se.Msg;
}
I get during runtime an Invalid cast error:
"System.InvalidCastException: Specified cast is not valid." on
StringArgs se = (StringArgs) e;
Please would you be kind enough to tell me why this is happening and
how to fix this?

Many thanks for your help.

Michael

--

----------------------------------------------

http://michael.moreno.free.fr/

Nov 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Michael Moreno schrieb:
Hello,

I am failing to understand why I get an invalid cast exception with this
very simple code.

I have derived the EventArgs class as follow:

public class StringArgs : System.EventArgs
{
public string Msg;
public StringArgs(string aMsg)
{
Msg = aMsg;
}
}
and I fire the event as shown below

private void EventLog(string Msg)
{
System.EventArgs e = new StringArgs(Msg);
object[] pList = { this, e };

BeginInvoke(new System.EventHandler(UpdateRTBLog), pList);
}
now when I receive the event in this method

private void UpdateRTBLog(object o, System.EventArgs e)
{
StringArgs se = (StringArgs) e;
RTBLog.Text = se.Msg;
}
I get during runtime an Invalid cast error:
"System.InvalidCastException: Specified cast is not valid." on
StringArgs se = (StringArgs) e;
Please would you be kind enough to tell me why this is happening and how
to fix this?

Many thanks for your help.

Michael


Hi,

is your "System.EventArgs e" in your Event-Handler "UpdateRTBLog" not
the array of objects named pList? If so, I think you can't convert it to
StringArgs but not sure about the syntax of BeginInvoke. But why do
you use BeginInvoke?

Yours sincerely

Andy
Nov 17 '05 #2

P: n/a
> is your "System.EventArgs e" in your Event-Handler "UpdateRTBLog" not the

Well, for some reasons it seems that the event I receive is empty which
does not make any sense at all.
array of objects named pList? If so, I think you can't convert it to
StringArgs but not sure about the syntax of BeginInvoke. But why do you use
BeginInvoke?


I have this sequence of event:

External Process fire an event to an object to which I have a reference
to in my process using a TCP channel.

In the "listener" process I get the event and fire an event to the Main
Form with a string in it. In the main form, if I try to display this
string into an EditBox, the process freezes. So I did a few test and it
looks like that I receive the event in a different thread and not in
the main thread which is hard to understand given that I have not
created any worker thread at all yet. So I assumed that "magically" I
was receiving this event in a worker thread and that was part of the
..Net Remoting behaviour (I did not find any documentation to confirm
this though). In the main form I got a kind of a confirmation for this
because I checked whether a call to BeginInvoke was required using
InvokeRequired and it returns TRUE !
Many thanks for your help.

Michael

--
----
http://michael.moreno.free.fr/

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.