Possible to set/reference a property 'dynamically'?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi all

I've got a number of classes already developed (basic entity classes)
like the following:

Public Class Contact
Private _firstname as String
Private _age as Integer

Public Property FirstName As String
Get
Return Me._firstname
End Get
Set
If (Me._firstname = value) Then
Return
End If
Me._firstname = value
End If
End Set
End Property

Public Enum PropertyList
FirstName,
Age
End Enum

.....

There are a number of classes like this that describe different
entities (Company, User etc etc) - all of which have a public Enum that
lists each available property for the entity.

I need to write a (separate) function that takes type-safe parameters
based on the properties available with these classes - e.g.

MyFunction(Firstname="John")
or
MyFunction(classname, parameter=value)

I imagined that to do this I would need to create a local instance of
the relevent class (i.e. Contact) in the function and (possibly) use
the param that's passed in the function call in a property setter call,
but that's where I fell over really. Firstly because I won't know the
name of the property to set (and didn't want to use a load of Select
Cases) and secondly because it wouldn't enforce type-safety in the
function call doing it this way).

Firstly, is this possible? If so, how could I go about a) limiting the
parameter that's passed to the function to those listed in the class
enum, and secondly, is it possible to enforce the property-type of the
passed parameter (i.e. if I pass a Firstname param, it will only accept
a string, if I pass an Age param, it'll only accept an Int)?

Sorry if these are stupid questions but I'm still trying to learn .Net
and OOP.

Thanks in advance
Martin

.



Relevant Pages