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 10 1239
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 :).
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 :).
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
"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/>
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
|
|
|
|
|
|
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 :).
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/>
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 | | | | | |
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Stuart P |
last post by:
Hello,
I am making my first real game in C++ and there is a problem with the class
used to contain a level. The level::load(filename) function gives an Access
Violation error when trying to...
|
by: matthurne |
last post by:
I'm doing a chapter 12 exercise from Accelerated C++ ... writing a
string-like class which stores its data in a low-level way. My class,
called Str, uses a char array and length variable. I've...
|
by: DotNetShadow |
last post by:
Hi Guys,
I'm trying to work out how events work in VB.NET Basically I want to
create a base class that has an Event. I would like all derived
classes to inherit this event. I sorta worked out...
|
by: Scott H. |
last post by:
I have aVB.NET application that uses an SQL Server wrapper class to connect
and perform any and all ADO.NET database activities. This is an MDI child
based application where the child forms are in...
|
by: Corey B |
last post by:
Is there a way for an instance of a custom class to access an ASPX page
level variable? I know that I can access a Session variable from
within a class using the following code:
myClassVar =...
|
by: Stephen Torri |
last post by:
I am trying to produce a singleton class that I can use throughout my
library to write tracing information to a file. My intent was to design
such that someone using the library in its debug mode...
|
by: =?Utf-8?B?R2Vvcmdl?= |
last post by:
Hello everyone,
I met with a strange issue that derived class function can not access base
class's protected member. Do you know why?
Here is the error message and code.
error C2248:...
|
by: Armin Zingler |
last post by:
Hi,
after dealing with the new possiblities of the current VB version, I again
and still wonder why Option Infer can not be used with Option Strict On
at class level, i.e. when declaring a...
|
by: Dann Corbit |
last post by:
Rather than create a new way of doing things:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2497.html
why not just pick up ACE into the existing standard:...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |