Click here to Skip to main content
15,922,584 members
Home / Discussions / C#
   

C#

 
AnswerRe: The "using" statements at the top of a .cs Pin
Martin#28-Aug-07 2:41
Martin#28-Aug-07 2:41 
GeneralRe: The "using" statements at the top of a .cs Pin
Vodstok28-Aug-07 2:46
Vodstok28-Aug-07 2:46 
AnswerRe: The "using" statements at the top of a .cs Pin
Spacix One28-Aug-07 2:42
Spacix One28-Aug-07 2:42 
GeneralRe: The "using" statements at the top of a .cs Pin
Scott Dorman28-Aug-07 4:19
professionalScott Dorman28-Aug-07 4:19 
GeneralRe: The "using" directives at the top of a .cs Pin
PIEBALDconsult28-Aug-07 6:58
mvePIEBALDconsult28-Aug-07 6:58 
GeneralRe: The "using" directives at the top of a .cs Pin
Scott Dorman28-Aug-07 7:00
professionalScott Dorman28-Aug-07 7:00 
AnswerRe: The "using" statements at the top of a .cs Pin
Mark Churchill28-Aug-07 3:11
Mark Churchill28-Aug-07 3:11 
AnswerRe: The "using" directives at the top of a .cs Pin
PIEBALDconsult28-Aug-07 6:36
mvePIEBALDconsult28-Aug-07 6:36 
No, they don't adversely affect the program; they simply save the programmer many many many keystrokes. And my opinion of them is pretty well documented in that other thread so I won't repeat myself yet again other than to say, "I don't use them".


On the other hand, most practitioners don't advance beyond the very simplest use of the using directive.

For instance, one could write

C#
using System.Collections.Generic ;

...

Dictionary<string,object> mystuff   = new Dictionary<string,object>() ;
Dictionary<string,object> yourstuff = new Dictionary<string,object>() ;
Dictionary<string,object> hisstuff  = new Dictionary<string,object>() ;


but one could also write

C#
using LookupTable=System.Collections.Generic.Dictionary<string,object> ;

...

LookupTable mystuff   = new LookupTable() ;
LookupTable yourstuff = new LookupTable() ;
LookupTable hisstuff  = new LookupTable() ;


which has the benefits of saving even more keystrokes, makes the program easier to maintain (the definition of LookupTable only needs to be changed in one place), and may just be better OOP.

(And less time replacing <s with &lt;s when posting on CP!)

Another use for the using directive is this

C#
using DatabaseConnection=System.Data.SqlClient.SqlConnection  ;
using DatabaseCommand   =System.Data.SqlClient.SqlCommand     ;
using DatabaseReader    =System.Data.SqlClient.SqlDataReader  ;
using DatabaseParameter =System.Data.SqlClient.SqlParameter   ;
using DatabaseAdapter   =System.Data.SqlClient.SqlDataAdapter ;
...


then, if the underlying database technology changes, only the using directives need be modified; again, simplifying maintenance.


Taking that to the next level, we can make the selection of the actual database classes a compile-time issue

C#
# if SqlClient
    using DatabaseConnection=System.Data.SqlClient.SqlConnection  ;
    using DatabaseCommand   =System.Data.SqlClient.SqlCommand     ;
    using DatabaseReader    =System.Data.SqlClient.SqlDataReader  ;
    using DatabaseParameter =System.Data.SqlClient.SqlParameter   ;
    using DatabaseAdapter   =System.Data.SqlClient.SqlDataAdapter ;
    ...
# endif

# if OleDb
    ...
# endif

# if MySql
    ...
# endif

...



The problem with this technique is that one must ensure that one and only one of the database systems is defined -- this is an unfortunate side-effect of Microsoft gutting the C preprocessor for use with C#.

Using the standard C preprocessor, one can write

C#
# define SqlClient   1
# define OleDb       2
# define MySql       3
# define UseDatabase 1

# if (UseDatabase==SqlClient)
    using DatabaseConnection=System.Data.SqlClient.SqlConnection  ;
    using DatabaseCommand   =System.Data.SqlClient.SqlCommand     ;
    using DatabaseReader    =System.Data.SqlClient.SqlDataReader  ;
    using DatabaseParameter =System.Data.SqlClient.SqlParameter   ;
    using DatabaseAdapter   =System.Data.SqlClient.SqlDataAdapter ;
    ...
# endif

# if (UseDatabase==OleDb)
   ...
# endif

# if (UseDatabase==MySql)
   ...
# endif

GeneralRe: The "using" directives at the top of a .cs Pin
Scott Dorman28-Aug-07 7:16
professionalScott Dorman28-Aug-07 7:16 
GeneralRe: The &quot;using&quot; directives at the top of a .cs Pin
PIEBALDconsult28-Aug-07 8:31
mvePIEBALDconsult28-Aug-07 8:31 
GeneralRe: The &quot;using&quot; directives at the top of a .cs Pin
Vodstok28-Aug-07 9:01
Vodstok28-Aug-07 9:01 
AnswerRe: The &quot;using&quot; directives at the top of a .cs Pin
PIEBALDconsult28-Aug-07 8:55
mvePIEBALDconsult28-Aug-07 8:55 
QuestionSaving User Settings to a different file Pin
baelzaden28-Aug-07 1:58
baelzaden28-Aug-07 1:58 
AnswerRe: Saving User Settings to a different file Pin
Spacix One28-Aug-07 2:51
Spacix One28-Aug-07 2:51 
AnswerRe: Saving User Settings to a different file Pin
Scott Dorman28-Aug-07 4:22
professionalScott Dorman28-Aug-07 4:22 
QuestionCalling client side control through httpwebrequest Pin
Dipti D Jadhav28-Aug-07 1:47
Dipti D Jadhav28-Aug-07 1:47 
QuestionSender vs From Pin
logicaldna28-Aug-07 1:33
logicaldna28-Aug-07 1:33 
AnswerRe: Sender vs From Pin
Scott Dorman28-Aug-07 4:38
professionalScott Dorman28-Aug-07 4:38 
GeneralRe: Sender vs From Pin
logicaldna28-Aug-07 4:46
logicaldna28-Aug-07 4:46 
GeneralRe: Sender vs From Pin
Scott Dorman28-Aug-07 5:15
professionalScott Dorman28-Aug-07 5:15 
GeneralRe: Sender vs From Pin
logicaldna28-Aug-07 4:56
logicaldna28-Aug-07 4:56 
GeneralRe: Sender vs From Pin
Scott Dorman28-Aug-07 5:23
professionalScott Dorman28-Aug-07 5:23 
GeneralRe: Sender vs From Pin
logicaldna28-Aug-07 5:31
logicaldna28-Aug-07 5:31 
QuestionRead a 3 Column CSV data in to a Datatable [modified] Pin
kibromg28-Aug-07 1:27
kibromg28-Aug-07 1:27 
AnswerRe: Read a 3 Column CSV data in to a Datatable Pin
Spacix One28-Aug-07 3:01
Spacix One28-Aug-07 3:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.