0

I have a class with the following properties

Public Class PetClass

    Public Enum Animal
         Bird = 1
         Dog = 2
         Cat = 3
    End Enum

    Public MyPetType As Animal = Animal.Cat

    Public MyPetName As String = "BigMeow"

End Class

When I declare it like this...

Dim n As New PetClass

... and then hover the mouse over it, I see the following:

enter image description here

I love how it displays the properties, especially the enum to string with the numeric representation in { }.

I would like to be able to print all this info in the debug console.

I don't think that this is possible, so I think I need to re-create this view so that I can then print my custom approach to the debug console.

I have tried to get the properties with the following code found here:

Public Sub DisplayAll(ByVal Someobject)
    Dim _type As Type = Someobject.GetType()
    Dim properties() As PropertyInfo = _type.GetProperties()  'line 3
    For Each _property As PropertyInfo In properties
        Console.WriteLine("Name: " + _property.Name + ", Value: " + _property.GetValue(Someobject, Nothing))
    Next
End Sub

However, no PropertyInfo is found for my class.

Why?

If that is because of missing Getters / Setters:

Using explicit Getters / Setters is the very last option that I would like to try. There are over 100 properties in my real class, and I have chosen the current design for a reason.

Thank you!

Edit: I have added a new property using Getting / Setters like this:

Public Property SomeTest() As String
    Get
        Return "foo"
    End Get
    Set(value As String)

    End Set
End Property

This property is listed in "DisplayAll", but as I have explained, I don't want to use getters / setters, and Visual Studio can seemingly also work without it, as it can display the properties just fine:

enter image description here

tmighty
  • 10,734
  • 21
  • 104
  • 218
  • 2
    Think you're after Enum.GetName. https://learn.microsoft.com/en-us/dotnet/api/system.enum.getname?view=netcore-3.1 – Hursey May 26 '20 at 21:52
  • 1
    @JQSOFT Thank you, I have edited the title. – tmighty May 26 '20 at 22:34
  • OK. First make `MyPetType` and `MyPetName` public properties with getters and setters. Second, you can override the `ToString` function to return the value of the `MyPetType` property the way you like. –  May 26 '20 at 22:43
  • @JQSOFT I have edited my question even more and asked for a general approach in order to deal with a class with many more properties. – tmighty May 26 '20 at 22:45
  • Ahh I got you now. Check [this](https://learn.microsoft.com/en-us/dotnet/framework/debug-trace-profile/enhancing-debugging-with-the-debugger-display-attributes) out. –  May 26 '20 at 22:48
  • @JQSOFT Thank you. Just to clarify: I would like to have the same as the debug view, just as a string so that I can print it out. I will edit my question accordingly. – tmighty May 26 '20 at 22:54
  • See the notes [here](https://stackoverflow.com/a/61689162/7444103) about a custom [DebuggerVisualizer](https://learn.microsoft.com/en-us/visualstudio/debugger/create-custom-visualizers-of-data?view=vs-2019). With a sample code to show Images while debugging (if you speak C#). Is this what you're referring to? – Jimi May 26 '20 at 23:00
  • @Jimi Thank you, I need to continue tomorrow. I will report back. – tmighty May 26 '20 at 23:11
  • You could always use Reflection to iterate over all properties of an object, pulling out their names, types, and values. You'd have to also use Reflection to check which property types are base data types and which are not, as more complex types will require recursion. Does this sound more like what you are looking for? – Sean Skelly May 27 '20 at 00:22
  • @SeanSkelly Yes, sounds like it. I tried various approaches shown on stackoverflow, but I couldn't get them to work. I guess I should try them more seriously. – tmighty May 27 '20 at 11:14
  • @SeanSkelly I have reformulated the question as I think I know the real problem now. – tmighty May 27 '20 at 11:49
  • 1
    You don't have properties in your class. you have public fields. See my first comment. –  May 27 '20 at 11:54

0 Answers0