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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

for each element in colection equivalent

Status
Not open for further replies.

krushpof

Programmer
Jul 4, 2003
72
CA

I am looking for an equivalent in delphi of VB for each.I got a servers items methods that needs the name to return it.But in vb i can use the for each to return the servers name without knowing it.The bad thing is there is no items[int] methods,so i can t just do simple loops.

Thank you!
 
Unfortunately, there is no "for each" construct like VBA (would be nice, though). Here is the customary way to process a collection (in this case an object list that contains custom defined objects):
[blue]
Code:
procedure TfrmMain.DoStuff(AList: TObjectList);
var
  i: integer;
begin
  for i := 0 to AList.Count - 1 do
    with AList[i] as TMySpecialObject do
      begin
        //
        //  do stuff
        //
      end;
end;
[/color]

 

Thank you for the idea.It doesn't seem to be working for this collection.See,this collection is from a historian SDK.It has a com technologie,but was build to be used with mostly vb.Now even with the trick you gave me,i still have the same problem, i cannot access this collection using an integer.It asks for a widestring,but it is what i am looking for.

So i need the name that i myself need to access the name i am looking for.Lucky me there is a defaultserver method.But it means i have to make sure default is set on the right one every time i change machine...

Anyhow,Thank you for your answer!
 
From Zathras's post

AList
is shorthand for
AList.Items

Items being the default property in this case.

If your collection has a widestring index to the default property, it is quite likely that there is another property that takes a integral index.

Good luck


 
Hi,

Well ,i really can't access this collection using an integer.Even worst, the collection is not compatible with TObjectList. I think this a big conceptual error.Like i said,it has been developped with vb6,.net in mind.

I'll try to see if i cannot find a parent object for this,as TObjectList could have been.

Thank you again!
 
If it is really a vb collection object, then you should be able to access it in either way with no problem (VBA code):
[blue]
Code:
Option Explicit

Sub test()
Dim col As New Collection
Dim x
Dim i As Integer

  col.Add (42)
  col.Add (43)
  col.Add (11)
  col.Add (12)
  
  For Each x In col
    MsgBox "By ""For Each"":   " & x
  Next x
  
  For i = 1 To col.Count
    MsgBox "By ""For i = ..."":   " & col(i)
  Next i
    
End Sub
[/color]

 
Well,what can i say.I guess they call it a collection but is not implemented like one.Even in vb it is not accessible with an integer.(Just tried to make sure b4 I write this)

but i could use the for each.

Misconception i think.It is not logical to have a collection with it s item not reachable with an integer.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top