473,394 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

Many thanks to Bill McCarthy who suggested to do something like:
MyErrorHandler.AddError(GetType(TheClass).GetPrope rty("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

Jun 27 '08 #1
4 1776
"Etienne-Louis Nicolet" <ni******@enicolet.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

Jun 27 '08 #2
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*******@freenet.dewrote in message
news:uv**************@TK2MSFTNGP03.phx.gbl...
"Etienne-Louis Nicolet" <ni******@enicolet.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

Jun 27 '08 #3
"Etienne-Louis Nicolet" <ni******@enicolet.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(exception)" in the case of handling exceptions. I'm writing this
without having used ErrorProviders so far...
Armin

Jun 27 '08 #4
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*******@freenet.dewrote in message
news:u8**************@TK2MSFTNGP04.phx.gbl...
"Etienne-Louis Nicolet" <ni******@enicolet.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(exception)" in the case of handling exceptions. I'm writing
this
without having used ErrorProviders so far...
Armin

Jun 28 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Nigel Sampson | last post by:
ey all, I'm in the process of creating a method of tracking whenever an objects properties are changed. Since its not possible to alter existing types I decided to used a proxy generated using...
6
by: Joel | last post by:
I need to inspect the current AppDomain for an object that implements a certain interface and get a property from the object. I know how to find out what class implements my interface but...
4
by: Lucas Tam | last post by:
Hi all, I need to set a property on a subclass such as: Report.ExportOptions.ExportFormatType = ExportFormatType.Excel AND Report.ExportOptions.FormatOptions = excelFormatOpts
5
by: Erol | last post by:
How do I get a type from a string? I'm retrieving a string value from my database so that I can set my property values dynamically. In the event "Form1_Load", you will see that I'm trying to set...
10
by: Qwert | last post by:
Hello, is it correct that if a type (System.Type) is a value (.IsValueType=True) and not primitive (.IsPrimitive=False), that type is a structure? Thanks.
0
by: optictygre | last post by:
Databinding with reflection I have a class, SmartString: Public Class SmartString ...Psuedo code follows Public Property Value() as String Return Me.Text
0
by: liko81 | last post by:
I have an Invoice class that must know, directly or indirectly, how to do anything associated with creating, reading, or otherwise processing an invoice to a customer. It is an uber-DAO object that...
7
by: Nemisis | last post by:
Hi everyone, Can anyone tell me if it is possible to pass in a property of an object into a sub, and within that sub, find out the name of the item that was passed along with the property name??...
2
by: Jay | last post by:
Is there a way to access the actual datatype of a property when looking at the PropertyInfo? For example: Property defined: public Nullable<DateTimeEndDate { get { return endDate; }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.