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

Searching for an Object in a List

Status
Not open for further replies.

tjcusick

Programmer
Dec 26, 2006
134
US
If i build my list with:
Code:
      [b]while[/b] [b]not[/b] GLookup.Eof [b]do[/b]
      [b]begin[/b]
        GroupLookup.Items.AddObject(GLookup.FieldAsString([teal]'GROUP_NAME'[/teal]), TObject(longInt(NewStr(GLookup.FieldAsString([teal]'GROUP_ID'[/teal])))));
        GLookup.Skip([purple]1[/purple]);
      [b]end[/b];

shouldn't i be able to search that list with:
Code:
  GroupLookup.ItemIndex:=GroupLookup.Items.IndexOfObject(TObject(longInt(NewStr(xxxx))));

Where xxxx is a str of numbers that is equal to (GLookup.FieldAsString([teal]'GROUP_ID'[/teal]))??

I am picking xxxx from an identical list but on a different form. I've stopped the procedures right after xxxx is assigned and it has the correct assigned "string/number", so i know that xxxx is holding the correct value.

Any Help is appreciated...
 
Have you tried something like:

Code:
[navy][i]// for automatic syntax highlighting see faq102-6487 
[/i][/navy][b]while[/b] [b]not[/b] GLookup.Eof [b]do[/b]
  [b]begin[/b]
    GroupLookup.Items.AddObject(GLookup.FieldAsString([teal]'GROUP_NAME'[/teal]), TObject(Ptr(GLookup.FieldAsInteger([teal]'GROUP_ID'[/teal])));
  GLookup.Skip([purple]1[/purple]);
[b]end[/b];

and then
Code:
GroupLookup.ItemIndex := GroupLookup.Items.IndexOfObject(TObject(Ptr(xxxx));

That works for me when I set up a demo. It avoids needing to allocate memory, because all you're working with is memory addresses. It won't cause any AVs because you're not accessing that memory.
 
Do I have to declare ptr as a pointer record? because its not finding the item in the list.
 
Actually that did start to work but then i ran into a snag. I was assuming that the field i was looking at was numeric only, but it appears that there are some non numeric characters in the field. So PTR won't work because PTR needs a number to convert. Does TObject have to have a specific value after it?
 
Ok, doing something similar to the ptr this worked but now the problem is that i am getting an access violation error.
I've narrowed it down to the FieldAsInteger string.

In fact i was getting the same problem with the ptr.

I'm wondering if it's because some of the items in that GROUP_ID field have non numeric characters in them?

Suggestions?

Code:
      [b]while[/b] [b]not[/b] GLookup.Eof [b]do[/b]
      [b]begin[/b]
        xGrpName:=GLookup.FieldAsString([teal]'GROUP_NAME'[/teal]);
        xGrpID:=GLookup.FieldAsinteger([teal]'GROUP_ID'[/teal]);
        GroupLookup.Items.AddObject(xGrpName, TObject(xGrpID));
        GLookup.Skip([purple]1[/purple]);
      [b]end[/b];
      GLookup.GoTop;
 
How about another thought... could it have to do with the size of xGrpID? xGrpID size is length 16, is there a limit with TObject?
 
If anyone is at all interested, basically I gave up trying to get the AddObject/IndexofObject to work, I believe it is because the parameter I was passing to TObject was not a true numeric value and didn’t convert to a true numeric value properly, so below is the solution we came up with that works...

I set xGrp2 to the ID of the drop down item from the first form, then as I build my list for form2 I compared the xGrp2 to xGrp1 which is the ID value of each item as the list is building, and just increment a counter to find how many it is, thus finding the itemindex of the built list.
Some times the simple solution is the one that eludes us... or at least eludes me :)

Remember use the KISS method!!!

Code:
  xGrp[purple]1[/purple], xGrp[purple]2[/purple]:String;

  [b]function[/b] BuildGroupBox(xGrp[purple]2[/purple]:String):Integer;
  [b]var[/b]
    xGLupName: [b]String[/b];
    xGLupID, xInd : Integer;
  [b]begin[/b]
    xInd:=[purple]0[/purple];
    [b]with[/b] RData[purple]2[/purple] [b]do[/b]
    [b]begin[/b]
      GLookup.EnsureOpen;
      GLookup.SetTag([teal]'GROUP_NAME'[/teal]);
      GLookup.GoTop;
[navy]// build the group list dropdown[/navy]
      [b]while[/b] [b]not[/b] GLookup.Eof [b]do[/b]
      [b]begin[/b]
        xGLupName:=GLookup.FieldAsString([teal]'GROUP_NAME'[/teal]);
        xGrp[purple]1[/purple]:=GLookup.FieldAsString([teal]'GROUP_ID'[/teal]);
        [b]if[/b] xGrp[purple]1[/purple] = xGrp[purple]2[/purple] [b]then[/b]
          Result := xInd;
        xGLupID :=LongInt(NEWSTR(xGrp[purple]1[/purple]));
        GroupLookup.Items.AddObject(xGLupName, TObject(xGLupID));
        GLookup.Skip([purple]1[/purple]);
        inc(xInd);
      [b]end[/b];
      GLookup.GoTop;
    [b]end[/b]; [navy]// with[/navy]
  [b]end[/b]; [navy]// procedure BuildGroupBox[/navy]

[b]begin[/b]
  xGrp[purple]2[/purple]:=RosterRecord.RosterGroup;
  xIndex:=BuildGroupBox(xGrp[purple]2[/purple]);
  GroupLookup.ItemIndex:=xIndex;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top