473,383 Members | 1,737 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,383 software developers and data experts.

Inheriting from EventArgs

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

Public Class MyEventArgs
Inherits System.EventArgs
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.EventArgs 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.EventArgs
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 4045
"Roy Soltoff" <ra*************@community.nospam> schrieb:
Two books, "Mastering Visual Basic.Net" and "Visual Basic.Net Developer's
Handbook" describe inheriting from System.EventArgs using a class similar
to:

Public Class MyEventArgs
Inherits System.EventArgs
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.nospam> wrote in message
news:OC*************@TK2MSFTNGP12.phx.gbl...

Public Class MyEventArgs
Inherits System.EventArgs
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) "MyEventArgs 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 "downCasting" 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
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...
1
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...
3
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...
5
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.