CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2005
    Posts
    130

    Question There's got to be a better way of doing this

    This should be a quickie...

    As per the following code
    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.

  2. #2
    Join Date
    Aug 2002
    Location
    Kerala
    Posts
    1,183

    Re: There's got to be a better way of doing this

    Code:
      for each (Object^ currObj in this->Controls) 	{
    	  	if(currObj->GetType()->Name->Equals("Whatever")) 
          {
           // do something
          }
     }

  3. #3
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210

    Re: There's got to be a better way of doing this

    I prefer using typeid to string comparisons:
    Code:
    for each (Object^ currObj in this->Controls)
    {
    	if(currObj->GetType()==dataBox::typeid) 
    	{
    		//do something
    	}
    }
    Hesham A. Amin
    My blog , Articles


    <a rel=https://twitter.com/HeshamAmin" border="0" /> @HeshamAmin

  4. #4

    Re: There's got to be a better way of doing this

    Code:
    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.

  5. #5
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210

    Re: There's got to be a better way of doing this

    Quote Originally Posted by JamesSchumacher
    // Forgot if controls collections are 0 or 1 based
    they are 0 based.

    Quote Originally Posted by JamesSchumacher
    // 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();
    Hesham A. Amin
    My blog , Articles


    <a rel=https://twitter.com/HeshamAmin" border="0" /> @HeshamAmin

  6. #6

    Re: There's got to be a better way of doing this

    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? And you do not need to use a for each because the base class System::Object defines GetType().

    Code:
    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.
    Last edited by JamesSchumacher; July 22nd, 2007 at 03:14 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured