473,503 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Noob question on Class\New keyword ahead

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

Nov 21 '05 #1
13 1535
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
Nov 21 '05 #2
<cg****@micros.com> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3

<cg****@micros.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
:
: 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
:


Nov 21 '05 #4

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:ub**************@TK2MSFTNGP14.phx.gbl...
<cg****@micros.com> schrieb:
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.

--

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

Mythran

Nov 21 '05 #5
"Mythran" <ki********@hotmail.comREMOVETRAIL> schrieb:
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
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #6

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

<cg****@micros.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
: 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
:

Nov 21 '05 #7

"Mythran" <ki********@hotmail.comREMOVETRAIL> wrote in message
news:eX*************@TK2MSFTNGP12.phx.gbl...

<snip>

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

Ralf
Nov 21 '05 #8

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:uE**************@TK2MSFTNGP14.phx.gbl...
"Mythran" <ki********@hotmail.comREMOVETRAIL> schrieb:
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
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>


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

Nov 21 '05 #9
"Mythran" <ki********@hotmail.comREMOVETRAIL> schrieb:
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/>

Nov 21 '05 #10

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
"Mythran" wrote:

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

Mythran

Nov 21 '05 #11

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:eb**************@TK2MSFTNGP15.phx.gbl...
"Mythran" <ki********@hotmail.comREMOVETRAIL> schrieb:
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

Nov 21 '05 #12
"Mythran" <ki********@hotmail.comREMOVETRAIL> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
:
: "Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
: news:eb**************@TK2MSFTNGP15.phx.gbl...
: >
: > "Mythran" <ki********@hotmail.comREMOVETRAIL> schrieb:
: >> 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
Nov 21 '05 #13
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
"Mythran" <ki********@hotmail.comREMOVETRAIL> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
|
| "Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
| news:eb**************@TK2MSFTNGP15.phx.gbl...
| > "Mythran" <ki********@hotmail.comREMOVETRAIL> schrieb:
| >> 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
|
Nov 21 '05 #14

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

Similar topics

8
2060
by: Rich Grise | last post by:
I think I've finally found a tutorial that can get me started: http://www.zib.de/Visual/people/mueller/Course/Tutorial/tutorial.html and I've been lurking for awhile as well. What happened is,...
4
1696
by: shumaker | last post by:
I'm wondering how/why this query works. Trying to get my head wrapped around SQL. Basically the Query deletes from the Import table all records that are already in FooStrings so that when I do an...
59
3874
by: seberino | last post by:
I've heard 2 people complain that word 'global' is confusing. Perhaps 'modulescope' or 'module' would be better? Am I the first peope to have thought of this and suggested it? Is this a...
8
2131
by: Ivan Shevanski | last post by:
Alright heres another noob question for everyone. Alright, say I have a menu like this. print "1. . .Start" print "2. . .End" choice1 = raw_input("> ") and then I had this to determine what...
3
1989
by: flat_ross | last post by:
For anyone who is just getting into VB.NET and/or is starting to work with inheritance I would like to point out a potential pitfall. We found this confusion recently when code-reviewing an...
4
1879
by: foker | last post by:
I have an array with 50 elements in it, and a huge document with like 35,000 words on it. What I want to do is count the number of times each element has appeared in the document. This is what I...
2
2834
by: Link360 | last post by:
Im a complete noob and im proud of it. I am excited in learning everything about the C++ language. Right now im trying to make tic-tac-toe. Go ahead laugh. here is what i have so far ...
7
1910
by: DJP | last post by:
Hi, I had sort of a noob question on memory allocation for strings. char *str1 = "Hello World!"; char str2 = "Hello World!"; In the above bit are both str1 & str2 stack allocated or heap...
4
1206
by: jobs | last post by:
Three noob questions. Please help. 1. Is there a way to have arguments on subroutine that are optional? 2. Say I need to convert datetime to string..in this format : 2006-09-07...
0
7202
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
7086
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
7332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5014
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
4673
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...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
382
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.