sinDizzy
sinDizzy

Reputation: 1364

Name of variable assigned in .NET

VB2010. Not even sure if this is possible but for debugging purposes I want to print out the name of the variable that was assigned to a master variable (both being classes).

        Dim mstTripClass As Trip = Nothing
        Select Case sc
            Case "0"
                mstTripClass = driver.BusinessTrip
            Case "7", "8"
                mstTripClass  = driver.LeisureTrip
            Case "10"
                mstTripClass = driver.OtherTrip
            Case Else
                Throw New Exception("Invalid trip class")
        End Select

        Debug.Print("Trip class=" & mstTripClass.GetType.ToString)

This prints out

        Trip class=MyCompany.MyDept.MyApp.Trip

What i would like to print out is something like:

        Trip class=BusinessTrip

I'm not even sure how to search for this type of thing. I searched for reflection but came up empty.

Upvotes: 0

Views: 56

Answers (1)

No reflection needed here at all. Two easy alternatives come to mind:

  1. Turn driver into an Enum having the values BusinessTrip, LeisureTrip etc.

  2. Whatever type driver.BusinessTrip has, extend that type with a abstract string property TypeDescription and have the BusinessTrip, LeisureTrip etc. subtypes override the property so that they return a textual description.

Upvotes: 1

Related Questions