Results 1 to 5 of 5

Thread: How to access a property or attribute by its name passed as parameter

  1. #1
    Join Date
    Jul 2009
    Posts
    122

    How to access a property or attribute by its name passed as parameter

    Here is a problem probably quite sharp. (Winform Application, Framework 3.5, possibly 2.0 compatible but not required)

    Either a number of different type of control but who all share a property called PropertyMagic. All these controls inherit from the generic class Control and are part of my screen:

    Code:
            Dim A As New MyTypeCtrl1
            Dim B As New MyTypeCtrl2
            Dim C As New MyTypeCtrl3
            Dim .....
     
            Me.Controls.AddRange(New Control() {A, B, C, ....})
    I need to change this property to all controls without necessarily managing their type. The idea is to:

    Code:
            For Each ctrl As Control In Me.Controls
                ctrl.PropertyMagic = MyNewValue
            Next
    Obviously this does not work. PropertyMagic is not a property of Control, but a class derived

    Yet the variable ctrl, although type Control, contains an object of one of the guys who manage this property. How can I then access this property through the variable Control? (Managing the type of controls is not too feasible, because there are already more than a dozen types, and it is not finished)

    The idea would be (perhaps) can do something like:
    Code:
    ctrl.PropertyByName("PropertyMagic")
    I can not imagine that this is not possible in .NET especially since it is standard in other object languages, but I can not find a hint of a solution in the framework. It seems to me to be a basic feature of OOP

  2. #2
    Join Date
    Nov 2008
    Posts
    1,221

    Re: How to access a property or attribute by its name passed as parameter

    Hello, what you seek is called the "reflection" .NET, but using an "interface" seems more appropriate:

    Interface:
    Code:
    Public Interface ImageControl
        Property PropertyMagic() As String
    End Interface
    The implementation on the UserControls Me:
    Code:
    Public Class UserControl1
        Implements ImageControl
     
        Public Property PropertyMagic() As String Implements ImageControl.PropertyMagic
            Get
                '...
            End Get
            Set(ByVal value As String)
                '...
            End Set
        End Property
    End Class
     
    Public Class UserControl2
        Implements ImageControl
     
        Public Property PropertyMagic() As String Implements ImageControl.PropertyMagic
            Get
                '...
            End Get
            Set(ByVal value As String)
                '...
            End Set
        End Property
    End Class
    And finally, with your use foreach in your Form:
    Code:
    For Each ctrl As Control In Me.Controls
        If TypeOf ctrl Is ImageControl Then
            DirectCast(ctrl, ImageControl).PropertyMagic = "..."
        End If
    Next

  3. #3
    Join Date
    Jul 2009
    Posts
    122

    Re: How to access a property or attribute by its name passed as parameter

    I agree to the reflection, except that I could not implement it.

    As for controls, this is not a UserControl. The controls are a third party publisher (libraries that were purchased by the box which we use imposes on me) and I have no access to source code. I therefore can not edit these controls.

    And if I gave the example of a property, I am actually (yet) need access to 2 or 3 properties after treatment

  4. #4
    Join Date
    Nov 2008
    Posts
    1,221

    Re: How to access a property or attribute by its name passed as parameter

    In this case we will start again on the reflection :

    Code:
    For Each ctrl As Control In Me.Controls
        Dim pi As System.Reflection.PropertyInfo = ctrl.GetType().GetProperty("PropertyMagic")
        If pi IsNot Nothing Then pi.SetValue(sender, MyNewValue, Nothing)
    Next

  5. #5
    Join Date
    Jul 2009
    Posts
    122

    Re: How to access a property or attribute by its name passed as parameter

    It seems so simple like that I even managed to get there. For a while it worked the first try, but no, I got an error message. The correct syntax, apparently, is:

    Code:
    For Each ctrl As Control In Me.Controls
        Dim pi As System.Reflection.PropertyInfo = ctrl.GetType().GetProperty("PropertyMagic")
        If pi IsNot Nothing Then pi.SetValue(ctrl, MyNewValue, Nothing)
    Next

Similar Threads

  1. Web pages go static when passed through IPS
    By Sera-phina in forum Networking & Security
    Replies: 4
    Last Post: 09-05-2011, 07:51 PM
  2. Replies: 5
    Last Post: 09-04-2011, 08:18 AM
  3. Arguments passed by value and reference
    By Chellam in forum Software Development
    Replies: 4
    Last Post: 30-12-2010, 04:57 AM
  4. Giving access to AD user attribute read/write
    By Drazen in forum Active Directory
    Replies: 4
    Last Post: 24-02-2010, 12:30 PM
  5. Finally , Passed Ccna With 987 !
    By samyei in forum Education Career and Job Discussions
    Replies: 9
    Last Post: 30-12-2009, 10:17 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,966,014.63477 seconds with 17 queries