Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
VFP's With...EndWith equivalent in C# ?
Message
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
C# 2.0
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01216132
Message ID:
01216867
Vues:
15
Hi, Hugo.

>>Instead, C# has powerful and meaningful constructions like "using", which provides an execution context for a disposable resource, simplifying the error handling code VERY much.
>
>You lost me. Evidently as it can be read in my previous message to Claudio, I am not sure if I agree; of course it is for my limited knowledge, and I am not even sure I understand what you are saying and how you simplify error handling with the using directive. If it is not too much trouble can you give an example to the simple folks?

Sure. Here's one simple example (not checked, so some syntax error could have slipped in):
public void ExecuteCommand( string connectionString, string commandString ) {
   using ( SqlConnection aConnection = new SqlConnection(connectionString) ) {
      using ( SqlCommand aCommand = new SqlCommand( aCommand, aConnection )){
         aConnection.Open();
         aCommand.ExecuteNonQuery();
      }
   }
}
There you have two nested using taking care of two different disposable resources and you don't have to use try/catches to manage potential exceptions in the middle, because they'll be disposed in any case. You can write the same with try/catches, but it will get quite messy.

Hope this clears my point.

Regards,
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform