473,403 Members | 2,071 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,403 software developers and data experts.

Which is advisable for object creation?

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
Nov 18 '05 #1
3 1369
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

"Balaji Kannan" <ba******@msdc.hcltech.com> wrote in message
news:02****************************@phx.gbl...
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

Nov 18 '05 #2
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 variablecreated on the heap and would be available for use outside the scope of thismethod.
SqlDataAdapter _da = new SqlDataAdapter(); // this is a localvariable... not available and gc'd after the method call
}
}

as for whether you should define it connection string as a member variableto the class... well if you dont need to use connection string anywhereelse... then why make an extra call to assign it to a local variable or adata memeber.. .rather read it from the passed value and instantiate yourobject...
but if you feel the need to access the connection string assigned from saylot many method calls... assign it to a data member so that you dont have towrite calls to repeatedly pass it... instead since the passed value isstored in data member it can be accessed....

hope this helps,

hd

"Balaji Kannan" <ba******@msdc.hcltech.com> wrote in messagenews:02****************************@phx.gbl...
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

.

Nov 18 '05 #3
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

"Balaji kannan" <ba******@msdc.hcltech.com> wrote in message
news:03****************************@phx.gbl...
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
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

"Balaji Kannan" <ba******@msdc.hcltech.com> wrote in

message
news:02****************************@phx.gbl...
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

.

Nov 18 '05 #4

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

Similar topics

7
by: Richard | last post by:
Hi all, I am looking for some help on understanding the overhead associated with object creation in Java. I am writing an application where I have written a class to encapsulate some text...
11
by: cfchou | last post by:
hi, all, i'm reading ch.20 -smart pointers- of . and i'm tring the trule.hpp test. but there's something different than i expect, and i found that's about temp object. so i simplified the...
17
by: lawrence | last post by:
How is it possible that the question "How do I detect which browser the user has" is missing from this FAQ: http://www.faqts.com/knowledge_base/index.phtml/fid/125 and is only here on this...
54
by: tshad | last post by:
I have a function: function SalaryDisplay(me) { var salaryMinLabel = document.getElementById("SalaryMin"); salaryMinLabel.value = 200; alert("after setting salaryMinLabel = " +...
15
by: Jonathan | last post by:
Few days back I posted this question. Can anyone help me on this question. var myObj = new ActiveXObject("Msxml2.XMLHTTP"); var myObj = new ActiveXObject("Microsoft.XMLHTTP"); If my...
3
by: Nick Dreyer | last post by:
I was quite surprised to notice that Sub New() gets called twice, once at declaration time and once at creation time. I can't figure out why it would be called at declaration if there is no class...
4
by: shaanxxx | last post by:
how can we restrict object creation on heap ? soln1: overload new any other way to do this ?
3
by: shotokan99 | last post by:
hi, shall we say you want to creae a banner, a button, and other images for your site. some create images on the fly through php using gd library. is this more advisable or more efficient...
15
by: asm23 | last post by:
Hi, everyone, I'm studying the <<Thinking in C++>volume Two. In Chapter One, the example code : Auto_ptr.cpp //------------------------------------------------------- #include <memory> #include...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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.