Register | Login
Thursday, August 21, 2008

Sections
  
About Us
  
Hosting Provided by Server Intellect
Partners
Downloads
  
 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 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.
  

 Latest Articles
  

 Latest News
  

 

Spotlight
Syndication

 


 


Digg This
 


DotNetNuke Platinum Benefactor

  
 

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