Thursday, February 07, 2008

How to Impress with Your VB.NET Skills

It is no secret that people who write in VB.NET are often looked down upon. Sometimes that can be without merit but sometimes there is a good reason as folks do not bother to learn and improve. That being said, here are some quick rules to impress people while still programming with VB.NET.

  1. Know the .NET Framework
    Seems like a no-brainer but it is mind boggling how many people switch from VB6 to VB.NET and never bother to learn the new functionality available in .NET. So here are some quick tips:
    1. Use System.IO.File.ReadAllText() for reading a complete text file instead of reading it line by line.
    2. If you need to process a file line by line, use #1 but split the text file with String.Split() with Environment.NewLine as the delimiter and then loop over the array.
    3. When you need to find something inside a string, indexOf() is not the only choice. Regular expressions are much better and more powerful.
    4. Related to #3, avoid using VB specific functions like Left(), Mid(), or Right() - use String.StartsWith(), String.EndsWith(), String.Substring() or regular expressions instead.
    5. Expanding on #4, avoid VB constructs left over from VB6. A prime example is using "vbCrLf" for inserting new lines instead of Environment.NewLine.
  2. Strive to program better
    Some of the rules below apply not to just VB.NET but to programming in general. Some of course apply only to VB.NET as for example you cannot get by compiling your program in other languages unless your variables are strongly typed (strongly typed simply means that each variable used has an assigned type at compile time such as integer, float or string).
    1. Strongly type your variables. "dim myVariable" might work for you but how do I know what data type it is. "dim myVariable as String" is much more clear.
    2. Initialize your variables. While for string this might not be needed, for complex object (such as DataTables), it is almost a must. You should simply do
      dim myData as New DataTable

      dim myStringData as String = String.Empty
    3. Settle on a naming convention that works for you. I name my private member variables starting with "m_" while my public properties have the same name without the "m_". Methods and functions are named in with camel case as in "doSomething()". Whatever you decide is great as long as you are consitant and you stick to it.
    4. Do not explicetly check a boolean value for "False". Instead of
      If Directory.Exists(tempPath) = false Then
      If Not Directory.Exists(tempPath) Then
    5. Use Path.Combine() to combine file system paths intead of concatinating strings with "&" or "+"
    6. Use String.Format() when outputting strings instead of simple concatenation:
      String.Format("{0}, {1}, {2}", row.Item("videoID"), row.Item("videoTitle"), row.Item("videoDate"))
    7. Do not assume the data type. A data table row may contain a string value but still use Convert.ToString() to get the value as
    8. Related to #3, enable "Option Explicit" and "Option Strict" in your VB.NET projects. This will catch many potential errors at compile time and not let you build your program unless you correct the issues.
  3. Do not reinvent the wheel
    This point goes hand in hand with knowing the .NET framework but I feel it is worth mentioning on it's own. .NET has so many methods and functions you can use to write your application. Furthermore, there are plenty of open source projects written for all kinds of things:
    1. Log4Net for file, event, or trace logging.
    2. NAnt for task automation through XML build files and inline scripting
    3. SubSonic, NHibernate or dozen other projects for database access
    4. Check out .NET Zone, CodePlex, CodeProject or even Google before writing your own implementation. If you need something that can be useful in more than one occasion, it is very likely that somebody else has already written it. You can easily convert code from C# to VB.NET with a dozen of free tools such as the one found at

No comments:

Post a Comment

// //]]>