473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Short Question on Performance

Hi,

Is it ok to create a new instance of a class on every Page_Load?

Given the following code behind a form:

protected void Page_Load(object sender, EventArgs e)
{
myclass= new MyClass(myControl);
}

protected void btnStart_Click(object sender, EventArgs e)
{
myclass.loadXYZ(0);
}

And the snippet from MyClass

public MyClass (MyControl c)
{
this.mycontrol = c;
}
public loadXYZ(int x)
{
...
c.text="abc"
}

So I need a reference to myControl which is on the form. My first approach
was to just pass it by the constructor of MyClass on the Page_Load event. Is
this just fine or do I loose performance?
Apr 19 '06 #1
3 852
It should be ok.

Object creation is only heavy if you are dong heavy stuff in the
contstructor...which you don't seem to be doing...ur just assigning a
reference..

Karl

--
http://www.openmymind.net/

"Christian Döring" <cd********@gmx.de> wrote in message
news:uS*************@TK2MSFTNGP05.phx.gbl...
Hi,

Is it ok to create a new instance of a class on every Page_Load?

Given the following code behind a form:

protected void Page_Load(object sender, EventArgs e)
{
myclass= new MyClass(myControl);
}

protected void btnStart_Click(object sender, EventArgs e)
{
myclass.loadXYZ(0);
}

And the snippet from MyClass

public MyClass (MyControl c)
{
this.mycontrol = c;
}
public loadXYZ(int x)
{
...
c.text="abc"
}

So I need a reference to myControl which is on the form. My first approach
was to just pass it by the constructor of MyClass on the Page_Load event.
Is this just fine or do I loose performance?

Apr 19 '06 #2
I might also mention that *every* object in the Page is re-created with each
Page_Load, including the Page class itself.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.

"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:ek**************@TK2MSFTNGP04.phx.gbl...
It should be ok.

Object creation is only heavy if you are dong heavy stuff in the
contstructor...which you don't seem to be doing...ur just assigning a
reference..

Karl

--
http://www.openmymind.net/

"Christian Döring" <cd********@gmx.de> wrote in message
news:uS*************@TK2MSFTNGP05.phx.gbl...
Hi,

Is it ok to create a new instance of a class on every Page_Load?

Given the following code behind a form:

protected void Page_Load(object sender, EventArgs e)
{
myclass= new MyClass(myControl);
}

protected void btnStart_Click(object sender, EventArgs e)
{
myclass.loadXYZ(0);
}

And the snippet from MyClass

public MyClass (MyControl c)
{
this.mycontrol = c;
}
public loadXYZ(int x)
{
...
c.text="abc"
}

So I need a reference to myControl which is on the form. My first
approach was to just pass it by the constructor of MyClass on the
Page_Load event. Is this just fine or do I loose performance?


Apr 19 '06 #3
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> schrieb im Newsbeitrag
news:uo**************@TK2MSFTNGP04.phx.gbl...
I might also mention that *every* object in the Page is re-created with
each Page_Load, including the Page class itself. Okay, nice to know, that I do nothing unusual here. ;-)
"Karl Seguin [MVP]" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net> wrote in message news:ek**************@TK2MSFTNGP04.phx.gbl...
It should be ok.

Object creation is only heavy if you are dong heavy stuff in the
contstructor...which you don't seem to be doing...ur just assigning a
reference..

No heavy stuff in there...

Thank you both for your help!
Apr 19 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Justin Hoffman | last post by:
This is a question concerning query optimisation. Sorry if it's a bit long, but thanks to anyone who has the patience to help - This is my first post here... If I have two tables:...
99
by: Glen Herrmannsfeldt | last post by:
I was compiling a program written by someone else about six years ago, and widely distributed at the time. It also includes makefiles for many different systems, so I know it has been compiled...
34
by: Andy | last post by:
Hi, Are 1 through 4 defined behaviors in C? unsigned short i; unsigned long li; /* 32-bit wide */ 1. i = 65535 + 3; 2. i = 1 - 3; 3. li = (unsigned long)0xFFFFFFFF + 3; 4. li = 1...
6
by: Raymond Lewallen | last post by:
I understand the differences between And and AndAlso and the differences between Or and OrElse. Under what circumstance would you NOT use short-circuiting when evaluating expressions? It seems to...
5
by: Phil Jones | last post by:
I'm just (as a matter of course) using AndAlso and OrElse statements to short circuit checking, even for nominal things like Boolean comparisons. I'm wondering, is that a good thing to do (for...
2
by: Stefan Thieme | last post by:
I'm using a wsdl.exe-generated class to access a web service on the LAN. In case of short messages there is a 200 ms latency per call. If running in the debugger (adds debug info to the SOAP...
8
by: avsrk | last post by:
Hello Folks , General C data types question , more geared up towards embedded folks . I have a positive float quantity with a fractional part (one decimal point) which occupies 4 bytes ....
8
by: NvrBst | last post by:
is there anyway to use bytes with numbers without actually casting at the end? byte Test = 10; byte row = Test - 5; Return Error: Cannot implicitly convert type 'int' to 'byte'. An explicit...
1
by: edi | last post by:
Hi! We have SQL Server 2000 on virtual machine (slowly). We run a pair of stored procedures from ADO.NET 2.0 (WebService): - first procedure: insert into table A a data row with correlationID -...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.