473,654 Members | 3,038 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inheriting from EventArgs

Two books, "Mastering Visual Basic.Net" and "Visual Basic.Net Developer's
Handbook" describe inheriting from System.EventArg s using a class similar
to:

Public Class MyEventArgs
Inherits System.EventArg s
Public Shared MyField As String
Public Sub New(Text As String)
Me.MyField = Text
End Sub
End Class

and using this in an event of the form:

Public Event MyEvent(sender As Object, e as MyEventArgs)

Using this technique works flawlessly in a VB.Net multi-project solution. VB
event handlers have no problem in accessing the static field. Unfortunately,
if this event is raised in a VB.Net component that is placed on a form in a
C# project, the C# delegate attached to the event does not see the MyField
item. My hunch is that the authors of these books use the assumption that
the MSDN documentation for the System.EventArg s class shows the public
fields (Empty) as a Public Shared (static declaration) so the assumption is
to declare all other fields that way. Just a hunch. The MSDN documentation
for the EventArgs class adds this comment, "Any public static (Shared in
Visual Basic) members of this type are thread safe. Any instance members are
not guaranteed to be thread safe."

Other references in MSDN that illustrate inheriting from EventArgs use a
private variable along with a public property (get:set) to access the
variable in question - similar to the following:

Public Class MyEventArgs
Inherits System.EventArg s
Dim MyField As String
Public Sub New(Text As String)
Me.MyField = Text
End Sub
Public Property TheField As String
Get
Return Me.MyField
End Get
Set (ByVal Value As String)
me.MyField = Value
End Set
End Class

There is absolutely no problem accessing the TheField property in the event
delegate from a C# form.

I have searched for a few days for some clues as to why the first form of
inheritance is suggested that does not surface the static members in C#.Net
but does in VB.Net, whereas the MSDN reference to inheritance uses
properties. Can anyone shed some light on the pros/cons of these two
techniques? Does this have to do with the comment in MSDN documentation
about threading safety?
Nov 21 '05 #1
3 4067
"Roy Soltoff" <ra************ *@community.nos pam> schrieb:
Two books, "Mastering Visual Basic.Net" and "Visual Basic.Net Developer's
Handbook" describe inheriting from System.EventArg s using a class similar
to:

Public Class MyEventArgs
Inherits System.EventArg s
Public Shared MyField As String
Public Sub New(Text As String)
Me.MyField = Text
End Sub
End Class


Using a shared member doesn't make much sense here. You'd better use an
instance member, or even better, a property:

\\\
Public Class MyEventArgs
Inherits EventArgs

Private m_MyField As String

Public Property MyField() As String
Get
Return m_MyField
End Get
Set(ByVal Value As String)
m_MyField = Value
End Set
End Property

Public Sub New(ByVal MyField As String)
Me.MyField = MyField
End Sub
End Class
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #2
"Roy Soltoff" <ra************ *@community.nos pam> wrote in message
news:OC******** *****@TK2MSFTNG P12.phx.gbl...

Public Class MyEventArgs
Inherits System.EventArg s
Public Shared MyField As String
Public Sub New(Text As String)
Me.MyField = Text
End Sub
End Class
Why would MyField be /Shared/? Wouldn't you rather have each
instance of this class hold its /own/ value for this field? Anyway ...
Unfortunately, if this event is raised in a VB.Net component that is
placed on a form in a C# project, the C# delegate attached to the
event does not see the MyField item.


What is the signature of the delegate method?
It may try to use (the default) "MyEventArg s e", which will compile
perfectly well and even run (because MyEventArgs is derived from
EventArgs), but /won't/ be able to see the fields you've defined in
your SubType.

In the C# project, try "downCastin g" the argument to the correct
type, something like:

MsgBox( ( (MyEventArgs)e ).MyField )

HTH,
Phill W.
Nov 21 '05 #3

Roy Soltoff wrote:
event handlers have no problem in accessing the static field. Unfortunately,
if this event is raised in a VB.Net component that is placed on a form in a
C# project, the C# delegate attached to the event does not see the MyField
item. My hunch is that the authors of these books use the assumption that


Unlike VB.Net, C# does not allow you to access shared (static) members
from an instance of the class. You must use the class name.
Therefore, declaring your field as static is the reason you cannot see
it in C#.

Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3363
by: Peter Cresswell | last post by:
Hi guys/gals, I'm using VS.net 2003 and having trouble with the following: I am trying to inherit a base class into a web form, but VS will then not allow me to add any ASP.NET controls using the designer. The base class does derive from System.Web.UI.Page. Here's a code snippet:
1
3378
by: bminder | last post by:
In the asp.net pages below, Common.vb has an overridable Page_Load sub. In the consuming page, Two.aspx, the Page_Load sub is inherited, but for some reason it (Overrides Sub Page_Load) executes twice. In another "regular" asp.net page that inherits only from its own codebehind page, the page load only executes once as you'd expect. Any ideas why the page_load fires twice in the former example? Thanks,
3
2366
by: Geraldine Hobley | last post by:
Hello, In my project I am inheriting several forms. However when I inherit from a form and add additional subroutines and methods to my inherited form I get all sorts of problems. e.g. I sometimes get MyVarialble is not declared errors when the variable is quite clearly declared, when I change it to public and then back again to private the error goes away!!! Also I get lots of member not found errors, these however don't stop me from...
5
2992
by: Andrew Robinson | last post by:
I am attempting to better automate a Pager Template within a GridView. I am succesfully skinning a Drop Down List withing my control (the DDL is added to my control). I correctly populate the item list that corresponds with the number of pages, but I am unable to wire up the Selected Index Changed event. Auto PostBack is set to true and the page is posting back when the DDL is selected / chaged, but the event is never being called. Any...
0
8379
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8709
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8596
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7309
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5627
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4297
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2719
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1924
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1597
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.