473,388 Members | 1,423 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,388 software developers and data experts.

Initializing an Object

Hi friends,

I have a method which is given below

public IDbConnection SetConnection(string connectionString)

{

try

{

IDbConnection connection;

switch (dbType)

{

case (int)DatabaseType.odbc:

connection = new OdbcConnection(connectionString);

break;

case (int)DatabaseType.oledb:

connection = new OleDbConnection(connectionString);

break;

case (int)DatabaseType.sqlClient:

connection = new SqlConnection(connectionString);

break;

default:

break;

}

return connection;

}

catch (Exception ex)

{

throw ex;

}

}

When this is compiled, a warning comes like "the object Connection is
unassigned or never used. So how can I initilize this object?

Thanks in Advance
Emmanuel Mathew
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.710 / Virus Database: 466 - Release Date: 23/06/2004
Nov 22 '05 #1
1 1225
Emmanuel,
Do you really need the "connection" variable?

You can initialize it here:
IDbConnection connection = null;

I however would probably simply return it, and avoid the variable.
case DatabaseType.odbc:

return new OdbcConnection(connectionString);

break;

Rather then silently fail, I would throw an exception if I did not have a
valid DatabaseType.
default:
throw new ArgumentOutOfRangeException("dbType", dbType, "Invalid
Database type")
break;

It appears that your Try/Catch is not doing anything, I would not bother
coding it.
catch (Exception ex)
{
throw ex;
}
http://msdn.microsoft.com/msdnmag/is...T/default.aspx

If you did need the Try/Catch, I would consider using "throw" over "throw
ex", as "throw" will preserve the stack trace.
catch (Exception ex)
{
throw;
}
Hope this helps
Jay

"Emmanuel Mathew" <em******@aitstech.com> wrote in message
news:eV*************@tk2msftngp13.phx.gbl... Hi friends,

I have a method which is given below

public IDbConnection SetConnection(string connectionString)

{

try

{

IDbConnection connection;

switch (dbType)

{

case (int)DatabaseType.odbc:

connection = new OdbcConnection(connectionString);

break;

case (int)DatabaseType.oledb:

connection = new OleDbConnection(connectionString);

break;

case (int)DatabaseType.sqlClient:

connection = new SqlConnection(connectionString);

break;

default:

break;

}

return connection;

}

catch (Exception ex)

{

throw ex;

}

}

When this is compiled, a warning comes like "the object Connection is
unassigned or never used. So how can I initilize this object?

Thanks in Advance
Emmanuel Mathew
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.710 / Virus Database: 466 - Release Date: 23/06/2004

Nov 22 '05 #2

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

Similar topics

50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
2
by: Dave | last post by:
Hello all, I have a class that contains a large number of discrete pieces of state information. Any combination of these member variables might be valid for a given object. Any given member...
1
by: Andreas Boehm | last post by:
Hi *.*, does the standard meanwhile define something about initializing variables by the compiler? I think, it is a side-effect of the OS used, if undefined global (static) variables are...
3
by: farseer | last post by:
If i have an array of a certain type, is there away of initializing with without knowing it's type? for example (forgive me if some of this is syntactically incorrect) if i have a procedure...
11
by: sg71.cherub | last post by:
Hi All, I have encapsulate CvMat of OpenCV into my own matrix class as the following: class CVMatrix { //== Fields private: unsigned m_Width;
1
by: subramanian | last post by:
Consider the following program: #include <iostream> #include <string> class Test { public: Test(const std::string &val); private:
12
by: Mik0b0 | last post by:
Hallo. Let's say, there is a structure struct struct10{ int a1; int a2; int a3; int a4; }count={ {10,20,30,40}, {50,60,70,80}
3
by: Reckoner | last post by:
would it be possible to use one of an object's methods without initializing the object? In other words, if I have: class Test: def __init__(self): print 'init' def foo(self): print 'foo'
13
by: WaterWalk | last post by:
Hello. When I consult the ISO C++ standard, I notice that in paragraph 3.6.2.1, the standard states: "Objects with static storage duration shall be zero-initialized before any other...
6
by: Jai Prabhu | last post by:
Hi All, Consider the following piece of code: void func (void) { static unsigned char arr = "\x00\xAA\xBB"; fprintf (stderr, "0x%x\n", arr); fprintf (stderr, "0x%x\n", arr);
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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,...
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
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...

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.