Kevin,
| E.g. if you pass myStructureDef to a method, you'll be passing a copy,
which
| includes a copy of the array and the string.
As you stated, Arrays & Strings are reference types. The myStructureDef
structure contains a reference to the actual objects on the heap. Not a copy
of the actual object!
If you pass myStructureDef to a method you will be passing a copy of the
structure, which includes a copy of the *references* to the array & the
string objects. There will only be a single instance of the array & string
object on the heap!
Because Strings are immutable its hard to notice a difference. However
Arrays & most other reference types are mutable, consider the following:
Public Structure myStructureDef
Public i As Integer
Public b() As Byte
Public t As String
End Structure
Private Sub Something(ByVal parameter As myStructureDef)
parameter.i = 2
parameter.b(0) = 5
parameter.b(1) = 6
parameter.b(2) = 7
End Sub
Dim local As myStructureDef
local.b = New Byte() {1, 2, 3}
local.t = "Hello"
Something(local )
"parameter" will be a copy of the "local" myStructureDef, parameter.i is
inline in the structure as its a value type, so changing parameter.i does
not change local.i. However the array that parameter.b references is the
same array that local.b references, so changing an element of the array
"parameter. b(0) = 5" also changes "local.i". However changing the reference
itself, will change the reference itself "parameter. b = New Byte() {4, 5,
6}" will create a new array object on the heap, replacing the reference that
"parameter. b" is...
--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley -
http://www.tsbradley.net
"Kevin Westhead" <ma***********@ nospam.nospam> wrote in message
news:uz******** ******@TK2MSFTN GP15.phx.gbl...
| Arrays and strings are both reference types, so they'll be allocated on
the
| GC heap. I think the 16 byte rule is just one guideline aimed at getting
you
| to consider whether you really want value type or reference type
semantics.
| E.g. if you pass myStructureDef to a method, you'll be passing a copy,
which
| includes a copy of the array and the string. If you assign one instance to
| another, again you'll be assigning a copy rather than having both
instances
| reference the same data. If you find yourself passing myStructureDef
| arguments ByRef alot then you should really consider using a reference
type
| instead. You should also think about how often myStructureDef will be
boxed
| in your core scenarios.
|
| --
| Kevin Westhead
|
| "Dennis" <De****@discuss ions.microsoft. com> wrote in message
| news:15******** *************** ***********@mic rosoft.com...
| > If I have a structure like;
| >
| > Public Structure myStructureDef
| > Public b() as Byte
| > Public t as String
| > End Structure
| >
| > If I pass this structure, will the values in the array b be stored on
the
| > stack or will just a pointer to the array be stored on the stack? I am
| > trying to decide whether to use Structures or Pointers. I know that
| > M'soft
| > recommends to use a class if the length is over about 16 bytes but does
| > this
| > include all the array elements or just pointers to the array?
| > --
| > Dennis in Houston
|
|