在类级别或在new()中类的实例? [英] instantion of class at class level or in new()?

查看:74
本文介绍了在类级别或在new()中类的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




公共舱控制器


Dim mx作为新HelperClass''在这里,我现在拥有它????


子新()


''或者这里??? Dim mx As New HelperClass


End Sub


*************** ******************************* *************** *******


我正在创建一个Windows服务,我想确保我有效率

带内存。


我从

windows服务中的Timer1_Elapsed事件调用上面的Controller类。


我应该在课堂上实例化HelperClass我现在或者在

新的水平()?


以某种方式做到这一点会有什么影响?


谢谢,


-hazz

解决方案

相当于。无论哪种方式,变量将在创建

对象时实例化。


" msnews.microsoft.com" < hazz @声波@净>在留言中写道

news:ec ************* @ TK2MSFTNGP15.phx.gbl ...



Dim mx作为新的HelperClass''在这里,我现在拥有它????

Sub New()
''或者这里??? Dim mx As New HelperClass


结束子

*********************** *************************** **********************

我正在创建一个Windows服务,我想确保我对内存有效。

我从我的Timer1_Elapsed事件调用上面的Controller类
windows服务。

我应该在课堂上实例化HelperClass,还是在New()中实现?

什么是以某种方式做到这一点的含义?

谢谢,





谢谢Marina。有道理。

欣赏,-hazz


" Marina" <所以***** @ nospam.com>在消息中写道

新闻:%2 **************** @ TK2MSFTNGP15.phx.gbl ...

它是当量。无论哪种方式,变量都将在创建
对象时实例化。

" msnews.microsoft.com" < hazz @声波@净>在消息中写道
新闻:ec ************* @ TK2MSFTNGP15.phx.gbl ...



公共课控制器

Dim mx作为新的HelperClass''在这里,我现在拥有它????

Sub New()

''或者这里??? Dim mx As New HelperClass


结束子

*********************** *************************** **********************

我正在创建一个Windows服务,我想确保我对内存有效。

我从我的Timer1_Elapsed事件调用上面的Controller类
windows服务。

我应该在课堂上实例化HelperClass,还是在New()中实现?

什么是以某种方式做到这一点的含义?

谢谢,






不太...


采取这种情况:


公开班长控制器


Dim mx作为新HelperClass


Sub New()


End Sub < br $>
结束班


公共班HelperClass


Sub New()

抛出新例外(处理我! ")

结束次级


结束班级


现在,如果你离开你拥有它的实例现在,你不能这样做。

a尝试......抓住它,吸引力打破了一切。但是,如果你将实例化拉到New()子项中,你可以试一试

catch ...就像这样。


公共舱控制器


Dim mx As HelperClass


Sub New()

试试

mx = New HelperClass

Catch ex as Exception

Console.Writeline(抓住你!& ex.Message)

结束尝试


结束子


结束班


公共类HelperClass


Sub New()

抛出新例外(处理我!)

End Sub


结束课


好​​多了:)。




Public Class Controller

Dim mx As New HelperClass ''here, where I have it now ????

Sub New()

''or here??? Dim mx As New HelperClass


End Sub

************************************************** **********************

I am creating a windows service and I want to make sure I am being efficient
with memory.

I call the Controller class above from my Timer1_Elapsed event in the
windows service.

Should I instantiate HelperClass at the class level as I have it now or in
the New()?

What are the implications of doing it one way or the other?

Thank you,

-hazz


解决方案

It is equivalent. Either way the variable will be instantiated when the
object is created.

"msnews.microsoft.com" <hazz@sonic@net> wrote in message
news:ec*************@TK2MSFTNGP15.phx.gbl...



Public Class Controller

Dim mx As New HelperClass ''here, where I have it now ????

Sub New()

''or here??? Dim mx As New HelperClass


End Sub

************************************************** **********************

I am creating a windows service and I want to make sure I am being
efficient with memory.

I call the Controller class above from my Timer1_Elapsed event in the
windows service.

Should I instantiate HelperClass at the class level as I have it now or
in the New()?

What are the implications of doing it one way or the other?

Thank you,

-hazz



thank you Marina. Makes sense.
appreciatively, -hazz

"Marina" <so*****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP15.phx.gbl...

It is equivalent. Either way the variable will be instantiated when the
object is created.

"msnews.microsoft.com" <hazz@sonic@net> wrote in message
news:ec*************@TK2MSFTNGP15.phx.gbl...



Public Class Controller

Dim mx As New HelperClass ''here, where I have it now ????

Sub New()

''or here??? Dim mx As New HelperClass


End Sub

************************************************** **********************

I am creating a windows service and I want to make sure I am being
efficient with memory.

I call the Controller class above from my Timer1_Elapsed event in the
windows service.

Should I instantiate HelperClass at the class level as I have it now or
in the New()?

What are the implications of doing it one way or the other?

Thank you,

-hazz




Not quite...

Take this scenario:

Public Class Controller

Dim mx As New HelperClass

Sub New()

End Sub

End Class

Public Class HelperClass

Sub New()
Throw New Exception("Handle me!")
End Sub

End Class

Now, if you leave the instantiation where you have it now, you can''t do
a Try...Catch on it, and the intatiation breaks everything. However, if
you pull the instantiation into the New() sub, you can place a try
catch...like this.

Public Class Controller

Dim mx As HelperClass

Sub New()
Try
mx = New HelperClass
Catch ex as Exception
Console.Writeline("Caught you! " & ex.Message)
End Try

End Sub

End Class

Public Class HelperClass

Sub New()
Throw New Exception("Handle me!")
End Sub

End Class

Much better :).


这篇关于在类级别或在new()中类的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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