Skip to main content

[This blog is part of a larger series, to find more parts in the series please see the Series Index]

monolithWe will start off the series with something very simple, two classes which give us access to some constant definitions that may be useful to use.

Control Characters

Microsoft.VisualBasic.ControlChars contains 10 constant fields for commonly used items:

public const char Back = '\b';
public const char Cr = '\r';
public const string CrLf = "\r\n";
public const char FormFeed = '\f';
public const char Lf = '\n';
public const string NewLine = "\r\n";
public const char NullChar = '\0';
public const char Quote = '"';
public const char Tab = '\t';
public const char VerticalTab = '\v';

As you can see this is just a useful list to have and can make code much easier to read.

As a C# developer there is one on that list I wouldn’t use – NewLine. In the mscorlib assembly, there is an Environment class which contains a property called NewLine which, on my machine is the exact same as the one above. So why would I use that over the VB one? This is because Environment.NewLine changes based on underlying OS – so on systems which use just \n it will be that where the VB one is always the same regardless of what the OS uses.

Constants

Microsoft.VisualBasic.Constants contains a lot of constant definitions which are used through out the whole Microsoft.VisualBasic assembly. A lot are to be used with specific functions, for example vbAbort is meant to be used with the MsgBox function, but there are a few which are interesting:

Control Characters

The first interesting group is that almost all the Control Characters from Microsoft.VisualBasic.ControlChars are repeated here – the only one missing is Quote. So now you have two ways (or three ways for Newline) to get control characters.

public const string vbBack = "\b";
public const string vbCr = "\r";
public const string vbCrLf = "\r\n";
public const string vbFormFeed = "\f";
public const string vbLf = "\n";
public const string vbNewLine = "\r\n";
public const string vbNullChar = "\0";
public const string vbTab = "\t";
public const string vbVerticalTab = "\v"

Tristate

Developers often have an bad habit of thinking that they can build it better than other people have done so in the past. There is a famous example of someone who decided that a boolean (something that is either true or false), needed a third option – FileNotFound.

It appears the VB guys also decided this would be a good route to go too, so we have the Tristate enum, which is atleast a little more logical than the above example.

public enum TriState
{
    False = 0,
    True = -1,
    UseDefault = -2
}

The values here match to the Convert.ToBoolean(int value) method where 0 is always false and anything else is true.

With the .NET Framework 4, I suspect this is not that useful anymore as you can set the default value on the parameters of a method (see example below), but if you are on older versions then this may be useful.

private void Demo(bool value = true)