Click here to Skip to main content
16,004,969 members
Home / Discussions / Visual Basic
   

Visual Basic

 
Questionusing Word.Document's SaveAs Function (VB6) Pin
reegan4112-Jul-07 6:15
reegan4112-Jul-07 6:15 
AnswerRe: using Word.Document's SaveAs Function (VB6) Pin
Thomas Chester12-Jul-07 8:56
Thomas Chester12-Jul-07 8:56 
QuestionBoxing/Unboxing Performance Pin
hrodberaht12-Jul-07 6:09
hrodberaht12-Jul-07 6:09 
AnswerRe: Boxing/Unboxing Performance Pin
Christian Graus12-Jul-07 6:12
protectorChristian Graus12-Jul-07 6:12 
AnswerRe: Boxing/Unboxing Performance Pin
Luc Pattyn12-Jul-07 6:31
sitebuilderLuc Pattyn12-Jul-07 6:31 
AnswerRe: Boxing/Unboxing Performance Pin
Dave Kreskowiak12-Jul-07 7:11
mveDave Kreskowiak12-Jul-07 7:11 
AnswerRe: Boxing/Unboxing Performance Pin
Guffa12-Jul-07 7:23
Guffa12-Jul-07 7:23 
AnswerRe: Boxing/Unboxing Performance [modified] Pin
Dave Kreskowiak12-Jul-07 8:19
mveDave Kreskowiak12-Jul-07 8:19 
ByVal and ByRef passing of Value types...

Passing a Value type ByVal is easy. It simply copies the value onto the call stack. You now have two copies of the value, the original and the one the method parameter is holding.

Passing a Value type ByRef causes a reference to the variable to be passed to the method. This means there is a single copy of the value, the original, and two variables, the other being the one in the method code, have a reference to it. If either variable makes a change to the value, the other variable will also be looking at the same changed value.

Passing a Reference type ByVal is a bit different than what you might expect. A variable holds the the reference, or a pointer, to the object on the heap, so passing a reference ByVal causes a copy of the reference to be passed. This means that both the original variable and the variable in the method both point to the exact same object on the heap. If one variable make a change to the object, the other variable will be able to see the change.

Passing a Reference type ByRef causes a reference to the referencing variable to be passed. Confused? How about this. The method variable holds a reference to the original variable that holds the reference to the object. In C++ terms, that's a pointer to a pointer to an object.

So what's the difference between passing a Reference type ByVal and ByRef? There really isn't one. VB.NET handles de-referencing the "pointer to a pointer" for you. The only real difference is a small performance hit because of the dereferencing. You should really stick to passing Reference types ByVal and not ByRef.

[EDIT] About the only time you would pass a Reference type ByRef is if the method being called should be able to set the original reference to Nothing (null in C#).


-- modified at 14:26 Thursday 12th July, 2007

A guide to posting questions on CodeProject[^]

Dave Kreskowiak
Microsoft MVP
Visual Developer - Visual Basic
     2006, 2007


AnswerRe: Boxing/Unboxing Performance Pin
hrodberaht12-Jul-07 10:14
hrodberaht12-Jul-07 10:14 
QuestionCheck file existence on FTP Pin
Paul .12-Jul-07 5:03
Paul .12-Jul-07 5:03 
AnswerRe: Check file existence on FTP Pin
Dave Kreskowiak12-Jul-07 5:35
mveDave Kreskowiak12-Jul-07 5:35 
AnswerRe: Check file existence on FTP Pin
Paul .12-Jul-07 5:38
Paul .12-Jul-07 5:38 
AnswerRe: Check file existence on FTP Pin
Vasudevan Deepak Kumar13-Jul-07 0:17
Vasudevan Deepak Kumar13-Jul-07 0:17 
QuestionUsername from network issue in VB Pin
Ajit Sandhu12-Jul-07 4:55
Ajit Sandhu12-Jul-07 4:55 
AnswerRe: Username from network issue in VB Pin
Dave Kreskowiak12-Jul-07 4:58
mveDave Kreskowiak12-Jul-07 4:58 
GeneralRe: Username from network issue in VB Pin
Ajit Sandhu12-Jul-07 5:02
Ajit Sandhu12-Jul-07 5:02 
GeneralRe: Username from network issue in VB Pin
Dave Kreskowiak12-Jul-07 5:30
mveDave Kreskowiak12-Jul-07 5:30 
AnswerRe: Username from network issue in VB Pin
Vasudevan Deepak Kumar13-Jul-07 0:16
Vasudevan Deepak Kumar13-Jul-07 0:16 
Question[Message Deleted] Pin
shri_khamitkar12-Jul-07 4:32
shri_khamitkar12-Jul-07 4:32 
AnswerRe: Institute Managment System Pin
Dave Kreskowiak12-Jul-07 4:58
mveDave Kreskowiak12-Jul-07 4:58 
GeneralRe: Institute Managment System Pin
Vasudevan Deepak Kumar13-Jul-07 0:15
Vasudevan Deepak Kumar13-Jul-07 0:15 
AnswerTHE RULES Pin
leckey12-Jul-07 5:55
leckey12-Jul-07 5:55 
GeneralRe: THE RULES Pin
Vasudevan Deepak Kumar13-Jul-07 0:14
Vasudevan Deepak Kumar13-Jul-07 0:14 
AnswerRe: Institute Managment System Pin
The ANZAC12-Jul-07 13:00
The ANZAC12-Jul-07 13:00 
AnswerRe: Institute Managment System Pin
Vasudevan Deepak Kumar13-Jul-07 0:14
Vasudevan Deepak Kumar13-Jul-07 0:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.