Click here to Skip to main content
15,887,267 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: Byref or Byval : which is faster and consume less memory Pin
dilkonika22-Jan-15 11:15
dilkonika22-Jan-15 11:15 
GeneralRe: Byref or Byval : which is faster and consume less memory Pin
Dave Kreskowiak22-Jan-15 17:06
mveDave Kreskowiak22-Jan-15 17:06 
GeneralRe: Byref or Byval : which is faster and consume less memory Pin
ZurdoDev22-Jan-15 11:18
professionalZurdoDev22-Jan-15 11:18 
GeneralRe: Byref or Byval : which is faster and consume less memory Pin
PIEBALDconsult22-Jan-15 11:21
mvePIEBALDconsult22-Jan-15 11:21 
GeneralRe: Byref or Byval : which is faster and consume less memory Pin
dilkonika22-Jan-15 11:23
dilkonika22-Jan-15 11:23 
GeneralRe: Byref or Byval : which is faster and consume less memory Pin
PIEBALDconsult22-Jan-15 11:41
mvePIEBALDconsult22-Jan-15 11:41 
AnswerRe: Byref or Byval : which is faster and consume less memory Pin
ZurdoDev22-Jan-15 13:36
professionalZurdoDev22-Jan-15 13:36 
GeneralRe: Byref or Byval : which is faster and consume less memory Pin
dbitsch22-Jan-15 15:34
dbitsch22-Jan-15 15:34 
Pieb is right in that one should use Byval most of the time and only use Byref sparingly, if at all.

I personally do not feel that performance is that much of an issue between the two.

And yes, Byref is passing a reference pointer back to the original variable. Whereas Byval is just passing the value to the called function/routine.

I recommend to use Byref sparingly because if, in the called subroutine, you change the value of the passed variable, what you are actually doing is changing the value in the original variable that you made your call with. See example:

Public sub TryThis

dim X as String
dim Y as String
dim Z as String

X = "Red"
Y = "Green"
Z = "Yellow"

Z = TestFunction(x,y)

debug.print x
debug.print y
debug.print z

end sub

======

Public Function TestFunction(byval Value1 as string, Byref Value2 as string) as string

Value1 = "White"
Value2 = "Blue"

Return Value1

End Function


Results: X = "Red"
Y = "Blue"
Z = "White"

Notice that the value of Y changed even though it was not referenced or accessible by name in the function. This is because Y was represented in the function as Value2 and was passed Byref. The value of X was passed ByVal and did not change even though Value1 was also changed in the function. One should be careful using Byref for this reason. ByVal should be the preferred option in most all cases.

Any time I code a Byref, I always make it clearly known in a comment at every call what is being passed byref and if any value is being returned using the byref variable.
GeneralRe: Byref or Byval : which is faster and consume less memory Pin
ZurdoDev22-Jan-15 16:00
professionalZurdoDev22-Jan-15 16:00 
GeneralRe: Byref or Byval : which is faster and consume less memory Pin
Dave Kreskowiak22-Jan-15 17:09
mveDave Kreskowiak22-Jan-15 17:09 
GeneralRe: Byref or Byval : which is faster and consume less memory Pin
Dave Kreskowiak22-Jan-15 17:07
mveDave Kreskowiak22-Jan-15 17:07 
QuestionHow to create relationship of 2 tables using OleDbConnection Pin
gwittlock17-Jan-15 11:36
gwittlock17-Jan-15 11:36 
AnswerRe: How to create relationship of 2 tables using OleDbConnection Pin
PIEBALDconsult17-Jan-15 12:02
mvePIEBALDconsult17-Jan-15 12:02 
GeneralRe: How to create relationship of 2 tables using OleDbConnection Pin
gwittlock17-Jan-15 12:12
gwittlock17-Jan-15 12:12 
AnswerRe: How to create relationship of 2 tables using OleDbConnection Pin
gwittlock17-Jan-15 22:45
gwittlock17-Jan-15 22:45 
GeneralRe: How to create relationship of 2 tables using OleDbConnection Pin
AccessDeveloper29-Jan-15 7:01
AccessDeveloper29-Jan-15 7:01 
QuestionClick on "connect" button, the progressbar is filled, a label appears writing if it is connected to the support or not Pin
Member 1138076216-Jan-15 10:12
Member 1138076216-Jan-15 10:12 
AnswerRe: Click on "connect" button, the progressbar is filled, a label appears writing if it is connected to the support or not Pin
Eddy Vluggen16-Jan-15 13:15
professionalEddy Vluggen16-Jan-15 13:15 
AnswerRe: Click on "connect" button, the progressbar is filled, a label appears writing if it is connected to the support or not Pin
Richard MacCutchan16-Jan-15 22:49
mveRichard MacCutchan16-Jan-15 22:49 
QuestionList containing custom controls Pin
Bart Van Eyndhoven15-Jan-15 23:10
Bart Van Eyndhoven15-Jan-15 23:10 
AnswerRe: List containing custom controls Pin
Richard Andrew x6416-Jan-15 5:57
professionalRichard Andrew x6416-Jan-15 5:57 
AnswerRe: List containing custom controls Pin
Eddy Vluggen16-Jan-15 7:38
professionalEddy Vluggen16-Jan-15 7:38 
QuestionSNMP Coding for VB 2013 Pin
RudyL15-Jan-15 11:11
RudyL15-Jan-15 11:11 
AnswerRe: SNMP Coding for VB 2013 Pin
Richard MacCutchan15-Jan-15 21:40
mveRichard MacCutchan15-Jan-15 21:40 
AnswerRe: SNMP Coding for VB 2013 Pin
Eddy Vluggen16-Jan-15 7:44
professionalEddy Vluggen16-Jan-15 7:44 

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.