473,785 Members | 3,067 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(c onnString);
.....
.....
}

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 1388
Bala,

class Class_Name
{
private string _connectionStri ng; // this is a member variable...
private SqlConnection _con;
public Global(string connString);
{
_con = new SqlConnection(c onnString); // 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(c onnString);
.....
.....
}

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(c onnString);

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 _connectionStri ng; // 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(c onnString);
.....
.....
}

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(c onnString);

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 _connectionStri ng; // 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(c onnString);
.....
.....
}

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
5655
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 data. The class is contains these private variables:
11
1555
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 question into the following code: ===code starts=== #include <iostream> using namespace std;
17
2560
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 with a link to old information that suggests use of "navigator": http://developer.irt.org/script/43.htm
54
4615
by: tshad | last post by:
I have a function: function SalaryDisplay(me) { var salaryMinLabel = document.getElementById("SalaryMin"); salaryMinLabel.value = 200; alert("after setting salaryMinLabel = " + salaryMinLabel.value); } I also have an asp.net object:
15
1342
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 development environment is ASP.Net/VB.Net/.NetFramework 1.1/IE 5+ Which of the above statement I should use? Thanks,
3
3098
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 instance to work with. What is going on here: Sub Main Dim NewObject as MyClass MsgBox("Before Creation")
4
3306
by: shaanxxx | last post by:
how can we restrict object creation on heap ? soln1: overload new any other way to do this ?
3
1669
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 compared to creating an image file (.jpg) shall we say using photoshop?
15
1881
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 <iostream> #include <cstddef> using namespace std; class TraceHeap { int i;
0
10325
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10091
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8972
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3646
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.