locked
How can I access property value by name RRS feed

  • Question

  • Hi there;

    I need a way to access any object's any property value by its name. Like in databindings.add method -- > object.DataBindings.Add(property, object, object).
    I need to create a sub getting object and its property name as parameters. Later in the program logic I will access that object's property value.

    Property grid can retrieve any object's property list and values, so there must be a way to access programmaticly.

    Thanks in advance.
    Real Blue
    Friday, August 28, 2009 10:00 AM

Answers

  • Properties rarely take an argument.  You'll need this:

      Return Prop.GetValue(ctl, Nothing)

    Hans Passant.
    • Proposed as answer by VinilV Friday, August 28, 2009 11:57 AM
    • Marked as answer by Real Blue Sunday, August 30, 2009 2:32 PM
    Friday, August 28, 2009 11:52 AM

All replies

  • Check out the GetType().GetProperties() method on any instance of your object.
    Friday, August 28, 2009 10:05 AM
  • Hi Christoph,

    Thank you for the prompt answer. As far as I understand GetProperty method doesn't return value of that property. Check out the lines below.

                Dim myType As Type = GetType(TextBox)
                Dim myPropInfo As PropertyInfo = myType.GetProperty("Text")


    What I need is that I need to access let's say "Text" property of TextBox1. So I need something like

    function PropertyValue(ctl as object, PropertyName as string) as objet
    return ctl.GetProperty(propertyname)
    end function





    Real Blue
    Friday, August 28, 2009 10:24 AM
  • Use PropertyInfo.GetValue(). 

    Hans Passant.
    Friday, August 28, 2009 10:26 AM
  • You just need to call the GetValue() method on you PropertyInfo object in order to get the Value

    Friday, August 28, 2009 10:29 AM
  • Hi,

    Thanks for the answers so far. I wrote down my function with your help but it doesn't work gives me "Parameter count mismatch" error on the return line.

        Function GetPropertyValue(ByVal ctl As Object, ByVal pn As String) As Object
            Dim myType As Type = ctl.GetType
            Dim Prop As PropertyInfo = myType.GetProperty(pn)
            Return Prop.GetValue(ctl, New Object() {0})
        End Function


    What am I doing wrong? BTW I am using VS2008 if it makes any difference.
    Real Blue
    Friday, August 28, 2009 11:10 AM
  • Properties rarely take an argument.  You'll need this:

      Return Prop.GetValue(ctl, Nothing)

    Hans Passant.
    • Proposed as answer by VinilV Friday, August 28, 2009 11:57 AM
    • Marked as answer by Real Blue Sunday, August 30, 2009 2:32 PM
    Friday, August 28, 2009 11:52 AM
  • The PropertyGrid object uses TypeDescriptor.GetProperties().  This returns a collection of PropertyDescriptor objects.  PropertyDescriptor has a property called Name, which is the name of the property, and it has two methods for getting and setting the value:  GetValue() and SetValue().
    MCP
    Friday, August 28, 2009 9:35 PM
  • Hi there,

    Thank you for the help finally my function is working and here is the code for those who are interested.

    Function GetPropertyValue(ByVal ctl As Object, ByVal pn As String) As Object
            Dim myType As Type = ctl.GetType
            Dim Prop As PropertyInfo = myType.GetProperty(pn)
            If Prop Is Nothing Then
                Return Nothing
            Else
                Return Prop.GetValue(ctl, Nothing)
            End If
        End Function


    Cution : Property name parameter is case sensitive so be carefull. For example if you send "forecolor" instead of "ForeColor" GetProperty returns nothing.

    Real Blue
    Sunday, August 30, 2009 2:38 PM
  • Since you are using VB, there is also a CallByName function that is a little easier to use. But it sounds like you already have your own function to do this.

    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
    Sunday, August 30, 2009 3:52 PM