Solved

Something like a eval('oblect'+1).whatever ???????

Posted on 2003-12-08
3
206 Views
Last Modified: 2011-09-20
okie.... supose u have 10 text boxies and i named them like box1, box2, box3..... now i want to make a do while or something..

and i want to call all those 10 boxies in this do while but of corse just using one single call.


In ActionScript there is a function named eval that does that:
counter = 0
same = 0
while counter < 11 do
{
   counter++
   if eval('box'+counter).text == 'lala'
   {
      same++
   }
}

Well there ar any possible to make it in Delphi ?

Thanks in advance :)
0
Comment
Question by:furmiga
3 Comments
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
look at the findcomponent-method

just as hint, no time yet

meikl ;-)
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 125 total points
Comment Utility

Perhaps something like...

function Eval(Name: String): TComponent;

  function _Eval(Component: TComponent; Name: String): TComponent;
  var   dwIndex:       Integer;
  begin

     // Set default result
     result:=nil;

     // Check direct component children
     if Assigned(Component.FindComponent(Name)) then
        // Found the component
        result:=Component.FindComponent(Name)
     else
     begin
        // Check the children's children
        for dwIndex:=0 to Pred(Component.ComponentCount) do
        begin
           result:=_Eval(Component.Components[dwIndex], Name);
           if Assigned(result) then break;
        end;
     end;
  end;

begin

  // Start at the application level
  result:=_Eval(Application, Name);

end;

This would allow you to do:

procedure TForm1.Button1Click(Sender: TObject);
var  counter:    Integer;
     same:       Integer;
begin

  counter:=0;
  same:=0;

  while (counter < 11) do
  begin
     Inc(counter);
     if TEdit(Eval('Edit'+IntToStr(counter))).Text = 'lala' then
     begin
        Inc(same);
     end;
  end;

  ShowMessage(IntToStr(same));

end;


The only drawback to this is that you have to cast the result of Eval(...) to the expected component type, like TEdit, TButton, TLabel, etc..., because the function returns a TComponent (or nil).
Note though, you can also use the "as" operator to cast, or use the "is" operator to perform dynamic checking. Using the "with" statement could also make coding a little simpler:

eg:

if (Eval('Edit'+IntToStr(counter)) as TEdit).Text = 'lala' then...
~or~
if Eval('Edit'+IntToStr(counter)) is TEdit then...
~or~

with Eval('Edit'+IntToStr(counter)) as TEdit do
begin
  if (Text = 'lala') then Inc(same);
end;


Hope this helps,
Russell


0
 
LVL 7

Author Comment

by:furmiga
Comment Utility
ty a lot man that solved my problem :)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now