Board index » cppbuilder » Find a component by name . alternative ways

Find a component by name . alternative ways

In a project im in i have the following function in a form base class.

template <class T>
T* __fastcall TGenerellForm::getComponent(String sName) const
{
  T *tform(0);
  for(int i = 0; i < ComponentCount; i++)
  {
    if ((tform = dynamic_cast < T *> (Components[i]))!= NULL)
    {
      if(tform->Name == sName)
        return tform;
    }
    tform = 0;
  }
  String str = "Component " + sName + "Not found.";
  throw EConvertError(str);

Quote
}

And this id used on derived forms like this:

getComponent <TButton>("kppSpara")->Caption = "&OK";

The Template functions seems very cumbersome to me. Is there any
alternative way to do this?

--
Mats Gaardstad Friberg Datarutin AB Sweden
spamt...@datarutin.se <- Adress never read
Im mats DOT g DOT friberg AT datarutin DOT se
(When replying to my email remove the nospam text)

 

Re:Find a component by name . alternative ways


I sat down to think after looking through the help files and newsgroups.
I came up with this revised version:

template <class T>
T* __fastcall TfrmGenerell::getComponentT(const String componentName)
{

  T *comp = dynamic_cast<T*>(FindComponent(componentName));
  if (comp == NULL) {
    String str = "Component " + componentName + " not found.";
    throw EConvertError(str);
  }
  return comp;
  // getComponent <TButton>("kppSpara")->Caption = "&OK";

Quote
}

It looks a lot cleaner to me. I'd love to have a discussion though... :-)

--
Mats Gaardstad Friberg Datarutin AB Sweden
spamt...@datarutin.se <- Adress never read
Im mats DOT g DOT friberg AT datarutin DOT se
(When replying to my email remove the nospam text)

Other Threads