Re: String Reference Type



"RN1" <rn5a@xxxxxxxxxxxxxx> wrote in message
news:6bde7973-75f8-478d-9ed0-db864a9e317c@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

OK...now suppose I have the following code:

<script runat="server">
Class MyInt
Public MyValue As Integer
End Class

Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)
Dim x As New MyInt

...................
...................
...................
End Sub
</script>

If I am not wrong, the Dim line in the Page_Load sub "creates space in
the heap & returns a pointer (say, 0x000001)". To whom does the Dim
line return the pointer?

Please correct me if I am wrong.

There seems to be some confusion in this thread with lots of references to
other languages and other mis-leading content.

You are correct the MyInt class is allocated off the heap.

The 'pointer' (lets call it a reference shall we) returned by the New
operator is stored in the variable x.

If we did this:-

Dim y As MyInt

y = x

y would also reference the same instance of the MyInt held in heap.

Class types are always stored on the heap, period.

y.MyValue = 16

would result in x.MyValue also being 16 since both variables reference the
same object.


What if we had:-

Structure MyInt
Public MyValue As Integer
Publoc MyOtherValue as Integer
End Structure


In this case the symbol x represents space allocated from the stack to store
the MyInt structure, there is no heap allocation and no reference involved.

y = x

Would result in the content of x being copied to y and this:.

y.MyValue = 16

would not affect the MyValue member held in x.

Note that even single value types like Integer or Double are treated as
Structures.


IMO its better to ask 'Is it a Class or a Structure?' instead of 'Is it a
ValueType or a ReferenceType?' Why? See note 1 after sig, I don't want to
blur things to much here.


So to the original point; is a string a class or a structure? Ans: It's a
class.
Therefore its always allocated in the heap and a variable of string type is
always a reference to this string object allocated on the heap.

Dim x As String = "Hello World"
Dim y As String
y = x

As with all classes in this case y and x both reference the same String
object.

What makes this less obvious (as Mykola alluded to) is the immutable nature
of a String. Once a string object has been allocated and initialised its
content cannot be changed, not even changes that don't involve changing the
length. This results in code which makes strings look like they are
behaving as structures because no where do see an operation on one reference
that is visable through another reference.

A similar type to a string is a character array but a character array is
mutable :-

Dim z As Char() = New Char(2) {"A"c, "B"c, "C"c}
Dim x As Integer()

x = z

x(1) = "X"c

Console.WriteLine(z(1))

Note that both z and x reference the same Array object. A modification made
to the object via one reference is visible using the other. This sort of
operation just isn't available on the string object. Its the lack of this
sort of thing happening in code to a string which gives the illusion of it
being a structure.


However Mykola is in error stating that you can't pass strings as ByRef.
You can (in both VB and C#).

Public Sub DoSomething(ByRef rs As String)
rs = "New content"
End Sub

Dim s as String = "Hello World"

DoSomething(s)

Console.WriteLine(s)


This confusion arises from the overloaded use of the word Reference (which
is why I prefer to think in terms of Structures or Classes). When you
think about it all variables are ultimately references to somewhere in
memory where a value is stored. For example:-

Sub Thing()
Dim i as Integer
Dim o as Object

The symbol i resolves to a offset pointer from the a fixed point on the
stack frame where 4 bytes hold the actual value of the integer. The symbol
o resolves to an offset pointer (likely the same as for i + 4) from the same
fixed point on the stack frame which will hold a reference to some reference
to an object stored on the heap.

Passing ByRef has nothing to do with whether the value is a Structure or a
Class but everything to do with where relative to a fixed point on a stack
frame to find the variable value (be that the actual structure content or
the reference to a heap held object). Since at the time of call the size of
the current frame is known its relatively simple to supply an offset from a
fixed point in the new frame back into the previous.


--
Anthony Jones - MVP ASP/ASP.NET


Note 1:-

A couple of other reasons (apart from my primary reason that the word
reference is already over used) I prefer to think in terms of Structure or
Class instead of ValueType or ReferenceType are:-

1). Its possible to have a reference to a structure. I.e. when its boxed
for example referenced via variable typed as Object.

2.) The MustInherit (abstract) type System.ValueType from which all
'ValueTypes' inherit is a class not a structure. How can a structure inherit
from a Class? Ans: it can't. Only when Boxed does a Structure inherit from
ValueType and at the point its a reference to copy on the heap.

Its all quite confusing.

The terms ValueType and ReferenceType apply globally to an Object (which is
always a reference) and indicates whether the type is 'normally' stored
directly in the variable declared of that type or whether the variable of
that type always holds a reference to the value held on the heap.



.



Relevant Pages

  • Re: My VB6 MS Word VBA code crashes Word - Word97
    ... The reason you can't go backwards (that is, compile with a reference to Word ... Dim oWord As Word.Application ' Note early bound is OK here! ... Set oDocuments = oApplication.Documents ' Note this is an early bound ... >>> Dim Firstname As String ...
    (microsoft.public.word.vba.general)
  • Re: Search and Delete Files Based on Mask
    ... If you're not familiar with VB you can reference the KB article for complete ... Dim fso As New FileSystemObject ... Dim sDir As String, sSrchString As String ... fso.DeleteFile (filespec[, force]); ...
    (comp.programming)
  • Re: Send Word Data to Excel
    ... Did you create the Reference to Microsoft Scripting Runtime? ... Dim oWordApp As Word.Application ... Dim FileArrayAs String ... MsgBox "A folder was not selected" ...
    (microsoft.public.word.vba.general)
  • Re: recordset
    ... The problem is with the reference to the form inside the string. ... As you say, this works fine if you try it in the query window, because the ... > Dim rsf As DAO.Recordset ...
    (microsoft.public.access.formscoding)
  • Re: Complex Specified Information - Pitman Formula
    ... Therefore a significant match between a reference and a test ... string is good evidence of non-random production. ... and there are no finite algorithms to compute their digits. ... probabilities of the different symbols the information source can produce. ...
    (talk.origins)

Loading