Delegates

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ryan

    #1

    Delegates

    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(MyPer son, "FIRSTNAME" )
    PrintItem(MyPer son, "LASTNAME")
    End Sub
    Public Sub PrintItem(byref P as Person, Byval PropertyNameToP rint as String)
    (SOME CODE HERE TO DO P. AND THE PROPERTYNAMETO PRINT)
    Windows.Forms.M essageBox.Show( P. XXXXX)
    End Sub
    End Module
  • Jon Skeet [C# MVP]

    #2
    Re: Delegates

    Ryan <Ryan@discussio ns.microsoft.co m> wrote:[color=blue]
    > 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:[/color]

    Yup. Look at Type.GetPropert y and PropertyInfo.Ge tValue.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    Working...