?
Solved

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

Posted on 2003-12-08
3
Medium Priority
?
213 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 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month12 days, 4 hours left to enroll

752 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