Recent

Author Topic: Can I create a new object by using an existing object and adding to it???  (Read 5743 times)

wpflum

  • Sr. Member
  • ****
  • Posts: 287
I've discovered the wonderful would of Tstings and TStringLists and I LIKE IT!!!  Since I'm relatively new to OOP and pascal can anyone tell me if it is possible or more to the point, desirable, to take something like a TStringlist and add properties to it?? 

Here is the scenario, I'm using a function to return a list of files in a directory in a TStringList which I then loop through to check each file, pdf file, for valid isbn numbers using an external program, pdftotext.  To try and cut down on the amount of pages I have to scan per pdf to look for an ISBN number I'd like to loop through and test each pdf one page at a time.  If the page contains an ISBN then I grab the info from the web and go on to the next document.  What I'd like to do is somehow flag or remove the filename from the TStringList so the next time through the loop I skip the pdf, this way I only keep looking for the ISBN on pdfs I didn't find one in already.  Since I have thousands of documents this would help a lot on speeding up things.

I tried to remove the file name from the TStringList but the loop is based on the TStringList.Count so I get a crash since the new count is one shorter.  I thought I might be able to flag the string in the TStringList somehow so I can skip it on the next loop but am unsure how to do it without using another array which seems redundant.  So I was wondering if there was any way I can take a TStringList and make it the basis for an object that also allows me to use a boolean flag per list object.

Ideas??

Don't be afraid to beat on me for missing something simple as I usually learn the most that way  ;)

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Can I create a new object by using an existing object and adding to it???
« Reply #1 on: December 28, 2010, 07:24:54 pm »
To avoid errors on deleting strings from the stringlist, do it like this:

Code: [Select]
  for i := YourStringList.Count - 1 downto 0 do
  begin
    // your code to delete strings from StringList
  end;

Also, you can use the array of Objects (YourStringList.Objects) to store values and access them like this:

Code: [Select]
YourStringList.Objects[YourStringList.IndexOf(StringName)]
« Last Edit: December 28, 2010, 07:40:18 pm by typo »

wpflum

  • Sr. Member
  • ****
  • Posts: 287
Re: Can I create a new object by using an existing object and adding to it???
« Reply #2 on: December 28, 2010, 07:37:33 pm »
Doh   :o

Never gave that a thought.  :-[

Worked like a charm.   :)

My question about a new object from an old one still stands though, I'm sure some where in the future I'll feel the need for something like it :D


typo

  • Hero Member
  • *****
  • Posts: 3051
Re: Can I create a new object by using an existing object and adding to it???
« Reply #3 on: December 28, 2010, 07:59:17 pm »
For example, you can increase the value of an integer stored on an Object doing like this:

Code: [Select]
YourStringList.Objects[YourStringList.IndexOf(S)] := TObject(Succ(Integer(YourStringList.Objects[YourStringList.IndexOf(S)])));

Maybe property Duplicates := dupIgnore can be useful too.

Also, obviously you can subclass any class and add properties to it and can create a new object from an existing one and add properties and methods.
« Last Edit: December 28, 2010, 08:43:44 pm by typo »

Wodzu

  • Full Member
  • ***
  • Posts: 171
Re: Can I create a new object by using an existing object and adding to it???
« Reply #4 on: December 30, 2010, 09:52:45 am »
@wpflum I guess youhave heard about class inheritance?:) If not it is a must for you to read about it.

However, for your particular needs I guess I would not derive your custom class from TStrignList. I would use a class helper which seems to be perfectably suited for your needs (adding few properties).

I don't know if they are currently implemented in FPC, though.

I've found a thread about them here:

http://www.mail-archive.com/fpc-devel@lists.freepascal.org/msg20456.html




 

TinyPortal © 2005-2018