473,395 Members | 1,556 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Problem with deriving System.EventArgs

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
2 2036
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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mubashir Khan | last post by:
I am deriving a class from TextBox to implement a NumericBox. I am overriding OnGotFocus and OnLostFocus methods but On GotFocus is causing problems. Actually the commented lines are causing...
1
by: Rhy Mednick | last post by:
I'm creating a custom control (inherited from UserControl) that is displayed by other controls on the form. I would like for the control to disappear when the user clicks outside my control the...
5
by: Mahesh Devjibhai Dhola | last post by:
Hi All, I want to make a custom class in c#, which extends System.Xml.XmlNode class of BCL. Now in custom class, I have implement abstract methods of XmlNode class also. Now when I am trying to...
1
by: Kris van der Mast | last post by:
Hi, been a while since I posted a question myself instead of trying to help others out. I'm refactoring an existing web app that uses dynamic loading of user controls and a lot of...
3
by: Lisa Calla | last post by:
Hi, I'm trying to control the foreground and background colors of a textbox. I hate that light gray foreground when the textbox.enabled=false. I've tried to derive a control from the textbox,...
4
by: zubair | last post by:
Hello everyone! I have uploaded my site on a webserver. Some times it works fine but some time it gives error "Null Object reference exception". The same site on my local server works just fine. ...
1
by: David | last post by:
Help!!! // // Contents of ValueNode.cs // using System; using Microsoft.Web.UI.WebControls;
0
by: SpaceMarine | last post by:
hello, i am working on a web application w/ UserControls. some of my controls use the same functionality (an Event they all raise) so it seemed sensible to place them into a custom base class,...
0
by: sandeepkavade | last post by:
Hi, I am basically a VC++ developer but now working on C#. I am having a assembly in that i have added a UserControl and on that UserControl I am having a tree-view control and Property Grid...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.