>Value type variables aren't always stored on the stack, that's only
true for local variables.
Could you give an example of when a value type is not stored on the stack?
Is it in the case of a class member? If so, does it matter if it's static?
[color=blue][color=green]
> >Another question: What is the purpose of the "new" keyword on a value type?
> >Is it just for initialization (calling a constructor)?[/color]
>
> Yes.[/color]
So to confirm, when using "new" on a reference type, it does two things: 1)
It calls the appropriate constructor and 2) it returns the address of the
object on the heap for the assignment to the reference type variable. In the
case of calling new on a value-type variable, calling new does call the
appropriate constructor but returns nothing. Is this correct?
Is this separation of "reference" and "value" types different from classes
versus structs (object vs. value types)? Can you always conclude that a
struct is a value type and a class is a reference type?
Thanks for your helpful info, Mattias...
-Ben
"Mattias Sjögren" wrote:
[color=blue]
>[color=green]
> >Is this because value types are stored on the stack[/color]
>
> Value type variables aren't always stored on the stack, that's only
> true for local variables.
>
>[color=green]
> >initialization is already taken care of?[/color]
>
> All variables in VB are initialized to their default values. The
> default value is basically when all bits are set to zero. That means
> zero for numeric types, False for booleans and Nothing for reference
> type variables.
>
>[color=green]
> >Now, when dealing with an object type, it seems you do need to use the "new"
> >keyword.[/color]
>
> Only to assign something other than a Nothing reference (which you
> probably want to do something useful with the variable).
>
>[color=green]
> >What exactly is x before it's assigned to something with the "new" keyword?[/color]
>
> Nothing
>
>[color=green]
> >Is it just space on the stack just as before?[/color]
>
> If it's a local variable, there's space allocated on the stack for it
> yes.
>
>[color=green]
> >Assuming this all to be the case, I'm curious about the behavior of the
> >String class. I was under the impression that it is not a value type.[/color]
>
> Right, it's a reference type.
>
>[color=green]
> >However, it can be assigned to without using the new keyword, which breaks
> >the logic I thought applied. Further, it allows you to use the new keyword on
> >it.[/color]
>
> When you use a string literal a String object is implicitly created
> for you so you don't have to write New.
>
>[color=green]
> >Another question: What is the purpose of the "new" keyword on a value type?
> >Is it just for initialization (calling a constructor)?[/color]
>
> Yes.
>
>
> Mattias
>
> --
> Mattias Sjögren [MVP] mattias @ mvps.org
>
http://www.msjogren.net/dotnet/ |
http://www.dotnetinterop.com
> Please reply only to the newsgroup.
>[/color]