Click to See Complete Forum and Search --> : Did you ever notice...
Patrick Steele
04-20-2001, 03:13 PM
One of the comparisons made between C# (c/c++/java) and VB
(classic/.NET) is that when declaring variables in a VB environment, you
do:
[varname] As [var_type]
Whereas in C-style, the type goes first:
[var_type] [varname];
Has anyone noticed that the syntax for declaring Events is pretty
similar:
Public Event MouseDroppings(C As Integer)
vs.
public event MouseDroppings(int c);
I assume that's because a VB style of:
Public MouseDroppings(C As Integer) as Event
would look funny. Although it could always be done as:
Public MouseDropping as Event(C As Integer)
Just something that popped into my head while I was looking at some C#
code.
--
Patrick Steele
(psteele@ipdsolution.com)
Lead Software Architect
Image Process Design
Jim Pragit
04-20-2001, 05:36 PM
Hi Patrick,
Actually, I've noticed it, too, and I would prefer:
Public MouseClick As Event
instead of:
Public Event MouseClick
- Jim
Elegance and consistency are *features* I wish VB had.
Patrick Steele <psteele@ipdsolution.com_> wrote:
>
>One of the comparisons made between C# (c/c++/java) and VB
>(classic/.NET) is that when declaring variables in a VB environment, you
>do:
>
>[varname] As [var_type]
>
>Whereas in C-style, the type goes first:
>
>[var_type] [varname];
>
>Has anyone noticed that the syntax for declaring Events is pretty
>similar:
>
>Public Event MouseDroppings(C As Integer)
>
>vs.
>
>public event MouseDroppings(int c);
>
>I assume that's because a VB style of:
>
>Public MouseDroppings(C As Integer) as Event
>
>would look funny. Although it could always be done as:
>
>Public MouseDropping as Event(C As Integer)
>
>Just something that popped into my head while I was looking at some C#
>code.
>
>--
>Patrick Steele
>(psteele@ipdsolution.com)
>Lead Software Architect
>Image Process Design
Patrick Steele
04-20-2001, 05:50 PM
In article <3ae0abd0$1@news.devx.com> (from Jim Pragit
<NoSpam@NoSpam.com>),
>
> Hi Patrick,
>
> Actually, I've noticed it, too, and I would prefer:
>
> Public MouseClick As Event
>
> instead of:
>
> Public Event MouseClick
But where do you put the parameter list for those events that support
parameters?
--
Patrick Steele
(psteele@ipdsolution.com)
Lead Software Architect
Image Process Design
Phil Weber
04-20-2001, 07:02 PM
> But where do you put the parameter list for those
> events that support parameters?
Patrick: An Event declaration could look similar to a VB6 array declaration:
Public ArrayName(x To y) As Type
Public EventName(p1, p2, ..., pn) As Event
That said, I have no objection to the current syntax.
---
Phil Weber
David Bayley
04-20-2001, 07:29 PM
Patrick,
> I assume that's because a VB style of:
>
> Public MouseDroppings(C As Integer) as Event
>
> would look funny.
The existing syntax is consistent IMO. The "Event" modifier is similar to
all the other new member modifiers like Shared, Delegate, Inheritable,
Default, etc.. The "As " part is used exclusively to declare the *type* of
a member.
This can be better justified when you consider that...
Public Event MouseDroppings(C As Integer)
Is just a shortcut for...
Public Delegate MouseDroppingsEventHandler(C As Integer)
Public Event MouseDroppings As MouseDroppingsEventHandler
The MouseDroppings' type is MouseDroppingsEventHandler, rather than "Event".
The Event modifier is used to signify a multi-cast delegate, with the
resulting AddHandler/RemoveHandler support.
It is also theoretically possible to declare...
Public Event MouseDroppings(C As Integer) As Object
Which in turn is a shortcut for...
Public Delegate MouseDroppingsEventHandler(C As Integer) As Object
Public Event MouseDroppings As MouseDroppingsEventHandler
Although, since an event can have many handlers (a multi-cast delegate),
there doesn't seem much point in declaring an event's type.
--
David.
Patrick Steele
04-20-2001, 10:04 PM
In article <3ae0c669@news.devx.com> (from David Bayley
<davidb@aebacus.another.spam.filter.com>),
> Patrick,
>
> The existing syntax is consistent IMO. The "Event" modifier is similar to
> all the other new member modifiers like Shared, Delegate, Inheritable,
> Default, etc.. The "As " part is used exclusively to declare the *type* of
> a member.
Agreed. I didn't think of looking at it as a "modifier", but the way
you explained it makes perfect sense.
>
> This can be better justified when you consider that...
> [snip]
Great examples! Thanks for the enlightening views!
--
Patrick Steele
(psteele@ipdsolution.com)
Lead Software Architect
Image Process Design
Rob Teixeira
04-21-2001, 12:27 AM
I'm actually quite thrilled with the VB syntax support for events, which also
makes CLS-compliance of event/delegates/handlers consistant and easy. It's
also consistant with the older VB event syntax, so the code migration issue
isn't impacted much at all by this.
Either way, it's much better than defining events in IDL for COM!
-Rob
(who looks forward to never touching C++ again)
"David Bayley" <davidb@aebacus.another.spam.filter.com> wrote:
>Patrick,
>
>> I assume that's because a VB style of:
>>
>> Public MouseDroppings(C As Integer) as Event
>>
>> would look funny.
>
>The existing syntax is consistent IMO. The "Event" modifier is similar
to
>all the other new member modifiers like Shared, Delegate, Inheritable,
>Default, etc.. The "As " part is used exclusively to declare the *type*
of
>a member.
>
>This can be better justified when you consider that...
>
> Public Event MouseDroppings(C As Integer)
>
>Is just a shortcut for...
>
> Public Delegate MouseDroppingsEventHandler(C As Integer)
> Public Event MouseDroppings As MouseDroppingsEventHandler
>
>The MouseDroppings' type is MouseDroppingsEventHandler, rather than "Event".
>The Event modifier is used to signify a multi-cast delegate, with the
>resulting AddHandler/RemoveHandler support.
>
>It is also theoretically possible to declare...
>
> Public Event MouseDroppings(C As Integer) As Object
>
>Which in turn is a shortcut for...
>
> Public Delegate MouseDroppingsEventHandler(C As Integer) As Object
> Public Event MouseDroppings As MouseDroppingsEventHandler
>
>Although, since an event can have many handlers (a multi-cast delegate),
>there doesn't seem much point in declaring an event's type.
>
>--
>David.
>
>
>
David Bayley
04-21-2001, 01:44 PM
Patrick,
> Agreed. I didn't think of looking at it as a "modifier", but the way
> you explained it makes perfect sense.
I just read Eric Gunnerson's new MSDN article...
"An Event to Remember"
http://msdn.microsoft.com/voices/csharp04192001.asp
....which does a great job of describing the relationship between Delegates
and Events. C# samples unfortunately, but if you remember that C#'s use of
+=/-= correspond to VB.NET's AddHandler/RemoveHandler statements, everything
can be easily translated.
When I said...
> The Event modifier is used to signify a multi-cast delegate, with the
> resulting AddHandler/RemoveHandler support.
I wasn't very accurate. It seems that *all* delegates are multi-cast. The
difference between...
Public MouseDroppings As MouseDroppingsHandler
and...
Public Event MouseDroppings As MouseDroppingsHandler
is that the Event modifier makes the MouseDroppings delegate private, whilst
providing public add/remove methods. It is in effect a shortcut for...
Private mouseDroppings As MouseDroppingsHandler
Public Sub add_MouseDroppings(handler As MouseDroppingsHandler)
mouseDroppings = Delegate.Combine(mouseDroppings, handler)
'- (required cast ommitted for clarity)
End Sub
Public Sub remove_MouseDroppings(handler As MouseDroppingsHandler)
mouseDroppings = Delegate.Remove(mouseDroppings, handler)
End Sub
Gotta' love those shortcuts <g>. AddHandler/RemoveHandler then get
translated into corresponding calls to the add_X/remove_X methods.
I believe that C# will be getting syntax to hook into those add_X/remove_X
methods in Beta2, using...
public event MouseDroppingsHandler MouseDroppings {
add {...}
remove {...}
}
I would hope that VB.NET gets similar abilities, the natural syntax being...
Public X Event MouseDroppings As MouseDroppingsHandler
Add
...
End Add
Remove
...
End Remove
End Event
Can't think of a decent keyword to use for 'X', but something will be needed
to distinguish it from the one-liner version.
--
David.
devx.com
Copyright Internet.com Inc. All Rights Reserved