Noob question on Class\New keyword ahead

C

cgough

My true programming language is C++. I am at best a VB6 hacker that is
just getting into VB.NET. I have a quick question about when to new and
when not to new.

Consider the following 2 classes. In the first I new an integer and
assign it to i, in the second one I don't bother. In both cases, an
integer is created and I can use it.

If I try to use a Collection object without New, I get a NULL reference
exception.

What I am looking for is some rules on when to New and when not to New.
Seems confusing ( from a C++ perspective) that in some cases
declaration instantiates an object and sometimes it doesn't.

Public Class Initialization

Public Sub New()
i = New Integer()
End Sub

Public Property Myint() As Integer
Get
Return i
End Get
Set(ByVal Value As Integer)
i = Value
End Set
End Property
Private i As Integer
End Class

Public Class Initialization

Public Sub New()
End Sub

Public Property Myint() As Integer
Get
Return i
End Get
Set(ByVal Value As Integer)
i = Value
End Set
End Property
Private i As Integer
End Class

Thanks,
Chris
 
C

Cor Ligthert

Chris,

Only objects from classes have (when the members are not shared) to be
instanced with new.

That is all.

I hope this helps,

Cor
 
H

Herfried K. Wagner [MVP]

Consider the following 2 classes. In the first I new an integer and
assign it to i, in the second one I don't bother. In both cases, an
integer is created and I can use it.

'Integer' is a value type. In VB.NET, its initial value is 0.
If I try to use a Collection object without New, I get a NULL reference
exception.

'Collection' is a reference type. A variable's initial value is a reference
to 'Nothing'.

'Nothing' is "overloaded" for value types and reference types. For value
types it stands for the type's default value (0 for numeric types), for
reference types it represents a 'NULL' reference.
What I am looking for is some rules on when to New and when not to New.

When dealing with value types, 'New' is never required, except you want to
call the type's parameterized constructor.
 
A

_AnonCoward

:
: My true programming language is C++. I am at best a VB6 hacker that is
: just getting into VB.NET. I have a quick question about when to new
: and when not to new.
:
: Consider the following 2 classes. In the first I new an integer and
: assign it to i, in the second one I don't bother. In both cases, an
: integer is created and I can use it.
:
: If I try to use a Collection object without New, I get a NULL
: reference exception.
:
: What I am looking for is some rules on when to New and when not to
: New. Seems confusing ( from a C++ perspective) that in some cases
: declaration instantiates an object and sometimes it doesn't.


Value types are initiated to some value by the framework. Reference
types aren't. Variables of a given value type must always contain a
value of that type. Variables of a reference type either contain a
refrence to an instance of that value type or a null reference.


From the SDK

----------------------------------------

The two fundamental categories of types in Visual Basic .NET are value
types and reference types. Primitive types, enumerations, and structures
are value types. Classes, strings, standard modules, interfaces, arrays,
and delegates are reference types.

With one exception, all types are either value types or reference types.
The root type Object, which is an alias for System.Object, is special in
that it is neither a reference type nor a value type, and may not be
instantiated. Thus, a variable of type Object can either contain a value
type or a reference type.

----------------------------------------

Although value types and reference types can be similar in terms of
declaration syntax, the semantics are distinct.

Reference types are stored on the run-time heap; they may only be
accessed through a reference to that storage. This allows the garbage
collector to track outstanding references to a particular instance and
free the instance when no references remain. A variable of reference
type always contains a reference to a value of that type or a null
reference. A null reference refers to nothing; it is invalid to do
anything with a null reference except assign it. Assignment to a
variable of a reference type creates a copy of the reference, not a copy
of the value being referenced.

Value types are stored directly on the stack, either within an array or
within another type. When the location containing a value type instance
is destroyed, the value type instance is also destroyed. Value types are
always accessed directly; it is not possible to create a reference to a
value type. Prohibiting such a reference makes it impossible to refer to
a value class instance that has been destroyed. A variable of a value
type always contains a value of that type. Unlike reference types, the
value of a value type cannot be a null reference, nor can it reference
an object of a more derived type. Assignment to a variable of a value
type creates a copy of the value being assigned.


: Public Class Initialization
:
: Public Sub New()
: i = New Integer()
: End Sub
:
: Public Property Myint() As Integer
: Get
: Return i
: End Get
: Set(ByVal Value As Integer)
: i = Value
: End Set
: End Property
: Private i As Integer
: End Class
:
: Public Class Initialization
:
: Public Sub New()
: End Sub
:
: Public Property Myint() As Integer
: Get
: Return i
: End Get
: Set(ByVal Value As Integer)
: i = Value
: End Set
: End Property
: Private i As Integer
: End Class
:
: Thanks,
: Chris
:
 
M

Mythran

Herfried K. Wagner said:
'Integer' is a value type. In VB.NET, its initial value is 0.


'Collection' is a reference type. A variable's initial value is a
reference to 'Nothing'.

'Nothing' is "overloaded" for value types and reference types. For value
types it stands for the type's default value (0 for numeric types), for
reference types it represents a 'NULL' reference.


When dealing with value types, 'New' is never required, except you want to
call the type's parameterized constructor.

--


Something I haven't looked into yet...but is it possible to create a
value-type class?

Mythran
 
A

_AnonCoward

<snip>

: Something I haven't looked into yet...but is it possible to
: create a value-type class?
:
: Mythran


Structures are value types.

Ralf
 
H

Herfried K. Wagner [MVP]

Mythran said:
Something I haven't looked into yet...but is it possible to create a
value-type class?

Value types are not called classes, they are called structures. You can
create them by declaring a structure:

\\\
Public Structure Foo
Public Age As Integer
Public Name As String
End Structure
///
 
A

_AnonCoward

Just for a little further clarification. The new keyword in C# can be
used to initialize (not instantiate) a value type. VB automatically
initializes variables to a default value if one isn't specified in the
code. C# does not. I assume the managed C++ compiler for .net behaves
like C# in this regard.


This is acceptable in vb:

Imports System
Public Class [class]
Public Shared Sub Main
Dim n As Integer
Console.WriteLine(n)
End Sub
End Class


However, the equivalent in C# will error on compile:

using System;
public class Class{
public static void Main(){
int n;
Console.WriteLine(n);
}
}

This generates the error "cs.cs(5,23): error CS0165: Use of unassigned
local variable 'n'".


Here are your choices in C#:

int n = 0;

int n;
n = 0;

int n = new int();


Ralf



: My true programming language is C++. I am at best a VB6 hacker that is
: just getting into VB.NET. I have a quick question about when to new
and
: when not to new.
:
: Consider the following 2 classes. In the first I new an integer and
: assign it to i, in the second one I don't bother. In both cases, an
: integer is created and I can use it.
:
: If I try to use a Collection object without New, I get a NULL
reference
: exception.
:
: What I am looking for is some rules on when to New and when not to
New.
: Seems confusing ( from a C++ perspective) that in some cases
: declaration instantiates an object and sometimes it doesn't.
:
: Public Class Initialization
:
: Public Sub New()
: i = New Integer()
: End Sub
:
: Public Property Myint() As Integer
: Get
: Return i
: End Get
: Set(ByVal Value As Integer)
: i = Value
: End Set
: End Property
: Private i As Integer
: End Class
:
: Public Class Initialization
:
: Public Sub New()
: End Sub
:
: Public Property Myint() As Integer
: Get
: Return i
: End Get
: Set(ByVal Value As Integer)
: i = Value
: End Set
: End Property
: Private i As Integer
: End Class
:
: Thanks,
: Chris
:
 
M

Mythran

Herfried K. Wagner said:
Value types are not called classes, they are called structures. You can
create them by declaring a structure:

\\\
Public Structure Foo
Public Age As Integer
Public Name As String
End Structure
///

Yes, I know a structure is a value type. I was thinking that a class
"could" be one if written correctly, but I seem to have been wrong. Are the
internal types (int, double, et cetera) written as structure's or some other
way?

Mythran
 
G

Guest

Mythran,

I'm not sure what you are asking here, but in many ways a structure is a
value-type class. With limitations, of course.

Kerry Moorman
 
H

Herfried K. Wagner [MVP]

Mythran said:
Yes, I know a structure is a value type. I was thinking that a class
"could" be one if written correctly, but I seem to have been wrong. Are
the internal types (int, double, et cetera) written as structure's or some
other way?

They are exposed as structure types, but in they are no structures.
 
M

Mythran

Herfried K. Wagner said:
They are exposed as structure types, but in they are no structures.

Ok, now back to my original question....is it possible to create a value
type class? Or are all classes, regardless of any attributes assigned to
them, reference types?

Thanks :)

Mythran
 
A

_AnonCoward

:
: : >
: >> Yes, I know a structure is a value type. I was thinking that a
: >>class "could" be one if written correctly, but I seem to have been
: >> wrong. Are the internal types (int, double, et cetera) written
: >> as structure's or some other way?
: >
: > They are exposed as structure types, but in they are no structures.
: >
: > --
: > M S Herfried K. Wagner
: > M V P <URL:http://dotnet.mvps.org/>
: > V B <URL:http://classicvb.org/petition/>
:
: Ok, now back to my original question....is it possible to create a
: value type class? Or are all classes, regardless of any attributes
: assigned to them, reference types?


To the best of my knowledge, if you define a class it will be treated as
a reference object in all cases.

Ralf
 
J

Jay B. Harlow [MVP - Outlook]

Mythran,
| Ok, now back to my original question....is it possible to create a value
| type class?
No, as you need to inherit from System.ValueType in order to be a value
type. Only Structure is allowed to implicitly inherit from System.ValueType.
The only "exception" is Enum, as Enums implicitly inherit from System.Enum,
which inherits from System.ValueType.


| Or are all classes, regardless of any attributes assigned to
| them, reference types?
All Classes are Reference Types, just as all Structures are Value Types.

Hope this helps
Jay


|
| | >> Yes, I know a structure is a value type. I was thinking that a class
| >> "could" be one if written correctly, but I seem to have been wrong.
Are
| >> the internal types (int, double, et cetera) written as structure's or
| >> some other way?
| >
| > They are exposed as structure types, but in they are no structures.
| >
| > --
| > M S Herfried K. Wagner
| > M V P <URL:http://dotnet.mvps.org/>
| > V B <URL:http://classicvb.org/petition/>
|
| Ok, now back to my original question....is it possible to create a value
| type class? Or are all classes, regardless of any attributes assigned to
| them, reference types?
|
| Thanks :)
|
| Mythran
|
 

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