* * *

Author Topic: TStringlist - accessing a string by its list number  (Read 312 times)

JohnSaltwell

  • Jr. Member
  • **
  • Posts: 76
    • Personal blog
TStringlist - accessing a string by its list number
« on: October 11, 2018, 03:21:05 pm »
Can anyone advise me how to access a single string in a stringlist? This should be simple but seems to be bafflingly different from Delphi, and so far impossible for me to fathom. I'm trying to create a list and then just read the strings in it one at a time.

Code: Pascal  [Select]
  1. DBases:= TStringlist.Create;  
  2. V:= DBases.Add(Path + R.Name);
  3. writeln(SL, 'Added list item + ' + IntToStr(v));
  4. writeln(SL, 'Added entry to History files list: ' + Path + R.Name + '. List count is ' + IntToStr(DBases.Count));  
  5. ActualPath:= DBases.Strings[0];  

Line 4 shows that the string was added but ActualPath in line 5 shows nothing and the debugger tells me <Error: Type TSTRINGLIST has no component named STRINGS[0].> The result is the same if I change the property to DBases[0] or DBases.Names[0]. I gather the second wouldn't work because the value stored is just a simple string, not a Name | Value pair. Surely a stringlist can be used to store just plain strings - that is, not in pairs?

Thanks in advance.   

                                         
John H, UK
Lover of the old (reasonably priced!) Delphi
Still inexperienced with FPC/Lazarus. Not an instinctive programmer!

Cyrax

  • Hero Member
  • *****
  • Posts: 611
Re: TStringlist - accessing a string by its list number
« Reply #1 on: October 11, 2018, 03:49:37 pm »
Show us more code. Are you sure that type of variable DBases is TStringList?

EDIT : Disable all optimization flags. Also you can't view values of properties in the debugger directly if there are read/write subroutines defined. GDB does not have a support for this.
« Last Edit: October 11, 2018, 03:54:14 pm by Cyrax »

Thaddy

  • Hero Member
  • *****
  • Posts: 6904
Re: TStringlist - accessing a string by its list number
« Reply #2 on: October 11, 2018, 03:53:43 pm »
Tstringlist.strings is a property. GDB can't handle properties very well.
This is a known limitation of the GDB debugger.

You can access the underlying TList / Flist, however, as well as any getter and setter.

Also note *previous* in https://www.freepascal.org/docs-html/rtl/classes/tstringlist.add.html
« Last Edit: October 11, 2018, 04:05:13 pm by Thaddy »
Ada's daddy wrote this:"Fools are my theme, let satire be my song."

wp

  • Hero Member
  • *****
  • Posts: 4982
Re: TStringlist - accessing a string by its list number
« Reply #3 on: October 11, 2018, 03:56:23 pm »
But a "WriteLn(SL, ActualPath)" would show you that everything is correct. You must know that the debugger gdb has problems understanding the Pascal classes and their properties.

As for StringList.Strings[index] and StringList.Names[index]: When you "Add" a string to a StringList like you do in line 2 then you can access the added string as StringList.Strings[index] where index, for example, is the number returned by "Add". Since Strings[] is the "default" property of TStringList, you can omit it and write simply like an array: StringList[index].

If you "Add" a string of the kind "Name=Value" then you can make TStringList split that into its Name and Value parts: StringList.Names[index] returns the part of the string before the '=' and StringList.Values[name] returns the part after the '='. There is also a TStringList.ValueFromIndex[index] - its function is clear from its name.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

JohnSaltwell

  • Jr. Member
  • **
  • Posts: 76
    • Personal blog
Re: TStringlist - accessing a string by its list number
« Reply #4 on: October 12, 2018, 05:14:24 pm »
Show us more code. Are you sure that type of variable DBases is TStringList?

EDIT : Disable all optimization flags. Also you can't view values of properties in the debugger directly if there are read/write subroutines defined. GDB does not have a support for this.


Thanks. Yes, DBases is declared as TStringList. I may just have got confused by the limitations of the debugger but ActualPath seems to be empty. I'll try wp's suggestion of writing it to my log file (as this is a hidden program and has no user interface). I'll check my code again and try to post what seem like the relevant parts.
John H, UK
Lover of the old (reasonably priced!) Delphi
Still inexperienced with FPC/Lazarus. Not an instinctive programmer!

JohnSaltwell

  • Jr. Member
  • **
  • Posts: 76
    • Personal blog
Re: TStringlist - accessing a string by its list number
« Reply #5 on: October 12, 2018, 05:41:26 pm »
Think I've spotted what I've done wrong - setting Result:= False within a recursive function (to search folders) instead of externally, only before the first call - probably why it never returns true because it's called again after it has returned true...
John H, UK
Lover of the old (reasonably priced!) Delphi
Still inexperienced with FPC/Lazarus. Not an instinctive programmer!

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus