关于Class \New关键字的Noob问题 [英] Noob question on Class\New keyword ahead

查看:56
本文介绍了关于Class \New关键字的Noob问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真正的编程语言是C ++。我充其量只是进入VB.NET的VB6黑客。我有一个关于什么时候换新的快速问题和

,当不是新的时候。


考虑以下2个班级。在第一个我新的整数和

分配给我,在第二个我不打扰。在这两种情况下,都会创建一个

整数,我可以使用它。


如果我尝试使用不带New的Collection对象,我会得到一个NULL引用

例外。


我要找的是关于什么时候到新的以及什么时候不到新的规则。

似乎令人困惑(从C ++的角度来看)在某些情况下,

声明实例化一个对象,有时它不会。


公共类初始化


Public Sub New()

i = New Integer()

End Sub


公共财产Myint ()作为整数

获取

返回i

结束获取

设置(ByVal值为整数)

i =价值

结束集

结束物业

私有我作为整数

结束类


公共类初始化


Public Sub New()

End Sub


公共财产Myint()作为整数

获取

返回i

结束获取

设置(ByVal值为整数)

i = Value

结束集

结束财产

私有我作为整数

结束班


谢谢,

克里斯

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

推荐答案

Chris,


只有来自类的对象(当成员不共享时)用新的

实例。


就是这样。


我希望这有帮助,


Cor
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


< cg **** @ micros.com> schrieb:
<cg****@micros.com> schrieb:
考虑以下2个类。在第一个我新的整数和
分配给我,在第二个我不打扰。在这两种情况下,都会创建一个
整数,我可以使用它。


''整数''是一种价值类型。在VB.NET中,它的初始值为0.

如果我尝试使用不带New的Collection对象,我会得到一个NULL引用
异常。


''Collection''是一种参考类型。变量'的初始值是参考

到''没什么''。


''没什么''是'重载'对于值类型和引用类型。对于值

类型,它代表类型的默认值(数字类型为0),对于

引用类型,它代表''NULL''引用。 br />
我正在寻找的是关于什么时候到新的以及什么时候不到新的规则。
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.




在处理价值类型时,''新的''永远不需要,除非你想要

调用类型的参数化构造函数。


-

MS Herfried K. Wagner

MVP< URL:http://dotnet.mvps.org/>

VB< URL:http://classicvb.org/请愿/>



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




< cg **** @ micros.com>在消息中写道

新闻:11 ********************** @ z14g2000cwz.googlegr oups.com ...



:我真正的编程语言是C ++。我充其量只是一个VB6黑客,它只是进入VB.NET。我有一个简短的问题,关于什么时候新的

:什么时候不要新的。



:考虑以下2个班级。在第一个我新的整数和

:分配给我,在第二个我不打扰。在这两种情况下,都会创建一个

:整数,我可以使用它。



:如果我尝试使用没有Collection对象新的,我得到一个空白

:引用异常。



:我要找的是关于什么时候到新的什么时候的规则不要

:新的。似乎令人困惑(从C ++的角度来看)在某些情况下

:声明实例化一个对象,有时它不会。

值类型由某个值启动框架。参考

类型不是。给定值类型的变量必须始终包含该类型的

值。引用类型的变量包含对该值类型的实例或空引用的

引用。

来自SDK


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


Visual Basic .NET中两个基本类型的类型是值

类型和引用类型。原始类型,枚举和结构

是值类型。类,字符串,标准模块,接口,数组,

和委托是引用类型。


除了一个例外,所有类型都是值类型或引用类型。

根类型Object,它是System.Object的别名,在

中是特殊的,它既不是引用类型也不是值类型,可能不是

实例化。因此,Object类型的变量可以包含值

类型或引用类型。


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


虽然值类型和引用类型可以类似于

声明语法的术语,语义是不同的。


引用类型存储在运行时堆上;它们可能只是通过对该存储的引用来访问
。这允许垃圾

收集器跟踪对特定实例的未完成引用,并且当没有引用时,
释放该实例。引用变量

类型始终包含对该类型值的引用或null

引用。空引用无效;

除了赋值之外的任何空引用都是无效的。分配给引用类型的

变量会创建引用的副本,而不是引用值的副本




值类型直接存储在堆栈中,在数组中或在另一种类型中的
。当销毁包含值类型实例

的位置时,也会销毁值类型实例。值类型是

总是直接访问;无法创建对

值类型的引用。禁止这样的引用使得无法将已销毁的值类实例引用到

。值的变量

类型始终包含该类型的值。与引用类型不同,值类型的

值不能是空引用,也不能引用更多派生类型的对象
。分配给值的变量

类型会创建所分配值的副本。

:公共类初始化



:Public Sub New()

:i = New Integer()

:End Sub



:公共财产Myint()作为整数

:获取

:返回我

:结束获取

:设置(ByVal值为整数)

:i =价值

:结束集

:结束物业

:私有我作为整数

:结束类



:公共类初始化



:Public Sub New()

:End Sub



:公共属性Myint()As Integer

:获取

:返回我

:结束获取

:设置(ByVal值为整数)

:i =价值

:结束集

:结束物业

:私有我作为整数

:结束班级



:谢谢,

:Chris





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



这篇关于关于Class \New关键字的Noob问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆