.netvb.netdebuggingdebuggerdisplay

Is it possible to use conditions in a DebuggerDisplay in VB.NET?


I ask the question asked here again because the answer is not suitable for VB.NET:

Consider the following class:

[DebuggerDisplay("{GetType().Name,nq}: FileName = {FileName,nq}")]
public class FileWrapper 
{
     public string FileName { get; set; }
     public bool IsTempFile { get; set; }
     public string TempFileName { get; set; } 
} 

I would like to add a debugger display based on the IsTempFileName property. I would like to add the string , TempFileName = {TempFileName,nq} when the instance is a temp file. How would I achieve something this?

How do I do this in VB.NET?


Solution

  • VB has it's own equivalent to the C# ?: operator these days, i.e. If. It can be used in the equivalent scenario:

    <DebuggerDisplay("{GetType(FileWrapper).Name,nq}: FileName = {FileName,nq}{If(IsTempFile, "", TempFileName: "" & TempFileName, System.String.Empty),nq}")>
    Public Class FileWrapper
    
        Public Property FileName As String
        Public Property IsTempFile As Boolean
        Public Property TempFileName As String
    
    End Class
    

    It seems that GetType is interpreted there as the VB operator rather than the Object.GetType method, so you need to add the type in there as an argument too.

    It's also worth checking out the second answer in that original thread. I'm accepting at face value the statement it includes about the compiler for the calling code being the one to evaluate the expression provided. That means that a C#- or VB-specific expression in that context will fail if the type is consumed by code written in the other language.