我应该使用“Dim ___ As New ___”吗? [英] Should I Use "Dim ___ As New ___"?

查看:81
本文介绍了我应该使用“Dim ___ As New ___”吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我是VB6er,他最近开始使用VB2005;我有一个关于变量声明的一般性问题

。在VB中,我总是试图避免使用

As New。构造时声明变量而不是在类''或形式'的初始化事件中或根据需要实例化
实例化。使用

" As New"在我的对象的生命周期中造成了不必要的开销并导致失去控制




.Net中的这种可行做法是什么?

TIA

~Tim

Hi All,

I''m a VB6er who''s recently started using VB2005; I have a general question
about variable declaration. In VB, I''ve always tried to avoid using the
"As New" construct when declaring a variable preferring instead to
instantiate in the class'' or form''s initialize event or as needed. Using
"As New" created unnecessary overhead and resulted in a loss of control
over my object''s life cycle.

Is this advisable practice in .Net?
TIA
~Tim

推荐答案

首先声明第一个并实例化第二个

*可能*在某些情况下有用。除此之外,它没有什么价值。


Tim Baur < TR ********** @ ARDyahoo.com>在消息中写道

新闻:Xn ********************************** @ 207.46 .2 48.16 ...
There is a bit more flexibility in declaring first and instantiating second
that *may* be usefull in certain scenarios. Other than this, it doesn''t
matter.

"Tim Baur" <tr**********@ARDyahoo.com> wrote in message
news:Xn**********************************@207.46.2 48.16...
大家好,

我是VB6er,最近开始使用VB2005;我有一个关于变量声明的一般性问题。在VB中,我总是试图避免使用
As New。构造时声明变量而不是在类''或形式'初始化事件中实例化或根据需要实例化。使用
As New造成了不必要的开销,导致失去对物体生命周期的控制。

这是明智的做法吗?

TIA
~Tim
Hi All,

I''m a VB6er who''s recently started using VB2005; I have a general question
about variable declaration. In VB, I''ve always tried to avoid using the
"As New" construct when declaring a variable preferring instead to
instantiate in the class'' or form''s initialize event or as needed. Using
"As New" created unnecessary overhead and resulted in a loss of control
over my object''s life cycle.

Is this advisable practice in .Net?
TIA
~Tim



除非使用该类的共享成员,否则需要使用New。

new关键字创建一个引用对于您正在使用的对象。

Using New is required except when using shared members of the class.
The new keyword creates a reference to the object that you are using.


" Tim Baur" < TR ********** @ ARDyahoo.com> schrieb
"Tim Baur" <tr**********@ARDyahoo.com> schrieb
大家好,

我是一个最近开始使用VB2005的VB6er;我对变量声明有一般性的问题。在VB中,我总是试图避免使用As New和As New。当声明一个变量而不是在类''或形式'的初始化
事件中或根据需要实例化时构造。使用As New造成了不必要的开销,导致失去对对象生命周期的控制。

.Net中这个可行的做法是什么?
Hi All,

I''m a VB6er who''s recently started using VB2005; I have a general
question about variable declaration. In VB, I''ve always tried to
avoid using the "As New" construct when declaring a variable
preferring instead to instantiate in the class'' or form''s initialize
event or as needed. Using "As New" created unnecessary overhead and
resulted in a loss of control over my object''s life cycle.

Is this advisable practice in .Net?



行为不同。在VB6中,As New表示一旦访问变量就会创建

对象。在VB.Net中,这不再存在
了。现在它意味着只要创建了

变量就会创建对象。所以,如果你在一个程序中写了一个新的形式


,那就和写作完全相同


dim bla as form

bla =新表格

如果你在班级宣布它,它与写作完全相同


dim bla as form

''...

sub新()

bla =新表格< br $> b $ b end sub

参见:
http://msdn.microsoft.com/library/en...ctCreation.asp

虽然,有时候不可取,但这是个人品味。我总是

假设声明的顺序无关紧要。如果声明包含New语句,则不再是
。例如:


dim con as new oledbconnection(...)

dim cmd as new oledbcommand(con)


如果您更改订单,您会看到该应用程序将失败,因为第二个

语句取决于第一个。


Armin



The behavior is different. In VB6, "As New" means that the
object is created as soon as the variable is accessed. In VB.Net this does
not exist anymore. Now it means that the object is created as soon as the
variable is created. So, if you write

dim bla as new form

in a procedure, it is exactly the same as writing

dim bla as form
bla = new form
If you declare it at class level, it is exactly the same as writing

dim bla as form
''...
sub New()
bla = new form
end sub
See also:
http://msdn.microsoft.com/library/en...ctCreation.asp
Though, it is sometimes not advisable, but this is personal taste. I always
assume that the order of declaration doesn''t matter. This is not true
anymore if the declaration contains a New statement. Example:

dim con as new oledbconnection(...)
dim cmd as new oledbcommand(con)

You see that the app will fail if you change the order because the 2nd
statement depends on the 1st one.

Armin


这篇关于我应该使用“Dim ___ As New ___”吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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