instantion of class at class level or in new()?

  • Thread starter Thread starter msnews.microsoft.com
  • Start date Start date
M

msnews.microsoft.com

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.
 
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 :).
 
Exception handling is a different topic.

The question had to do with the timing of instantiation.

Exception handling is not related to the timing of instantiating - just the
logic of your program.

Of course when you have a method to put code in, you will have a lot more
flexibility in what you can do, as opposed to a declaration of a variable
line.
 
I thought that if you dim a variable witnin a sub, it was local and could
only be used in that sub such that if you dim mx in the sub new, it is only
local to that sub. Isn't this correct?
 
Marina said:
It is equivalent. Either way the variable will be instantiated when the
object is created.

It's not equivalent. 'mx' is declared in a larger scope and has a longer
lifetime.
 
Hazz,
As Dennis & Herfried suggests (I just noticed it myself).

| Dim mx As New HelperClass 'here, where I have it now ????
|
| Sub New()
| Dim mx As New HelperClass
| End Sub

The mx variable in New is local to New, it is at an entirely different scope
then the mx field. The mx field has the lifetime of the object itself, the
mx local variable only has the lifetime of the constructor.

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

I suspect you meant to initialize it in New, something like:

| Sub New()
| mx = New HelperClass
| End Sub

In which case as Marina suggested they are equivalent, I normally do my
initialization in the constructor as it allows all the initialization to be
done in one place, consistently.

Private ReadOnly m_name = String
| Private m_mx As HelperClass

| Sub New(ByVal name As String)
m_name = name
| mx = New HelperClass
| End Sub

However it is based more on personal preference then any technical reason.

Hope this helps
Jay


|
|
| 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 Herfried for that clarification. I can see that memory 'might' be
adversely affectived given the longer lifetime. If I want to keep memory as
optimized (no accumulation of memory, ie. memory leaks) then I should make
sure the object 'goes away' when it no longer is useful.
I will have to look at the overall process once again.
-hazz
 

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

Back
Top