ThePentiumGuy Posted June 7, 2005 Posted June 7, 2005 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 Quote 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 snarfblam Posted June 7, 2005 Leaders Posted June 7, 2005 (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 June 7, 2005 by snarfblam Quote [sIGPIC]e[/sIGPIC]
Jaco Posted June 7, 2005 Posted June 7, 2005 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. Quote
ThePentiumGuy Posted June 7, 2005 Author Posted June 7, 2005 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 Quote 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 PlausiblyDamp Posted June 7, 2005 Administrators Posted June 7, 2005 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
ThePentiumGuy Posted June 7, 2005 Author Posted June 7, 2005 Hmm.. that seems to make sense. Which would be more sensible to use though (in terms of memory management)? ByVal or ByRef on objects. -The Pentum Guy Quote 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 snarfblam Posted June 7, 2005 Leaders Posted June 7, 2005 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. Quote [sIGPIC]e[/sIGPIC]
Jaco Posted June 7, 2005 Posted June 7, 2005 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 Quote
FYRe Posted August 19, 2005 Posted August 19, 2005 -------------------------------------------------------------------------- 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. -------------------------------------------------------------------------- Quote sOMEONE'S gONNA dO iT, wHY nOT yOU ?
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.