By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,745 Members | 1,382 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,745 IT Pros & Developers. It's quick & easy.

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

P: n/a


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


Nov 21 '05 #1
Share this Question
Share on Google+
10 Replies

P: n/a
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


Nov 21 '05 #2

P: n/a
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



Nov 21 '05 #3

P: n/a
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 :).

Nov 21 '05 #4

P: n/a
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.

"Jerod Venema" <jv*****@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
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 :).

Nov 21 '05 #5

P: n/a
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?
--
Dennis in Houston
"msnews.microsoft.com" wrote:


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


Nov 21 '05 #6

P: n/a
"Marina" <so*****@nospam.com> schrieb:
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.
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


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

Nov 21 '05 #7

P: n/a
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
"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
|
|
|
|
|
|
Nov 21 '05 #8

P: n/a
thank you for the distinction Jerod. That is very important.
-hazz

"Jerod Venema" <jv*****@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
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 :).

Nov 21 '05 #9

P: n/a
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

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:u2*************@TK2MSFTNGP09.phx.gbl...
"Marina" <so*****@nospam.com> schrieb:
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.
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


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

Nov 21 '05 #10

P: n/a
Thank you Jay. -hazz

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:O%****************@TK2MSFTNGP15.phx.gbl...
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
"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
|
|
|
|
|
|

Nov 21 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.