Using new when declaring an object, is it OK?

M

MBALOVER

Hi all,

I am a new bee in OOP. In C#, assume I have two version of the class
Form1

Version 1:

public class Form1: Form
{
Button mybutton= new Button();

public void Form1
{
this.mybutton.Text="aaaa";
this.Controls.Add(mybutton);
}

}

And Version 2:

public class Form1: Form
{
Button mybutton;

public void Form1
{
this.mybutton=new Button();
this.mybutton.Text="aaaa";
this.Controls.Add(mybutton);
}

}

The difference between the two versions is the declaration of the
object mybutton. In Version 1, the mybutton is assigned a memory
outside the constructor function while in Version 2 it is assigned a
memory inside. (here, with assigning a memory location, I mean the
command new).

So can you please tell me if the two versions perform exactly the
same, or there is some difference that I should note about?

Thanks
 
P

Peter Duniho

MBALOVER said:
[...]
The difference between the two versions is the declaration of the
object mybutton. In Version 1, the mybutton is assigned a memory
outside the constructor function while in Version 2 it is assigned a
memory inside. (here, with assigning a memory location, I mean the
command new).

So can you please tell me if the two versions perform exactly the
same, or there is some difference that I should note about?

There are some subtle differences between the two. But to a first
approximation, they are identical. They will both result in that field
being initialized when the object is created.

Pete
 
M

MBALOVER

Hi Pete

Thanks for answer. I would like to learn a little bit deeper than just
a simple use. So can you please tell me one of the most significant
differences between the two versions, and do you have in mind what
advantages that make someone chooses one version over than the other?

What I am thinking is that whenever I declare an object from a Class,
for example Form1 myform;, I often have to use Form1 myform new
Form1() that calls the constructor function of the Form1 class.

Now with version 1, when I declare Form1 myform; without the
constructor call, do you think that the object mybutton is created and
can be accessed?

Thanks

MBALOVER said:
[...]
The difference between the two versions is the declaration of the
object mybutton. In Version 1, the mybutton is assigned a memory
outside the constructor function while in Version 2 it is assigned a
memory inside. (here, with assigning a memory location, I mean the
command new).
So can you please tell me if the two versions perform exactly the
same, or there is some difference that I should note about?

There are some subtle differences between the two.  But to a first
approximation, they are identical.  They will both result in that field
being initialized when the object is created.

Pete
 
P

Peter Duniho

MBALOVER said:
Hi Pete

Thanks for answer. I would like to learn a little bit deeper than just
a simple use. So can you please tell me one of the most significant
differences between the two versions,

The most significant differences is that field initializers are executed
from most-derived class to least-derived, followed by constructors being
executed from least-derived class to most.

In other words, by the time the first constructor is executed, _all_
field initializers have been executed.
and do you have in mind what
advantages that make someone chooses one version over than the other?

The primary consideration is what you feel is most readable. The order
of initialization of a field should not matter, except when initialized
in a constructor and even then only with respect to what else in that
constructor may require use of the field. That latter issue being
identical to order of initialization in _any_ method is IMHO a non-issue
with respect to the decision of using a field initializer versus
initialization in a constructor.
What I am thinking is that whenever I declare an object from a Class,
for example Form1 myform;, I often have to use Form1 myform new
Form1() that calls the constructor function of the Form1 class.

You don't "often" have to use "new Form1()". You _always_ have to use
"new Form1()".
Now with version 1, when I declare Form1 myform; without the
constructor call, do you think that the object mybutton is created and
can be accessed?

You cannot create an object without the constructor being called. It's
not possible.

If your class has multiple constructors, you will need to either ensure
that each constructor does the necessary initialization or (much
preferably) there is a common constructor or other initialization method
that is called by each of the public constructors.

With a correctly designed class, it is not possible for client code to
use an instance of that class without it being correctly initialized,
and this is true whether you use field initializers for initialization
or not.

Pete
 
A

Arne Vajhøj

I am a new bee in OOP. In C#, assume I have two version of the class
Form1

Version 1:

public class Form1: Form
{
Button mybutton= new Button();

public void Form1
{
this.mybutton.Text="aaaa";
this.Controls.Add(mybutton);
}

}

And Version 2:

public class Form1: Form
{
Button mybutton;

public void Form1
{
this.mybutton=new Button();
this.mybutton.Text="aaaa";
this.Controls.Add(mybutton);
}

}

The difference between the two versions is the declaration of the
object mybutton. In Version 1, the mybutton is assigned a memory
outside the constructor function while in Version 2 it is assigned a
memory inside. (here, with assigning a memory location, I mean the
command new).

So can you please tell me if the two versions perform exactly the
same, or there is some difference that I should note about?

They do the same.

I will suggest that you use version 2.

To maximize readability then you should either:
- assign an object in the field declaration
- not modify the object in the constructor
or:
- assign an object in the constructor just before
modifying it

Arne
 

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

Top