Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Aren't all objects byRef anyways?

 

Public Sub DoSomething(ByVal X as device )

x.asdf = asdf

End Sub

 

Public Sub DoSomethingElse(ByRef x as device)

x.asdf = asdf

End Sub

 

Same thing isn't it? Objects are referenced by default

My VB.NET Game Programming Tutorial Site (GDI+, Direct3D, Tetris [coming soon], a full RPG.... you name it!)

vbprogramming.8k.com

My Project (Need VB.NET Programmers)

http://workspaces.gotdotnet.com/ResolutionRPG

  • Leaders
Posted (edited)

Passing an object, a variable of reference type, ByRef allows you to modify the reference, passing it ByVal doesn't (just as passing an integer ByRef allows you to modify the integer, whereas passing ByVal doesn't).

 

Public Sub TryByRef(ByRef X As Form)
   X = New Form
End Sub

Public Sub TryByVal(ByVal X As Form)
   X = New Form
End Sub

Public Sub ByRef_vs_ByVal()
   Dim Test As Form = Nothing

   TryByVal(Test)
   'Test is still Nothing
   MessageBox.Show((Test Is Nothing).ToString)   

   TryByRef(Test)
   'Test is now pointing to a form
   MessageBox.Show((Test Is Nothing).ToString)
End Sub

Edited by snarfblam
[sIGPIC]e[/sIGPIC]
Posted

In your example, both have the same effect.

But in the following example, only the second passes back the changed reference to the calling code:

 

Public Sub DoSomething(ByVal X as device )

x = New device()

End Sub

 

Public Sub DoSomethingElse(ByRef x as device)

x = New device()

End Sub

 

i.e., passing ByVal passes a copy of the reference, so that changes to the actual reference are not passed back. Changes to the object are passed back regardless since a copy of a reference still points to the same object.

Posted

It's pretty odd. Because if you do this:

 

Public Sub val(ByVal X as device )

x.asdf = "hi"

End Sub

 

Public Sub ref(ByRef x as device)

x.asdf = "hi"

End Sub

 

----------------

val(x)

ref(Y)

msgbox(x.asdf) ' "hi"

msgbox(y.asdf) ' "hi"

----------------

____________________________________

On the other hand:

 

Public Sub val(ByVal X as device )

x = nothing

End Sub

 

Public Sub ref(ByRef x as device)

x = nothing

End Sub

 

----------------

val(x)

ref(Y)

msgbox(x.asdf) ' <Default Value of asdf>

msgbox(y.asdf) ' <Null Reference Error>

----------------

 

So byVal uses a POINTER to the object (aka when you kill the pointer, the actual object isn't destroyed), but byREF uses a reference (when you kill the reference, the actual object IS destroyted)

 

-TPG

My VB.NET Game Programming Tutorial Site (GDI+, Direct3D, Tetris [coming soon], a full RPG.... you name it!)

vbprogramming.8k.com

My Project (Need VB.NET Programmers)

http://workspaces.gotdotnet.com/ResolutionRPG

  • Administrators
Posted

Public Sub val(ByVal X as device )
x = nothing
End Sub

is effectively sendin a copy of the reference (or pointer) to the object in memory - that means any changes done through this reference will affect the object; however any changes done to the reference itself will not be reflected in the calling routine.

 

Public Sub ref(ByRef x as device)
x = nothing
End Sub

In this case you are passing the reference itself any changes done to the reference will be reflected in the calling routine.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

  • Leaders
Posted
In your example' date=' both have the same effect.[/quote']

There was an error in my code: the parameters should have been forms, not objects. I've edited it though, and otherwise, it does the same exact thing as your code: demonstrates the difference of modifying a reference passed ByRef vs. ByVal. Also note that strings do not work exactly the same as other classes. They fall somewhere between a value type and a reference type for the sake of simplifying their use.

[sIGPIC]e[/sIGPIC]
Posted
There was an error in my code: the parameters should have been forms' date=' not objects. I've edited it though, and otherwise, it does the same exact thing as your code: demonstrates the difference of modifying a reference passed ByRef vs. ByVal. Also note that strings do not work exactly the same as other classes. They fall somewhere between a value type and a reference type for the sake of simplifying their use.[/quote']

 

No - I meant in the original post by Pentium Guy

  • 2 months later...
Posted

--------------------------------------------------------------------------

 

ByVal & ByRef are not the same...

 

(refer to attached pic.)

 

"ByVal" passes the whole value from the Private Sub main to the function. And if the function involves arithmetic calculations, and the value is returned back to the Private Sub main, the value returned is the new value.

 

However, "ByRef", copies the value into the function. Meaning, the value in the Private Sub main remains the same.

 

--------------------------------------------------------------------------

sOMEONE'S gONNA dO iT, wHY nOT yOU ?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...