Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 09-11-2009
Member
 
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
Reply With Quote
  #2  
Old 09-11-2009
Member
 
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
Reply With Quote
  #3  
Old 09-11-2009
Member
 
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
Reply With Quote
  #4  
Old 09-11-2009
Member
 
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
Reply With Quote
  #5  
Old 09-11-2009
Member
 
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
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to access a property or attribute by its name passed as parameter"
Thread Thread Starter Forum Replies Last Post
Web pages go static when passed through IPS Sera-phina Networking & Security 4 09-05-2011 07:51 PM
Unable to delete read-only attribute folder - access denied rowanld Windows Security 5 09-04-2011 08:18 AM
Arguments passed by value and reference Chellam Software Development 4 30-12-2010 04:57 AM
Giving access to AD user attribute read/write Drazen Active Directory 4 24-02-2010 12:30 PM
Finally , Passed Ccna With 987 ! samyei Education Career and Job Discussions 9 30-12-2009 10:17 PM


All times are GMT +5.5. The time now is 12:01 AM.