[AccessD] DotNet passing by value

Shamil Salakhetdinov shamil at users.mns.ru
Fri Aug 26 08:05:55 CDT 2005


No, objects are always passed by reference - here is a quick test, which
shows the difference (watch line wraps):

Module Module1

    Class TestMe
        Public TestInt As Integer = 0
    End Class

    Sub Main()
        Dim obj As TestMe = New TestMe
        Dim s As String = "***"
        testObject(obj, s)
        Console.WriteLine(String.Format("ByVal test: [{0}] [{1}]",
obj.TestInt.ToString(), s))
        obj.TestInt = 0
        testObject1(obj, s)
        Console.WriteLine(String.Format("ByRef test: [{0}] [{1}]",
obj.TestInt.ToString(), s))
    End Sub

    Sub testObject(ByVal obj As TestMe, ByVal s As String)
        obj.TestInt = 503
        s = "503"
    End Sub
    Sub testObject1(ByRef obj As TestMe, ByRef s As String)
        obj.TestInt = 503
        s = "503"
    End Sub

To pass objects by value you have to implement custom cloning:

See also:
 1. IClonable interface
 2. http://www.windojitsu.com/blog/copyctorvsicloneable.html (
Copy Constructors vs ICloneable -- Redux (Updated!))

Shamil


----- Original Message ----- 
From: "John W. Colby" <jwcolby at colbyconsulting.com>
To: "'Access Developers discussion and problem solving'"
<accessd at databaseadvisors.com>
Sent: Friday, August 26, 2005 4:34 PM
Subject: RE: [AccessD] DotNet passing by value


> Does this imply that it makes a copy of every object before passing a
> pointer to that copy of the object?  That seems like a lot of overhead.
And
> is there any way to set the default to true "by reference"?  I am
accustomed
> to "by reference", and know what that means, and am accustomed to thinking
> about whether I am modifying the original object.
>
> John W. Colby
> www.ColbyConsulting.com
>
> Contribute your unused CPU cycles to a good cause:
> http://folding.stanford.edu/
>
> -----Original Message-----
> From: accessd-bounces at databaseadvisors.com
> [mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Shamil
> Salakhetdinov
> Sent: Friday, August 26, 2005 8:25 AM
> To: Access Developers discussion and problem solving
> Subject: Re: [AccessD] DotNet passing by value
>
>
> John,
>
> AFAIK VBA passes ByVal strings by reference but it first makes the copy of
> the string to the heap memory... The same should be true for  VB.NET/C#.
So,
> one million bytes' length string will be copied to the heap memory and
then
> passed internally by ref. Heap memory is handled by virtual memory
> management system - it should work rather well if one has speedy moderrn
> HHDs for automatic swapping...
>
> But of course passing one million bytes long strings as a ByVal parameter
> isn't a good idea at all....
>
> > As you know, everything in .net is an object
> Yes, everything is an object but scalar types are used internall as they
are
> usually used in unmanaged systems - this optimization is done by .NET
> Framework compilers. When a scalar type has to be treated as an object it
is
> getting "boxed" - boxing is a  ,NET Framework term:
>
> See also:
>
> Writing Faster Managed Code: Know What Things Cost
>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/ht
> ml/fastmanagedcode.asp
>
> Shamil
>
> ----- Original Message ----- 
> From: "John W. Colby" <jwcolby at colbyconsulting.com>
> To: "'Access Developers discussion and problem solving'"
> <accessd at databaseadvisors.com>
> Sent: Friday, August 26, 2005 3:21 PM
> Subject: RE: [AccessD] DotNet passing by value
>
>
> > Oooops...
> >
> > >.. passing anything else (including strings) by value.
> >
> > Should have said "by reference", i.e. placing a pointer to the object
> > on
> the
> > stack.
> >
> > John W. Colby
> > www.ColbyConsulting.com
> >
> > Contribute your unused CPU cycles to a good cause:
> > http://folding.stanford.edu/
> >
> > -----Original Message-----
> > From: accessd-bounces at databaseadvisors.com
> > [mailto:accessd-bounces at databaseadvisors.com] On Behalf Of John W.
> > Colby
> > Sent: Friday, August 26, 2005 7:11 AM
> > To: dba-vb at databaseadvisors.com; 'Access Developers discussion and
> > problemsolving'
> > Subject: [AccessD] DotNet passing by value
> >
> >
> > As you might be aware, VB.Net (and I assume the other .net languages
> > as
> > well) pass all variables by value.  But what does this mean, and is it
> true.
> > As you know, everything in .net is an object, even common variables
> > such
> as
> > an integer or decimal etc.  Passing by value (in other languages)
> > means placing a COPY of the variable (or object in this case) on the
> > stack as
> the
> > function is called.  In VBA for example, when you pass by value, it
> > really does ONLY for the simple data types, passing anything else
> > (including
> > strings) by value.
> >
> > Imagine passing a string, which could be a million bytes, by value -
> > literally making a copy and passing that into the stack.  IIRC the
> > total stack space for a given program in an Intel machine is something
> > like 128 kbytes which means that passing a single (huge) string by
> > value could
> cause
> > a stack overflow.  Now DotNet comes along claiming to pass everything
> > by value.  Is this more doublespeak?  And if so, what is the truth?
> >
> > John W. Colby
> > www.ColbyConsulting.com
> >
> > Contribute your unused CPU cycles to a good cause:
> > http://folding.stanford.edu/
> >
> >
> > --
> > AccessD mailing list
> > AccessD at databaseadvisors.com
> > http://databaseadvisors.com/mailman/listinfo/accessd
> > Website: http://www.databaseadvisors.com
> >
> >
> >
> > --
> > AccessD mailing list
> > AccessD at databaseadvisors.com
> > http://databaseadvisors.com/mailman/listinfo/accessd
> > Website: http://www.databaseadvisors.com
>
> -- 
> AccessD mailing list
> AccessD at databaseadvisors.com
> http://databaseadvisors.com/mailman/listinfo/accessd
> Website: http://www.databaseadvisors.com
>
>
>
> -- 
> AccessD mailing list
> AccessD at databaseadvisors.com
> http://databaseadvisors.com/mailman/listinfo/accessd
> Website: http://www.databaseadvisors.com




More information about the AccessD mailing list