VB2008: Getting the System.Reflection.PropertyInfo from within a class property

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Etienne-Louis Nicolet

    VB2008: Getting the System.Reflection.PropertyInfo from within a class property

    I am working on an error handler class. Within a property set method of a
    given class property I'd like to pass the PropertyInfo to a function.
    Imagine something like:

    Public Class MyClass
    ....
    Public Property MyProperty() as String
    Get
    Return _myProperty
    End Get
    Set (ByVal pMyProperty as String)
    ' Validate property. If there's an error, add an item to the
    error handler
    MyErrorHandler. AddError([MyProperty], errorCode) ' <--- How to
    get the reference to 'MyProperty'?
    End Set
    End Property
    ....
    End Class


    Public Class MyErrorHandler
    ....
    Public Sub AddError(ByVal pProperty As System.Reflecti on.PropertyInfo ,
    pErrorCode As Integer)
    ....
    End Sub
    ....
    End Class

    Many thanks to Bill McCarthy who suggested to do something like:
    MyErrorHandler. AddError(GetTyp e(TheClass).Get Property("MyPro perty").

    I'd rather like to use a statement that does not hard-code the property
    name, but rather uses intellisense, which makes code less vulnerable to
    possible changes.

    Many thanks for any ideas,
    Etienne



  • Armin Zingler

    #2
    Re: VB2008: Getting the System.Reflecti on.PropertyInfo from within a class property

    "Etienne-Louis Nicolet" <nie.msft@enico let.chschrieb
    I am working on an error handler class. Within a property set method
    of a given class property I'd like to pass the PropertyInfo to a
    function. Imagine something like:
    >
    Public Class MyClass
    ....
    Public Property MyProperty() as String
    Get
    Return _myProperty
    End Get
    Set (ByVal pMyProperty as String)
    ' Validate property. If there's an error, add an item to
    the error handler
    MyErrorHandler. AddError([MyProperty], errorCode) ' <---
    How to get the reference to 'MyProperty'?
    End Set
    End Property
    ....
    End Class
    I prefer raising an exception. If it is caught anywhere, maybe in the
    thread's main sub, you can programatically examine the Exception object to
    get all necessary information, including the PropertyInfo.


    Armin

    Comment

    • Etienne-Louis Nicolet

      #3
      Re: VB2008: Getting the System.Reflecti on.PropertyInfo from within a class property

      Many thanks for your tip. Since I'm quite new to VB.NET, I just try to get
      into Exception handling.
      The idea was the following: MyClass is responsible for the data handling. In
      case user input is required the the data is passed to a form which then
      returns the new/modified data back to MyClass.MyClass validates the data. In
      case of errors it would send a kind of error report (what i called my error
      handler) which would finally feed the form's ErrorProviders. ..

      "Armin Zingler" <az.nospam@free net.dewrote in message
      news:uvQazPu1IH A.1772@TK2MSFTN GP03.phx.gbl...
      "Etienne-Louis Nicolet" <nie.msft@enico let.chschrieb
      >I am working on an error handler class. Within a property set method
      >of a given class property I'd like to pass the PropertyInfo to a
      >function. Imagine something like:
      >>
      >Public Class MyClass
      > ....
      > Public Property MyProperty() as String
      > Get
      > Return _myProperty
      > End Get
      > Set (ByVal pMyProperty as String)
      > ' Validate property. If there's an error, add an item to
      >the error handler
      > MyErrorHandler. AddError([MyProperty], errorCode) ' <---
      >How to get the reference to 'MyProperty'?
      > End Set
      > End Property
      > ....
      >End Class
      >
      I prefer raising an exception. If it is caught anywhere, maybe in the
      thread's main sub, you can programatically examine the Exception object to
      get all necessary information, including the PropertyInfo.
      >
      >
      Armin

      Comment

      • Armin Zingler

        #4
        Re: VB2008: Getting the System.Reflecti on.PropertyInfo from within a class property

        "Etienne-Louis Nicolet" <nie.msft@enico let.chschrieb
        Many thanks for your tip. Since I'm quite new to VB.NET, I just try
        to get into Exception handling.
        The idea was the following: MyClass is responsible for the data
        handling. In case user input is required the the data is passed to a
        form which then returns the new/modified data back to
        MyClass.MyClass validates the data. In case of errors it would send
        a kind of error report (what i called my error handler) which would
        finally feed the form's ErrorProviders. ..
        Hmmm... not easy find the right approach... It also depends on where you
        want to continue if an error(/exception) occurs. If you want to handle
        exceptions at different locations individually but standardized - that's how
        it looks in your example - you can still call a method in MyClass, and,
        inside, walk the Stacktrace (Dim bla as new Stacktrace(<... >)) and retrieve
        the required information, just like you can do it with "New
        Stacktrace(exce ption)" in the case of handling exceptions. I'm writing this
        without having used ErrorProviders so far...


        Armin

        Comment

        • Etienne-Louis Nicolet

          #5
          Re: VB2008: Getting the System.Reflecti on.PropertyInfo from within a class property

          Dear, Armin. As mentioned I'm a Newbie, so your answers are quite nourishing
          and therefore take some time to be digested ;-) In fact, stacktraces might
          be the right approach, I'll try to get deeper into it!

          Many thanks for your kind support, you're giving me lots of new input!

          Kind regards,
          Etienne

          "Armin Zingler" <az.nospam@free net.dewrote in message
          news:u8l0mR71IH A.4188@TK2MSFTN GP04.phx.gbl...
          "Etienne-Louis Nicolet" <nie.msft@enico let.chschrieb
          >Many thanks for your tip. Since I'm quite new to VB.NET, I just try
          >to get into Exception handling.
          >The idea was the following: MyClass is responsible for the data
          >handling. In case user input is required the the data is passed to a
          >form which then returns the new/modified data back to
          >MyClass.MyClas s validates the data. In case of errors it would send
          >a kind of error report (what i called my error handler) which would
          >finally feed the form's ErrorProviders. ..
          >
          Hmmm... not easy find the right approach... It also depends on where you
          want to continue if an error(/exception) occurs. If you want to handle
          exceptions at different locations individually but standardized - that's
          how
          it looks in your example - you can still call a method in MyClass, and,
          inside, walk the Stacktrace (Dim bla as new Stacktrace(<... >)) and
          retrieve
          the required information, just like you can do it with "New
          Stacktrace(exce ption)" in the case of handling exceptions. I'm writing
          this
          without having used ErrorProviders so far...
          >
          >
          Armin
          >

          Comment

          Working...