What's in Microsoft.VisualBasic for C# Developers: Part 2 - Constants & Control Characters
[This blog is part of a larger series, to find more parts in the series, please see the Series Index].
We will start off the series with something very simple: two classes that give us access to some constant definitions that may be useful.
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 that 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 the underlying OS—so on systems that use just \n, it will be that, whereas the VB one is always the same regardless of what the OS uses.
Constants
Microsoft.VisualBasic.Constants contains a lot of constant definitions used throughout the whole Microsoft.VisualBasic assembly. A lot are meant to be used with specific functions—for example, vbAbort is meant to be used with the MsgBox function—but there are a few that 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 the bad habit of thinking they can build something better than others did in the past. There’s a famous example of someone who decided a boolean (something that is either true or false) needed a third option—FileNotFound.
It appears the VB team also decided this was a good idea, so we have the Tristate enum, which is at least a little more logical than the above example.
public enum TriState
{
False = 0,
True = -1,
UseDefault = -2
}
The values here match those in the Convert.ToBoolean(int value) method, where 0 is always false and anything else is true.
With the .NET Framework 4, I suspect this isn’t that useful anymore, as you can set a default value for method parameters (see example below). But if you’re on older versions, this may still be useful.
private void Demo(bool value = true)