Sections
spotlight
     
     
WWWCoder.com Resource Directory

Properties, Getting and Setting Them Dynamically
1/3/2006 12:05:18 PM

In the following code snippets we'll cover how to get the properties of your object at run time. This can be handy to know if you're going to generate dynamic reports, or some other function of dynamically viewing and assigning values of the properties of an object.

Iterating Through Properties

In this code snippet we have a custom object that contains a method called GetProperties. The purpose of this method is so the object can return an arraylist of all the named properties it contains. Again you could use this to populate a drop down which then adds fields to be reported on or calculated against.

Public Function GetProperties() As ArrayList
   Dim Properties As New ArrayList
   Dim PropertyInfo As PropertyInfo() = Me.GetType.GetProperties()
   Dim PropertyItem As PropertyInfo
   For Each PropertyItem In PropertyInfo
      Properties.Add(PropertyItem.Name)
   Next
   Return Properties
End Function

You can see we use the PropertyInfo object and then used the method GetProperties. This returns an array type which we can then iterate through and build an arraylist that the object will return containing all the named properties it contains.

Setting the Value of a Named Property

In this next method we provide code on how to set the value of a named property at run time. For example, say we used the previous method to obtain a list of named properties, and now we want to set the value of the property for our object.

Public Sub SetProperty(ByVal PropertyName As String, _
   ByVal PropertyValue As String)
   Dim userType As Type = Me.GetType()
   Dim UserProp As PropertyInfo = userType.GetProperty(PropertyName)
   UserProp.SetValue(Me, PropertyValue, Nothing)
End Sub

As you can see again we use PropertyInfo, get the property of the object and pass the current object which in this example is a custom user object. Then pass the value we want the property to contain.

Get the Value of a Named Property

In the next code block, we'll obtain the value of a named property at runtime for our custom object.

Public Function GetPropertyByNamedValue(ByVal PropertyName As _
    String)
   Dim userType As Type = Me.GetType()
   Dim UserProp As PropertyInfo = userType.GetProperty(PropertyName)
   Dim myValue As String
   myValue = UserProp.GetValue(Me, Nothing)
   Return myValue
End Function

Basically a similar method here, again using the PropertyInfo class and then calling the GetValue method passing our custom object, and returning the value we specified when first instantiating the PropertyInfo class.


Page Options:
format for printing  Format for Printer
email article  Email Page
add to your favorites   Add to Favorites
How would you rate the quality of this content?
Poor - - Excellent
Comments?
Overall Rating:
Comments Left:
Left on 6/2/2009 2:48:08 PM by Anonymous
Comments: I get an "Object Reference not set to an instance of an object" error when I run the UserProp.SetValue(Me, PropertyValue, Nothing) code.  I am not able to use the New keyword as I get an error about must inherit.

rZiegler@state.pa.us
Left on 9/23/2008 10:57:47 AM by Anonymous
Comments: great stuff
Left on 2/2/2006 11:31:30 AM by Anonymous
Comments: Here's the set propert translated for C#. You should be able to figure out the rest on your own.
public SetProperty(string PropertyName, string PropertyValue)
{
System.Type userType = this.GetType();
System.Reflection.PropertyInfo userProp = userType.GetProperty(PropertyName);
userProp.SetValue(this, PropertyValue, null);
}
No ratings available.
Left on 1/25/2006 3:32:07 PM by Anonymous
Comments: cool
Left on 1/19/2006 1:12:50 AM by Anonymous
Comments: would love to see this in c#
No ratings available.
     
     

 

     
     

 


 


Digg This
 


DotNetNuke Platinum Benefactor

     
     

Other family network sites: santry.com - katieandkarleigh.com

Powered by 

 

     
Copyright 20010 - Santry Technology Solutions, Box 172, Girard, PA 16417, (814) 774-0970
Privacy Statement | Terms Of Use