1

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?

codeDom
  • 1,623
  • 18
  • 54

1 Answers1

3

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.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • 1
    To get around ```GetType()``` being reserved in VB, I tend to use `````` so that it continues working when I change the class name. – Fabulous Oct 09 '17 at 12:56
  • @Fabulous, I tried that and it didn't work. Maybe I did something wrong so I'll give it another go but what I posted above did work for me. – jmcilhinney Oct 09 '17 at 13:06
  • Hmmm... just tried `Me.GetType` and it worked. Not sure what I did differently the first time but at least now we have two options. – jmcilhinney Oct 09 '17 at 13:21
  • I think you might have done ```GetType``` without the Me. I don't know how that is resolved when the attribute is processed, but my guess is that if resolves the the operator GetType, rather than the method. – Fabulous Oct 09 '17 at 14:46