Newbie Question on Object and the "New" Keyword

S

Sam

Hi all

I asked a similar question a few days ago but I'm still confused. I'm new to
VB.Net so could someone please explain the following:

According to the .Net documents posted on the MS website "To create an
instance of a class, use the New keyword. Unlike value types, such as
Integer and Double, objects are reference types, and you must explicitly
create them before you can use them".
1. Can an object be both reference and value types?
2. Aren't Integer and Double objects (isn't everything in .Net object so
Integer and Double are reference types?)
3. If objects are reference type, then why the Size class, a value type
(Structure in Visual Basic, struct in C#) needs to use the New keyword to
create an instance of its class

Thank you

Sam
 
M

Mattias Sjögren

1. Can an object be both reference and value types?
No


2. Aren't Integer and Double objects (isn't everything in .Net object so
Integer and Double are reference types?)

Everything can be treated as an Object. But for that to happen for
value types such as Integer, they must be boxed. Boxing is the process
of taking a value type variable and wrapping it in an object on the
heap.

3. If objects are reference type, then why the Size class, a value type
(Structure in Visual Basic, struct in C#) needs to use the New keyword to
create an instance of its class

The following works fine for me, without using New.

Dim s As Size
s.Height = 42
Console.WriteLine(s)



Mattias
 
C

Cor Ligthert

Sam,

In addition to Mattias and Jonathan there are two confusing values in VBNet.
DateTime and String.

It are objects however follow the rules of values.

For the rest is it all regular as you saw in the messages.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top