Solved

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

Posted on 2003-12-08
3
212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

691 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