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

  • Thread starter Etienne-Louis Nicolet
  • Start date

E

Etienne-Louis Nicolet

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.Reflection.PropertyInfo,
pErrorCode As Integer)
....
End Sub
....
End Class

Many thanks to Bill McCarthy who suggested to do something like:
MyErrorHandler.AddError(GetType(TheClass).GetProperty("MyProperty").

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
 
Ad

Advertisements

A

Armin Zingler

Etienne-Louis Nicolet said:
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
 
E

Etienne-Louis Nicolet

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 said:
Etienne-Louis Nicolet said:
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
 
A

Armin Zingler

Etienne-Louis Nicolet said:
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(exception)" in the case of handling exceptions. I'm writing this
without having used ErrorProviders so far...


Armin
 
Ad

Advertisements

E

Etienne-Louis Nicolet

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top