Wednesday, December 31, 2008

C# is so much easier then C++

I have been given the opportunity to work on an old c++ program for work this last couple works.  I just have to say that C# is so much easier to follow and understand then MFC any day of the week.  I did learn how to check if a directory exists before creating it though.

 1569 if(GetFileAttributes(_T(tPath)) == INVALID_FILE_ATTRIBUTES)
 1570     {
 1571         SECURITY_ATTRIBUTES sa;
 1572         sa.nLength = sizeof(SECURITY_ATTRIBUTES);        
 1573         sa.lpSecurityDescriptor = NULL;
 1574         sa.bInheritHandle = FALSE;
 1575         CreateDirectory(_T(tPath),&sa);
 1576     }

Just thought I put this here for when I need it again.

Wednesday, December 10, 2008


Let’s talk about string delimiters and why you shouldn’t hard code them. Take the following example.

   16 return String.Format("#Command#{0}#{1}#{2}#{3}#{4}#{5}#{6}#{7}#{8}#{9}", completePath,
   17                                                                       GUID,
   18                                                                       UID,
   19                                                                       otherinfo,
   20                                                                       option1,
   21                                                                       displayText,
   22                                                                       filter,
   23                                                                       someint.ToString(),
   24                                                                       file,
   25                                                                       seckret);

It and three other ones just like it can be found in the code I am currently working on. Now when you see this you think that isn’t all that bad. Until you need to change the delimiter for one reason or another. You could go with something like the following but it not the most readable thing you will ever see.

   28 return String.Format("{10}Command{10}{0}{10}{1}{10}{2}{10}{3}{10}{4}{10}{5}{10}{6}{10}{7}{10}{8}{10}{9}",
   29                                                                       completePath,
   30                                                                       GUID,
   31                                                                       UID,
   32                                                                       otherinfo,
   33                                                                       option1,
   34                                                                       displayText,
   35                                                                       filter,
   36                                                                       someint.ToString(),
   37                                                                       file,
   38                                                                       seckret,
   39                                                                       commandStringDelimiter);

This is a little better now you can just change “commandStringDelimiter” to a different value when you need to change the delimiter. But it’s not verry readabel a better soluction is to do build a function that can build the string for you. This allows you to hide how it is created and keep it all in one place. Like this.

   54 public string BuildFileCommandString(params string[] options)
   55         {
   56             List<string> cStr = new List<string>();
   57             cStr.Add("Command");
   58             cStr.AddRange(options);
   59             return this.commandStringDelimiter + string.Join(this.commandStringDelimiter, cStr.ToArray());
   60         }      

You would call it like this.

   41 BuildFileCommandString(completePath,
   42                        GUID,
   43                        UID,
   44                        otherinfo,
   45                        option1,
   46                        displayText,
   47                        filter,
   48                        someint.ToString(),
   49                        file,
   50                        Seckret);

You will also notice that I used a string.Join rather then a string.Format this just makes it more readable to me.