can we make a long statement become short by pre-declare it ?

T

Tee

Hi,

my question is as the topic, can we make a long statement become short by
pre-declare it.
sorry if you can't understand what I asked, as I am not too sure how to ask
it, but below is the example of what I want.

I want just type "Connection", and it will same as "Oledb.OledbConnection",
so I just need to type Connection.Open instead of
Oledb.OledbConnection.Open.

it's not hard to type Oledb.OledbConnection as it is not long, but this is
just an example of what I want.

so can anyone tell me how to do it ?


Thanks.
 
M

mikeb

Tee said:
Hi,

my question is as the topic, can we make a long statement become short by
pre-declare it.
sorry if you can't understand what I asked, as I am not too sure how to ask
it, but below is the example of what I want.

I want just type "Connection", and it will same as "Oledb.OledbConnection",
so I just need to type Connection.Open instead of
Oledb.OledbConnection.Open.

Since OleDbConnection is a class and the Open method is not static, your
example above would not work. However, in the interest of doing what
you mean instead of what you say, you could do the following:

using System.Data.OleDb;

// ...

OleDbConnection conn = new OleDbConnection( "my connection string");

conn.Open();


Or, to alias the classname itself (which is what I think you were
getting at):

using OleConn = System.Data.OleDb.OleDbConnection;

// ...

OleConn conn = new OleConn( "my connection string");

conn.Open();

Personally, I'd avoid aliasing the classname unless it's required to
avoid a conflict. If you use OleDbConnection, everyone familiar with
the ADO.NET namespace immediately knows they're dealing with that class.
if you alias the classname, then someone reading your code needs to
know or learn that they're really dealing with OleDbConnection and not
some custom class.

Not a big deal, but then using the original classname is generally not
really a burden in the first place.
 

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,731
Messages
2,569,432
Members
44,835
Latest member
KetoRushACVBuy

Latest Threads

Top