Which is advisable for object creation?

B

Balaji Kannan

Hi,

In dot net during component development i have used some
member variables in the class file. Inside the class i
have used the member declaration and the instant handling
in the following way.

In the constructor i have created the connection instant
in the following way

public Global(string connString)
{
//member variable
private Sqlconnection m_connDB;
.......
.......
m_connDB = new SqlConnection(connString);
.....
.....
}

In otherway we can do the member declaration and the
object creation in the same place. That is

public Global(string connString)
{
//member variable
private Sqlconnection m_connDB = new SqlConnection
(connString);
.......
......
.....
.....
}

Among the above which one is best as for the performance
is concerned. Which one is more advisable? Can anyone
guide me..

Advance thanks and regards
 
H

Hermit Dave

Bala,

class Class_Name
{
private string _connectionString; // this is a member variable...
private SqlConnection _con;
public Global(string connString);
{
_con = new SqlConnection(connString); // member variable
created on the heap and would be available for use outside the scope of this
method.
SqlDataAdapter _da = new SqlDataAdapter(); // this is a local
variable... not available and gc'd after the method call
}
}

as for whether you should define it connection string as a member variable
to the class... well if you dont need to use connection string anywhere
else... then why make an extra call to assign it to a local variable or a
data memeber.. .rather read it from the passed value and instantiate your
object...
but if you feel the need to access the connection string assigned from say
lot many method calls... assign it to a data member so that you dont have to
write calls to repeatedly pass it... instead since the passed value is
stored in data member it can be accessed....

hope this helps,

hd
 
B

Balaji kannan

hi,
My actual question is, which of the following coding is
best for performance gain.

1. private Sqlconnection m_connDB;
m_connDB = new SqlConnection(connString);

2. private Sqlconnection m_connDB = new SqlConnection
(connString);

in the first case, i have created reference variable then
assigning object to that variable.

In the second case, while creating variable itself
assigning values.

Which one is best?? please let me know.

Thanks,
Bala
-----Original Message-----
Bala,

class Class_Name
{
private string _connectionString; // this is a member variable...
private SqlConnection _con;
public Global(string connString);
{
_con = new SqlConnection
(connString); // member variable
 
H

Hermit Dave

since you define the members as being private explicitly i would presume
that they are data members belonging to a class.
for datamembers it is better to declare a reference and then use constructor
to create an instance for the ref.

Performance., not a big deal. Cause the compilers their own optimisations
your code so what you write may not be output.
Think the second one internally maps to the first one.... but i would say
write two and compare the msil.

Hope this helps

HD
 

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

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top