473,421 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,421 software developers and data experts.

"new" keyword on value and object types?

I'm curious about the differeng behavior of the "new" keyword when dealing
with value versus object types. If I'm correct, when I do:

dim x as integer

There's no need for "new" to be called since this "value" type is all set to
go. Is this because value types are stored on the stack so the memory
initialization is already taken care of?

Now, when dealing with an object type, it seems you do need to use the "new"
keyword. Accordingly, when I do:

dim x as oledbconnection

What exactly is x before it's assigned to something with the "new" keyword?
Is it just space on the stack just as before?

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.
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.

Another question: What is the purpose of the "new" keyword on a value type?
Is it just for initialization (calling a constructor)? When "new" is called
on a object type, does it do this as well as return a pointer to the heap
address?

Thanks for any clarification...

-Ben

Nov 23 '05 #1
4 2606
Is this because value types are stored on the stack
Value type variables aren't always stored on the stack, that's only
true for local variables.

initialization is already taken care of?
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.

Now, when dealing with an object type, it seems you do need to use the "new"
keyword.
Only to assign something other than a Nothing reference (which you
probably want to do something useful with the variable).

What exactly is x before it's assigned to something with the "new" keyword?
Nothing

Is it just space on the stack just as before?
If it's a local variable, there's space allocated on the stack for it
yes.

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.
Right, it's a reference type.

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.
When you use a string literal a String object is implicitly created
for you so you don't have to write New.

Another question: What is the purpose of the "new" keyword on a value type?
Is it just for initialization (calling a constructor)?


Yes.
Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 23 '05 #2
>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?
Another question: What is the purpose of the "new" keyword on a value type?
Is it just for initialization (calling a constructor)?
Yes.


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:
Is this because value types are stored on the stack


Value type variables aren't always stored on the stack, that's only
true for local variables.

initialization is already taken care of?


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.

Now, when dealing with an object type, it seems you do need to use the "new"
keyword.


Only to assign something other than a Nothing reference (which you
probably want to do something useful with the variable).

What exactly is x before it's assigned to something with the "new" keyword?


Nothing

Is it just space on the stack just as before?


If it's a local variable, there's space allocated on the stack for it
yes.

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.


Right, it's a reference type.

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.


When you use a string literal a String object is implicitly created
for you so you don't have to write New.

Another question: What is the purpose of the "new" keyword on a value type?
Is it just for initialization (calling a constructor)?


Yes.
Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 23 '05 #3
Ben,
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?
Yes exactly. Or when you have an array of the value type.

If so, does it matter if it's static?
No

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?
Yes that sounds right. Since the space for the value type variable is
already allocated there's no need to return a pointer to any newly
allocated object. You just overwrite the existing space.

Is this separation of "reference" and "value" types different from classes
versus structs (object vs. value types)?
Reference types include classes (including delegates) and interfaces.
Value types include structs and enums.

Can you always conclude that a
struct is a value type and a class is a reference type?


Yes
Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 23 '05 #4
Hi Ben,

I think mattias has given you a detailed reply. Do you have any concern on
this issue? Please feel free to tell me, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

Nov 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Paul Auleciems | last post by:
Hi: I'm having trouble using an Object which is created based on the following: Public CarDetail () as Car Where the CLASS "Car" is defined as: Public Class Car
1
by: alanrn | last post by:
I've implemented a number of strongly-typed collections that inherit from CollectionBase and recently noticed something that I don't fully understand. CollectionBase defines method RemoveAt(). ...
12
by: Ola Johansson | last post by:
Can anyone explain to me a meningfull use of the "new" keyword (Shadows in VB). I think i understand how it works fully but i cant figure out why you would use it. Why would you want to declare...
8
by: Dot net work | last post by:
I need VB.NET's "shadows" functionality inside a C# project. I tried the "new" keyword, but it didn't seem to work, because my particular function does in fact differ in signature to the function...
0
by: Michael Steidl | last post by:
I use XMLspy for handling XML files and started testing its feature to generate C# code from XML Schemas. Now I got stuck in a strange situation: (I have to add being not a C# geek, only currently...
5
by: Sam | last post by:
Hi everyone Could anyone help me understand the usage of the "New" keyword I'm new to VB.Net. 1. Why do we use the "New" keyword on some object type variables such as the myPen of the...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
6
by: vinnie | last post by:
i'm at the very beginning, and while trying to use the following small code, i got an error msg thta said use the "new" keyword to create an object instance. Actually, since i'm at the very...
30
by: Medvedev | last post by:
i see serveral source codes , and i found they almost only use "new" and "delete" keywords to make they object. Why should i do that , and as i know the object is going to be destroy by itself at...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.