Name:
Location: Mumbai, India

Friday, May 19, 2006

System.Runtime.Reflection

Here's a way to access the Objects that are added to ur current form without actually going through loop of the control list.

Normally one will loop through the Controls Collection and look for a particular control this is very efficient if you have very less no of controls, imagine a form having many controls and you need to change the properties of the controls dynamically doing this by looping will slow down the application.

The other way is to use Hashtable instead, HashTable is nothing but a collection of the References of the objects.
Example:
C#
using System.Collection;
using System.Refelection;

HashTable tmpHashTable=new Hashtable();
private void InitializaComponent()
{
//Here add all the controls to the HashTable.
tmpHashTable.Add(Control.Name, Control);
}

Then u can directly refere to the Object i.e get the reference of a particular object by just passing the name of the control.

Control tmpControl=(Control)tmpHashTable[Name of the Control];

Note: Hash Table Contains Only Reference of the Object and not the actual object.
so to use the properties u have to type cast the object to control.

0 Comments:

Post a Comment

<< Home