casting collections heeeellpppp

hello

i am trying to turn these 2 methods into one

private int IndexOfControl(string strName, string strControlType)
{
int iIndex = 0;
foreach(Control tmpControl in this.Controls)
{
if(tmpControl.Name == strName & tmpControl.GetType().ToString() == strControlType)
return iIndex;

iIndex++;
}
iIndex = 0;
return -1;
}

private int IndexOfColumn(string strName, string strControlType)
{
int iIndex = 0;
foreach(columnheader comunalHeader in this.listView_LanguageDirs.Columns)
{
if(comunalHeader.Name == strName & comunalHeader.GetType().ToString() == strControlType)
return iIndex;

iIndex++;
}
iIndex = 0;
return -1;
}

this is what i have so far but the casting seems not to work
i dont know how to cast properly
any ideas ?

private int IndexOfComunal(string strName, string strControlType, object collection, object iter)
{
int iIndex = 0;
CollectionBase cb;

cb = (CollectionBase) collection;
iter = (Type.GetType(iter.GetType)) iter;

foreach(iter in cb)
{
if(comunalHeader.Name == strName & comunalHeader.GetType().ToString() == strControlType)
return iIndex;

iIndex++;
}

return -1;
}

thanks
[1366 byte] By [paxromana] at [2007-11-18 23:37:41]
# 1 Re: casting collections heeeellpppp
I assume this is the problem?

iter = (Type.GetType(iter.GetType)) iter;

Casting only works with explicit classes eg. (YourClass)iter. "GetType()" only returns a Type object that describes your class.

What is it that you are trying to do? Do to anything useful you need to know what methods you are going to send to an object, in which case why not cast your object using an interface.
Norfy at 2007-11-9 1:40:18 >
# 2 Re: casting collections heeeellpppp
someone found a solution for me

private int IndexOfComunal(string strName, string strControlType, object collection)

{

int iIndex = 0;
IList cb = (IList) collection;

foreach(object obj in cb)
{

Type type = obj.GetType();
PropertyInfo pi = type.GetProperty("Name");
string name = (string)pi.GetValue(obj, BindingFlags.Public | BindingFlags.GetProperty, null, null, null);
if(name == strName & type.ToString() == strControlType)
return iIndex;

iIndex++;

}

return -1;

}
paxromana at 2007-11-9 1:41:19 >