Pointer in VB.net

  • Thread starter Thread starter Daryl Davis
  • Start date Start date
D

Daryl Davis

Can someone tell me (or point me) on how to use pointers in VB.net

Passing a Varible by ref to a new form. Now I want to change that varible
in that form.
thank you
Daryl
 
Think of it this way -> using ByRef passes a variable by pointer and using ByVal passes a copy of the variable..

Private Sub MyProcedure(ByRef strValue As String
strValue = "New Value
End Su

strValue = "Original Value
MyProcedure(strValue
MsgBox(strValue) 'displays "New Value

....notice how the value of strValue has changed as the variable was passed by *pointer* which allowed the procedure to change the original variable value..

Private Sub MyProcedure(ByVal strValue As String
strValue = "New Value
End Su

strValue = "Original Value
MyProcedure(strValue
MsgBox(strValue) 'displays "Original Value

....this time the variable has not changed as the procedure was passed a *copy* of the variable so the change to the value in the procedure did not affect the original variable value

I hope this helps explain things

Gary
 
There are some caveats to all this.
Depending on whether the variable being passed is a value type or a
reference the ByRef and ByVal have different behaviours.

For instance if your passing a reference type ByRef you can change the
class instance it points, i.e the memory address, in the called method
and that will be reflected in the calling method, but if you pass
ByVal then you can only change the members of that reference type, not
the actual memory address of the referred instance in the calling
procedure.

hth

Richard
 
Let me explain with the code.
public someclass as class
dim newperson as person
dim mbCancel as boolean
#Region " Windows Form Designer generated code "

Public Sub New(ByRef newperson As Person, ByRef mbCancel As Boolean)

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

Me.newperson = newperson

Me.mbCancel = mbCancel

End Sub

private sub changeNewPerson

'here is where I want to change the newperson value

end sub
 
private sub changeNewPerson
'here is where I want to change the newperson value

end sub

My response might sound like attitude but its not. :)
My response is "So whats stopping you?"
private sub changeNewPerson

'here is where I want to change the newperson value

dim anotherperson as new person()
me.newperson = anotherperson

Because you passed newperson by ref the newperson in the code block
that instantiated this form and passed its person into the constructor
of this form, will now point to another person as well.

If you had of passed by val you could have done
Me.newperson.someproperty = xyz

and this would have affected the person in the calling class (because
they are "both" pointing to the same person.) Note i said "both".

BUT

you if you did this

me.newperson = anotherperson,

the person in the calling code would still point to the old person.
Thats because ByVal essentially creates another variable that gets put
on the stack so you have two variables on the stack pointing to the
same object on the heap.
So when you change the heap reference in one var you dont change it in
the other.

Passing By Ref, passes the orignal address on the stack, so when you
change the object thats it pointing to, this affects both the variable
in the calling code and that in this form. *Variables* are just memory
addresses. And in this case there is only "one".

Having said all of this. I would make changeperson a function and
explicitly return a reference to the calling code. It makes things
clearer and much more understandable for the next guy/girl developer.

I would also include a counselling method, because i suspect some of
these people are going to have start experiencing some serious
identity crisis'.

hth

Richard
 
Back
Top