Click here to Skip to main content
15,886,873 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralRe: tiny tiny REGEX problem Pin
ant1xxx17-Aug-07 4:35
ant1xxx17-Aug-07 4:35 
AnswerRe: tiny tiny REGEX problem Pin
Guffa17-Aug-07 6:25
Guffa17-Aug-07 6:25 
GeneralRe: tiny tiny REGEX problem Pin
ant1xxx17-Aug-07 7:49
ant1xxx17-Aug-07 7:49 
GeneralRe: tiny tiny REGEX problem Pin
Guffa17-Aug-07 14:35
Guffa17-Aug-07 14:35 
QuestionByval Vs ByRef Performance Pin
K.P.Kannan22-May-07 4:09
K.P.Kannan22-May-07 4:09 
AnswerRe: Byval Vs ByRef Performance Pin
Kschuler22-May-07 5:05
Kschuler22-May-07 5:05 
GeneralRe: Byval Vs ByRef Performance Pin
GregScott22-May-07 7:31
GregScott22-May-07 7:31 
AnswerRe: Byval Vs ByRef Performance Pin
MidwestLimey22-May-07 7:14
professionalMidwestLimey22-May-07 7:14 
First: abandon any notion of ByRef vs. ByVal from VB6.
Second: there is a difference with ByVal and ByRef wrt object instances.

What ByRef and ByVal indicate are which memory address to copy to the method's stack.

Lets look at the following code:
<br />
    Private Sub DataTableCreator()<br />
        Dim dt As New DataTable()<br />
        ByValReceiver(dt)<br />
        ByRefReceiver(dt)<br />
    End Sub<br />
<br />
    Private Sub ByValReceiver(ByVal valdt As DataTable)<br />
        valdt = Nothing<br />
    End Sub<br />
<br />
    Private Sub ByRefReceiver(ByRef refdt As DataTable)<br />
        refdt = Nothing<br />
    End Sub<br />


ByValReceiver will take in the memory address of the object on the heap and assign it to a local variable "valdt" on it's stack.

ByRefReceiver will take in the memory address of the variable "dt" from DataTableCreator's stack and assign that to refdt.

So what does this do for you? Two things.

1) If you run the code you'll see ByValReceiver won't effect dt in DataTableCreator, but ByRefReceiver does.
When you assign Nothing you're in effect clearing the memory reference from the variable.
refdt is really a reference to a reference (or pointer to a pointer in in the old days Smile | :) **p), setting it to nothing clears the real reference to the DataTable, which is dt in DataTableCreator.

2) I can't prove this since .Net can be a black box, but I fully expect that nesting calls with ByRef will cause multiple dereferencing steps (going to the memory address in the variable) to get back to the heap.
If you used refdt, first it must dereference to dt, then dereference dt to get to the actual DataTable instance on the heap.
This would actually marginally slow things down.

ByRef is required for when you want value types, which are held on the local stack (i.e. integer, long, structures etc) to be modified by the called method.
Think out parameters.

Objects should be handed down ByVal, unless you really want to use the trick above!

Hope this clears things up at least a little?


Coding since '85 - ATARI BASIC, Forth, C, Java, C#, VB.Net ..
AnswerRe: Byval Vs ByRef Performance Pin
Guffa22-May-07 9:37
Guffa22-May-07 9:37 
AnswerRe: Byval Vs ByRef Performance Pin
K.P.Kannan22-May-07 20:10
K.P.Kannan22-May-07 20:10 
Questionerror handling Pin
WhiteGirl2322-May-07 3:52
WhiteGirl2322-May-07 3:52 
AnswerRe: error handling Pin
Dave Herren22-May-07 3:55
Dave Herren22-May-07 3:55 
Questionwininet in vb.net Pin
psiva198422-May-07 2:22
psiva198422-May-07 2:22 
AnswerRe: wininet in vb.net Pin
Dave Kreskowiak22-May-07 17:42
mveDave Kreskowiak22-May-07 17:42 
GeneralRe: wininet in vb.net Pin
psiva198422-May-07 18:52
psiva198422-May-07 18:52 
GeneralRe: wininet in vb.net Pin
psiva198423-May-07 1:17
psiva198423-May-07 1:17 
GeneralRe: wininet in vb.net Pin
Dave Kreskowiak23-May-07 15:36
mveDave Kreskowiak23-May-07 15:36 
QuestionHow to disable Ctrl P keys of webbrowser control Pin
koolprasad200322-May-07 2:07
professionalkoolprasad200322-May-07 2:07 
AnswerRe: How to disable Ctrl P keys of webbrowser control Pin
codemunkeh22-May-07 10:12
codemunkeh22-May-07 10:12 
GeneralRe: How to disable Ctrl P keys of webbrowser control Pin
koolprasad200322-May-07 18:53
professionalkoolprasad200322-May-07 18:53 
Questionhelp meeeeeee.......... Pin
choorakkuttyil22-May-07 1:42
choorakkuttyil22-May-07 1:42 
AnswerRe: help meeeeeee.......... Pin
ne0h22-May-07 2:19
ne0h22-May-07 2:19 
AnswerFrequent message title abuser! Pin
leckey22-May-07 3:08
leckey22-May-07 3:08 
QuestionExport from Crystal report to MS Access Pin
charchabil0322-May-07 1:23
charchabil0322-May-07 1:23 
QuestionHow to compare two types Pin
eschem22-May-07 1:03
eschem22-May-07 1:03 

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.