In this post I want describe the manners in which Visual Studio.Net shows information of object instances in the several variable debugger windows; the Autos, Local and Watch windows. To support the post I have placed images of the source code and several variable debugger windows at the end of the article.

Normally Visual Studio.Net shows variables in the debugger windows as a node representing the type with only the type name in the Value column (see below). This can be useful but in general you want more information about the value of the attributes of the class. Especially if the node is folded. Fortunately, the. NET framework gives a developer an opportunity to manipulate the information shown contained in the Value column. This can be done by overriding the ToString() method of a class or by annotating the class with the DebuggerDisplayAttribute.

Visual Studio.Net uses the ToString() method to fill the Value column. Hence, the type is shown. By overriding the ToString() method a developer can change the value in the Value column. Look for an example at the images below.

A developer can use the DebuggerDisplayAttribute when overriding the ToString() method is not suitable. As you can see in the code the DebuggerDisplayAttribute accepts a string which indicates how and what data should be shown. In the string can contain the {} braces. The text between the braces is evaluated to a field, property or method. Locals, pointers and aliases can not stand between the braces.

The DebuggerDisplayAttribute can be used on:

  • Classes,

  • Structures,

  • Delegates,

  • Enumerations,

  • Fields,

  • Properties,

  • Assemblies.

The Type property can be left blank if no type information is required to be shown. The Name property can contain the string that indicates information that should be shown: the string with the {} braces. The Target is used to register the type on which the attribute applies when it is defined at assembly level.

kick it on DotNetKicks.com

 

The code:
 
 
 The standard view
 
The view with ToString() overridden
 
The view with the DebuggerDisplayAttribute

With code snippets software development with Visual Studio.Net 2005 and 2008 can be alot easier. Creating snippet can be done with any text editor. It is just xml. But using a tool is much easier. Snippy is a such a tool.

   Snippy screenshot

  kick it on DotNetKicks.com

There are some nice things to know about Visual Studio.Net 2008 that can make life easier as a programmer. In the series ‘Debugger tips VS.Net’ I am gonna talk about the debug techniques VS.Net 2008 offers. Tracepoints is the subject of this post. A tracepoint is a breakpoint with a custom action associated with it. When the tracepoint is hit the custom action is performed. An example of such a action is writing a traceline to the output window.

How can you add a tracepoint? This is easy: go to the line of code where you want the tracepoint and press F9. This will add a breakpoint. The picture below shows a breakpoint.

 

Then right click the breakpoint and choose When hit from the menu that appears.

 

The When Breakpoint Is Hit dialog appears. In this dialog you can choose to print a message. This will add tracelines to the ouput window every time the tracepoint is hit. The message can be formatted and You can include programmatic information using DebuggerDisplayAttribute syntax. You can choose to run a macro when the tracepoint hits too. In the screenshot below the DumpStacks macro from the samples is picked. Select Continue Execution if you don’t want the tracepoint to break execution of your program.

 

 A tracepoint has its own icon. It is a red diamond in the margin of the code edit window.

 That was it.

kick it on DotNetKicks.com

 Software development is an art.

The ?? operator C# operator returns the left side if it is not null. It returns the right side if the left side is null. It is very handy in situations where you can expect a value to be null but you want to use a default value if it is. Here is an example:

        string valueCouldBeNull = GetTheValue() ?? "The default value";
        Console.WriteLine("The result is " + valueCouldBeNull);
 

When assigning nullable types to non-nullable types you can avoid compile-time exceptions using the ?? operator.

kick it on DotNetKicks.com

Yesterday I was asked a question. A collegue asked how to convert an UTF-16 xml file to an UTF-8 one. He was using xslt for the conversion. I took a look at his code and everything looked fine. He was using a StringBuilder, StringWriter and a XmlWriter. He had set the encoding settings for the XmlWriter to UTF-8. Should work. Shouldn’t it? No, it does not work because a .Net string has a UTF-16 encoding and it cannot be changed, of course. So even though my collegue was setting the encoding it would not be used anyway.

Another approach was needed and I found one. The XmlWriter accepts a stream also. A MemoryStream can be used. It is just a buffer of bytes and won’t do anything with the content. Here is some example code. The first example uses a StringBuilder and doesn’t work. The second example uses the MemoryStream.

Example 1 (does not work):

StringBuilder sb = new StringBuilder();
using (XmlWriter xw = XmlWriter.Create(new StringWriter(sb)))
{
    XslCompiledTransform xct = new XslCompiledTransform();
    xct.Load(@"cdcatalog.xsl");
    xct.Transform(@"cdcatalog.xml", xw);
    xw.Flush();
}
Console.WriteLine(sb.ToString());

 

Example 2 (works):

MemoryStream ms = new MemoryStream();
using (XmlWriter xw = XmlWriter.Create(new StreamWriter(ms)))
{
    XslCompiledTransform xct = new XslCompiledTransform();
    xct.Load(@"cdcatalog.xsl");
    xct.Transform(@"cdcatalog.xml", xw);
    xw.Flush();
}
string xmlOutput = Encoding.UTF8.GetString(ms.GetBuffer());
Console.WriteLine(xmlOutput);

 

You can download an example project here.

  kick it on DotNetKicks.com

I like VB.Net. Do not tell anyone. Wink I programmed in it for many years and it is a powerful language. Just as powerful as C#. It just does not look like C. It looks like BASIC. Ok, yes, uuuuuhhmm, anyway there are some issues with the performance when using VB.Net in VS.Net 2008. Embarassed The VB team solved those issues. Read about it here.

Yesterday I installed the first service pack for Vista. But at first I had a problem installing it. Right after starting the installer it  showed a message that I did not have the right language version of Vista installed. Weird, I thought, this is the english version. The message mentioned something about incompatible language packages also, but I did not pay attention to that. Disappointed I searched the internet with Google to find out if there were other people with the same problem. There were none but one. (S)He mentioned language packages also. Hmmm, wait a minute, I thought, I installed the dutch language package. Is this the problem? Yes, it was. After uninstalling the language package the installation went fine.

How do you uninstalling a language package? In the control panel search for ‘Regional setting and language options’. Also see the figure below. Double click it and go to the ‘keyboards and Languages’ tab. In the middle of the tab you see the ‘Install/uninstall languages…’ button . After clicking it a wizard lets you select language packages to uninstall. 

This afternoon I was experimenting a bit with Filedisk. Filedisk is a tool to mount files on the filesystem as a drive. I succeeded to mount a file, but the file I created was 0 bytes long. When I tried to format the mounted file I got an error message saying the file was te small to create the filesystem. It took me a moment before realizing that the error message was correct. The file I mounted was 0 bytes long after all. I needed to create a file with some body but I could not find a application that could do that. I made my own program to do that. It is called CreateFile. You can download it here (src). You use it like this:

CreateFile filename [/Size:xxxx]

The size is entered in bytes. You can leave out the size argument. The default size is used when you do. The default size is 1024 bytes.

 P.S.: There is also a GUI version of FileDisk. It is called Filedisk Iso Mounter and you can find it here

If you like the application, maybe also like to donate.