Delegates

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there a way I could access a property of a object either by a string or by
a delegate? For example if I have a class called Person, and Person has two
properties (FirstName, and Lastname) and I have a routine in the calling code
called PrintItem, could I do something like:

Public Module
Public Sub Main()
Dim MyPerson as Person = New Person
PrintItem(MyPerson, "FIRSTNAME")
PrintItem(MyPerson, "LASTNAME")
End Sub
Public Sub PrintItem(byref P as Person, Byval PropertyNameToPrint as String)
(SOME CODE HERE TO DO P. AND THE PROPERTYNAMETO PRINT)
Windows.Forms.MessageBox.Show(P. XXXXX)
End Sub
End Module
 
Ryan said:
Is there a way I could access a property of a object either by a string or by
a delegate? For example if I have a class called Person, and Person has two
properties (FirstName, and Lastname) and I have a routine in the calling code
called PrintItem, could I do something like:

Yup. Look at Type.GetProperty and PropertyInfo.GetValue.
 
Back
Top