newbie needs some definitions!

  • Thread starter Thread starter campbellbrian2001
  • Start date Start date
C

campbellbrian2001

Thanks in advance to all:

I just need a little nudge to understand a couple of elementary VB.net
concepts:

1) In the following code I copied, what does the lower-case "e" mean?
(ByVal e As)

2) Does the word "sender" mean that "system.Object" is sending the
"value".

3) I assume that ByVal means a value is being used, whereas ByRef means
a reference to an object is being used.



Private Sub FormB_During_Closing(ByVal sender As System.Object, ByVal e
As
CancelEventArgs) Handles MyBase.Closing

Thanks! Brian
 
I believe "sender" is the object that is actually rasiing the Event.
Since it's possible to have multiple objects raise an event, you can
use this argument to determine which object actually called the event.

The "e" argument is the type "CancelEventArgs". You can search MSDN
to read more about it, but you can set the e.cancel property to TRUE to
Cancel the form's close event (this would keep the form open)
 
I just need a little nudge to understand a couple of elementary VB.net
concepts:

1) In the following code I copied, what does the lower-case "e" mean?
(ByVal e As)

'e' stands for "event arguments". You can use 'e' to access data passed
into the event handler. This is useful for events like a form's 'Closing'
event or a textbox' 'KeyPress' event.
2) Does the word "sender" mean that "system.Object" is sending the
"value".

'sender' contains a reference to the object which raises the event. In a
button's 'Click' event handler it will contain a reference to the button
that has been clicked by the user.
3) I assume that ByVal means a value is being used, whereas ByRef means
a reference to an object is being used.

That's true for value types (structures, enumerations), but different for
reference types (classes, interface types).

In VB.NET, 'ByVal' is the default (opposed to VB6, where 'ByRef' was the
default), so you don't need to explicitly specify it. 'ByVal' and 'ByRef'
serve slightly different purposes for value types and reference types:

When passing a variable to a method in a 'ByVal' parameter, the variable's
content is duplicated and the copy is passed to the method. Altering the
parameter variable inside the method won't change the variable you passed to
the method. On the other hand, 'ByRef' will pass a "reference" to the
variable to the method, and thus assigning another value to the parameter
variable inside the method will affect the value of the variable passed to
the method:

\\\
Dim i As Integer = 2
DoSomething1(i)
MsgBox(CStr(i)) ' "2".
i = 2
DoSomething2(i)
MsgBox(CStr(i)) ' "3".
..
..
..
Private Sub DoSomething1(ByVal i As Integer)
i = i + 1
End Sub

Private Sub DoSomething2(ByRef i As Integer)
i = i + 1
End Sub
///

For reference types (classes) there is also a difference between 'ByVal' and
'ByRef'. By passing a variable to a parameter that is marked as 'ByVal', a
copy of the reference stored in the variable is created and this reference
is passed to the method. Thus you cannot change the reference the variable
you passed to the method is pointing to inside the method ('DoSomething1').
When passing the variable 'ByRef', the variable's reference is exposed to
the method directly and can be manipulated by the method, which means that
the method can let the variable you passed to the method point to another
object. The sample below will demonstrate this behavior:

\\\
Dim sb As New StringBuilder("Foo")
DoSomething1(sb)
MsgBox(sb.ToString()) ' "Foo".
sb = New StringBuilder("Foo")
DoSomething2(sb)
MsgBox(sb.ToString()) ' "Bla".
..
..
..
Private Sub DoSomething1(ByVal s As StringBuilder)
s = New StringBuilder()
s.Append("Bla")
End Sub

Private Sub DoSomething2(ByRef s As StringBuilder)
s = New StringBuilder()
s.Append("Bla")
End Sub
///

Hope this helps!
 
Thanks in advance to all:

I just need a little nudge to understand a couple of elementary VB.net
concepts:

1) In the following code I copied, what does the lower-case "e" mean?
(ByVal e As)

e represents your Event Arguments
2) Does the word "sender" mean that "system.Object" is sending the
"value".

sender represents the Object that Raised the event in question
3) I assume that ByVal means a value is being used, whereas ByRef means
a reference to an object is being used.

byval means the data stored in that variable can be modified however you
like and the original variable wont get changed

byref means any changes you make to the byref variable will modify the
original variable sent to the sub/function
 

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

Back
Top