Geeks With Blogs
Blog Moved to http://podwysocki.codebetter.com/ Blog Moved to http://podwysocki.codebetter.com/
Kenny Kerr has recently had an article published by the MSDN for best practices for C++/CLI.  The link can be found here:  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/CplusCLIBP.asp
 
Overall it is a very thorough article on what you should and should not do when using C++/CLI.  I found one interesting piece in here about the best practices dealing with the auto_handle versus using the delete keyword on a managed object.  The auto_handle from what the link describes, is an easy way to let C++ clean up the objects for you using the template class by calling the delete behind the scenes.  Since I do both C++ and C#, I find the differences a little interesting. 
 
Here are some examples of that:
 
Using the delete keyword:
 
StringCollection MyNamespace::MyClass::GetZipCodes()
{
     StringCollection^ zipCodes = gcnew StringCollection();
     SqlConnection^ connection = gcnew SqlConnection(myConnString);
 
     try
     {
          connection->Open();
         
          SqlCommand^ command = gcnew SqlCommand("usp_GetZipCodes", connection);
 
         try
         {
              command->CommandType = CommandType::StoredProcedure;
              SqlDataReader reader = command->ExecuteReader();
 
              try
              {
                   while(reader->Read()
                   {
                        zipCodes->Add(reader->GetString(0));
                   }
              }
             finally
             {
                  delete reader;
             }
         }
        finally
        {
              delete command;
        }
     }
     finally
     {
          delete connection ;         
     }
 
     return zipCodes
}
 
Using the auto_handle:
 
#include "auto_handle.h"
using namespace msclr;
 
StringCollection MyNamespace::MyClass::GetZipCodes()
{
     StringCollection^ zipCodes = gcnew StringCollection();
 
     auto_handle<SqlConnection> connection = gcnew SqlConnection(myConnString);
     connection->Open();
 
     auto_handle<SqlCommand> command = gcnew SqlCommand("usp_GetZipCodes", connection);
     command->CommandType = CommandType::StoredProcedure;
 
     auto_handle<SqlDataReader> reader = command->ExecuteReader();
     while(reader->Read())
     {
          zipCodes->Add(reader->GetString(0));
     }
 
     return zipCodes;
}
 
Using the C# standard using keyword:
 
StringCollection GetZipCodes()
{
     StringCollection zipCodes = new StringCollection();
 
     using(SqlConnection connection = new SqlConnection(myConnString))
     {
          connection.Open();
 
          using(SqlCommand command = new SqlCommand("usp_GetZipCodes", connection))
          {
               command.CommandType = CommandType.StoredProcedure;
 
               using(SqlDataReader reader = command.ExecuteReader())
               {
                    while(reader.Read())
                    {
                         zipCodes.Add(reader.GetString(0));
                    }
               }
          }
     }
}
 
It's interesting about the little differences between the implementations of .NET with regards to C# versus C++ when it comes to patterns and method implementations.
 
Anyhow, back on topic...  These guidelines are definitely a must to keep in mind when writing C++/CLI code, especially when it comes to mixed-mode code.
 
Posted on Wednesday, May 31, 2006 2:32 PM .NET | Back to top


Comments on this post: C++/CLI Best Practices

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Matthew Podwysocki | Powered by: GeeksWithBlogs.net