Friday, March 16, 2012

Looping through items in component class

Hello everyone,I want to iterate (for or foreach loop) through all my sqldataadapters in a component class at runtime.Right now I am doing:' Declaration:private Container iControls = new Container();' Adding:this.iControls.Add(this.daUTM5, "daUTM5");this.iControls.Add(this.da..., "da...");etcObviously it would be nice/better if I could useforeach element in this.Container.Componentsor in this.components.Componentsbut these collections seem to be empty at runtime...Anyone know how I could do this?Wouter

You need to implement IEnumerable to be able to do that. I have several small objects that have need for this enumerator, so I created an abstract ObjectCollection class that implements an IEnumerable interface for databinding controls. All you have to do is declare a new object collection, and pass in an array of your custom objects. Then in code behind, if you ever need to access an object from the collection, just use the GetObject(index) property. It will return as an object, but you can change the type in order to program more effectively.

i.e.

dim t as textbox = DirectCast(objCollection.GetObject(0), textbox)

' get text

dim strText = t.Text

The code is in VB, though it shouldn't be hard to translate. Visitthis post to view the ObjectCollection class.

0 comments:

Post a Comment