Thursday, March 22, 2012

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?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 Dring" <cdoering22@.gmx.de> wrote in message
news:uSxwLr0YGHA.444@.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?
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:ekpQI30YGHA.1192@.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 Dring" <cdoering22@.gmx.de> wrote in message
> news:uSxwLr0YGHA.444@.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?
>>
"Kevin Spencer" <kevin@.DIESPAMMERSDIEtakempis.com> schrieb im Newsbeitrag
news:uoW3Ty5YGHA.4916@.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:ekpQI30YGHA.1192@.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!

0 comments:

Post a Comment