Click to See Complete Forum and Search --> : There's got to be a better way of doing this


^Johnny2Bad
July 13th, 2007, 07:32 PM
This should be a quickie...

As per the following code

int IPv4Control::ReturnIndexOfName(String ^Name)
{
int Index=0;
int Result=-1;
dataBox^ dbCurr;

dbCurr=gcnew dataBox();

for each (Object^ currObj in this->Controls)
{
if (currObj->GetType()==dbCurr->GetType())
{
dbCurr=(dataBox^) currObj;

if (Name==dbCurr->Name)
{
Result=Index;
break;
}
}
Index++;
}

return Result;
}


I am iterating through the controls collection of the current windows form looking for all the "dataBoxes", which when found are examined too see if the name matches the String^ Name. My problem with this is I have too create a dummy databox (dbCurr) with gcnew in order to access the GetType for that object which apart from that does not get used.

Is there another way too get the dataBox type without creating a databox, so I can save some memory and the code ultimately does not look sloppy.

Cheers,
Jonathan.

Sahir
July 14th, 2007, 06:29 AM
for each (Object^ currObj in this->Controls) {
if(currObj->GetType()->Name->Equals("Whatever"))
{
// do something
}
}

hspc
July 14th, 2007, 02:12 PM
I prefer using typeid to string comparisons:
for each (Object^ currObj in this->Controls)
{
if(currObj->GetType()==dataBox::typeid)
{
//do something
}
}

JamesSchumacher
July 21st, 2007, 06:36 PM
int nCount = this->Controls->Count;

for (int x = 1; x <= nCount; ++x) // Forgot if controls collections are 0 or 1 based
{
// Names should not clash, correct?
if (this->Controls[x]->Name == Name)
{
index = x;
break;
}
}


A simple for loop incrementing an integer, should be better performance than the for each loop.

hspc
July 22nd, 2007, 04:55 AM
// Forgot if controls collections are 0 or 1 based
they are 0 based.

// Names should not clash, correct?
yes..but he wants to do something on all controls of a certain type on the form.
if he already knows the name of the control, he could just use: controlsname->dosomthing();

JamesSchumacher
July 22nd, 2007, 04:02 PM
Read his code. He wants to return the index of a given name (Because he is searching if a control has a certain name). Of course he is 'giving the name'.

It's not that he wants to do something with that control, he wants the INDEX into the Controls collection. Look at his code, and read his post.

Second of all, if the name does not match, then he does not have to do a check of the types. Why do unnecessary type checking if the name you are looking for does not match? String matching in names of controls would be fast, as the strings are short. Therefore, why do any type checking unless the names match? :confused: And you do not need to use a for each because the base class System::Object defines GetType().


int nIndex = -1;
int nCount = this->Controls->Count;

for (int x = 0; x < nCount; ++x)
{
// Names should not clash, correct?
if (this->Controls[x]->Name == Name)
{
if (this->Controls[x]->GetType() == dataBox::typeid)
{
nIndex = x;
}

break; // should be no other control with the same name
}
}


Question is, do you want to call the GetType() and dataBox::typeid methods if the name does not match? I know I do not.

Comparing strings you are not creating any new objects, by calling GetType() you are getting a new object instance representing the data, same with calling dataBox::typeid, and you have 2 methods being called to boot. In this situation, it is more efficient both memory and speedwise, to compare the names of ALL controls, and only check the type if it matches.