Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

accessing comp name dinamically?

Status
Not open for further replies.

jamesp0tter

Programmer
Feb 20, 2003
119
PT
i have:

N TShape objects named 'shape1'...'shape2'...'shapeN'

a string list with strings '1'...'2'...'N'

where N is an unknown number

can i access the TShape object named
*figure* 'shape' + Str[N] */figure* ?

example:
Code:
 TShape -> 'shape7'
 String -> 7
  what_i_want := 'shape' + 7;
---
 shape7.do_WHAT_i_WANT

i know in PHP we can use {'shape'.string}, if this helps clarify the problem to anyone into php ;)

tks in advance


jamesp0tter,
jamespotter@netcabo.pt

p.s.: sorry for my (sometimes) bad english :p
 
hi

for idx := 0 to ComponentCount-1 do
if components[idx] is TShape then
if (components[idx] as TShape).name = 'shape7' then
...

Hope this helps
lou

 

Hold pointers to your shapes in an array.

Code:
type
  TShapeArray = array[1..n] of TShape;
var
  ShapeArray : TShapeArray;

...
ShapeArray[1] := Shape1;
ShapeArray[2] := Shape2;
...
ShapeArray[N] := ShapeN;
...
[COLOR=red][b]
x := StrToInt(Str[n]);   // x, n are integer vars
ShapeArray[x].SomeMethod;
[/b][/color]

Delphi is a compiled language; you can't make symbolic address evaluation at run time.

buho (A).
 
Can't you also do...

// assuming I is the number...
With TShape(FindComponent('Shape'+IntToStr(I))) Do
Yada....



????

Regards,
JGS
 
LucieLastic is correct:

I've asked this question too in the last few months, and that code will work. I've expanded it here for you

Code:
for idx := 0 to ComponentCount-1 do
  if components[idx] is TShape then
    if (components[idx] as TShape).name = 'shape7' then
      (components[idx] as TShape).MyMethod;

If your TShape objects don't belong to your form (ie. you're creating them at runtime), then consider adding them to a TObjectList, which will allow you to do for..do loops on your shapes to find the one you want.
 
none of the solutions solved my problem :(

the only one that *almost* worked, but end up in an exception was jgslater9's ...

in my app the TShape objects are created at runtime, each one named PChar('shape' + inttostr(K)) where K is incremented always a TShape is created. I tried the TObjectList, but with no luck 'cuz i can't access the *component's* methods, like ObjectList.Items[x].blabla ... Maybe i'm doing something wrong *unsure*

If it's easier i can create the TShape with K in the hint, so if it's easier that way no problem ;)

tks again :]

i just want to find the component and free it lol :((( the way doesn't matter :x


jamesp0tter,
jamespotter@netcabo.pt

p.s.: sorry for my (sometimes) bad english :p
 
if you use objectlist you must cast the object as a TShape when you retreive the object.

Code:
var MyShape : TShape;
...
MyShape := TShape(ObjectList.Items[x])
MyShape.whateverpropertyyouwant:=xxx;
...



--------------------------------------
What You See Is What You Get
 
Your exception probably came from an invalid component where "I" was allowed to be a number for a Shape"I" that had not been created.

My components aren't dynamic so I don't have that problem.

If you don't "know" whether a component exists or not you could put a try/except on your "for" loop and in the except do a break or a continue ...


JGS
 
thank you ALL i got my problem solved :p

TObjectList created on form create, add the TShape objects to the list when created, and used this code:
Code:
procedure TForm1.ListView1Deletion(Sender: TObject; Item: TListItem);
var t: integer; cenas: TShape;
begin
  for t:=0 to obj.Count-1 do
  begin
    cenas := TShape(obj.Items[t]);
    if cenas.Name = 'shape' + Item.Caption then cenas.Free;
  end;
end;

works like a charm!

tk u all ;D

jamesp0tter,
jamespotter@netcabo.pt

p.s.: sorry for my (sometimes) bad english :p
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top