Solved

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

Posted on 2003-12-08
3
208 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
ID: 9899969
look at the findcomponent-method

just as hint, no time yet

meikl ;-)
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 125 total points
ID: 9900184

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
ID: 9900336
ty a lot man that solved my problem :)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

813 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

11 Experts available now in Live!

Get 1:1 Help Now