"ByVal sender As Object" hmmm, I've so much to learn....

M

Maurice Walmsley

I'll avoid tell you how lame I am and get straight to the question...

I need a good expanation to the "ByVal sender As Object, ByVal e As
System.EventArgs" part of;
Public Sub button01_click(ByVal sender As Object, ByVal e As
System.EventArgs)

I dont understand any of that part. ByVal? sender? e?
System.EventArgs? Do you know of any good references explaining what
this stuff is? I'm sure it's different for each routine, and I
obviously need to learn...

cheers and thanks

m
 
F

Frank Drebin

First, ByVal and ByRef mean "by value" and "by reference". Since everything
you do on a computer is in memory, something things just want a "pointer" to
the memory location where you data is. Some want the actual data itself.
There are advantages to both.

A ByVal parameter makes a copy of the value, whereas ByRef passes the
pointer to the memory address. Therefore, if a parameter is passed ByRef and
you change it's value in your function, it changes for the caller too -
because you were both pointing at the same memory location.

If a parameter is passed in ByVal, then you have a copy of it. If you make
changes inside your function, then it leaves the orginal alone - because you
have a seperate copy.

"sender" is a generic reference to which object kicked off the event that
you are in. You may just want to know, who fired the event. "EventArgs" are
arguments that go along with that event. Those both become more meaningful
when you use an object like a treeview - where the sender is the node that
was clicked, etc..

hth
 
J

JohnG

Is Object always passed by reference?

Frank Drebin said:
First, ByVal and ByRef mean "by value" and "by reference". Since everything
you do on a computer is in memory, something things just want a "pointer" to
the memory location where you data is. Some want the actual data itself.
There are advantages to both.

A ByVal parameter makes a copy of the value, whereas ByRef passes the
pointer to the memory address. Therefore, if a parameter is passed ByRef and
you change it's value in your function, it changes for the caller too -
because you were both pointing at the same memory location.

If a parameter is passed in ByVal, then you have a copy of it. If you make
changes inside your function, then it leaves the orginal alone - because you
have a seperate copy.

"sender" is a generic reference to which object kicked off the event that
you are in. You may just want to know, who fired the event. "EventArgs" are
arguments that go along with that event. Those both become more meaningful
when you use an object like a treeview - where the sender is the node that
was clicked, etc..

hth
 
M

MS News \(MS ILM\)

Say what!

Kevin Spencer said:
Yes, but there are still times when you may need to pass an Object ByRef.
For example, if you need to re-assign the Object, you have to pass it ByRef.
If you only need to change a property, passing it ByVal will work.

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
http://www.takempis.com
Neither a follower nor a lender be.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top