eBob.com wrote:
I'm not the OP, but thanks Phill W for your very complete response to Alex.
Two questions related to part of your response ...
If an event handler is exclusively for a Button click, could you declare the
first argument as "ByVal sender as Button" instead of "ByVal sender as
Object"?
[Annoyingly] Probably not.
Your method has to "match" the signature of the event delegate method
that you want it "hooked up" to. That sounds clumsy but, here's what I
think the compiler does:
You code:
Sub X_Click( sender as Object, e as EventArgs ) Handles X.Click
The compiler takes this and churns it around into something more like
Sub X_Click( sender as Object, e as EventArgs)
.... and ...
AddHandler X.Click, AddressOf X_Click
.... which, internally, gets churned into ...
AddHandler X.Click, New ClickEventHandler( X_Click )
"ClickEventHandler" is a delegate method that gets called when the event
is raised. Each event can have lots of these delegated methods, which
is how you can have many routines handling a single event.
For this last line to work, though, the signature of "X_Click" has to
"match" the signature of the ClickEventHandler delegate method - just
how tightly the compiler enforces this, I don't know.
Best advice: try it and see! ;-)
Also, if sender is ByVal, are you really changing anything in the
statement ...
DirectCast(sender, Button).Enabled = False
Remember: you're playing with Objects here, not basic, Value Types.
(Damn! I miss not being able to say "Pointer"!)
Whenever you have a variable "containing" an Object, you /don't/
actually have the whole object - you only have a "reference" (a.k.a
"Pointer") to that object; the compiler does all the de-referencing for
you so you don't notice the difference, so when you say
Sub X_Click( Byval sender as Object ...
.... you're passing the /reference/ By Value, not the whole object.
You /can/ change the properties on that object (as in my example) but
what you /can't/ do is assign and return a whole /new/ object, so ...
Sub Button1_Click( Byval sender as Object ...
sender = New Button()
.... /won't/ have any effect on the original button.
My understanding is that you would be changing something in a copy of
'sender' and that the caller would not be aware of any change to a ByVal
object.
Nope. Pass an Object By Value and you can still modify /that instance/
of the object. If you want to "return" a /new/ instance, though, you
have to pass the reference By Reference.
BTW, there's no easy way to "copy" an Object; there internals are so
variable that the framework simply can't do the job automatically; you
have to code a [ICloneable.]Clone method yourself.
HTH,
Phill W.