Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    23
    Rep Power
    0

    Passing a TStringList to a function


    Hello, I am quite new to Delphi, so please be gentle, as they say :-)

    I am trying to write code for a stand-alone unit (a function) to be used in different programs. I need to pass a TSrtingList to this function. Here is the code for this unit. The problem is that every occurrence of "TStringList" is apparently an error: "Undeclared identifier 'TStrngList' at line ..." What am I doing wrong?

    Any help will be greatly appreciated! Thanks very much!

    unit CountGaps;
    interface
    function CountNumGaps(OriAlignment: TStringList): integer;
    implementation
    function CountNumGaps(OriAlignment: TStringList): integer;
    var
    I, J, K, Len, NumSeqs, NumDashes: integer;
    Seq: string;
    Nuc: Char;
    SeqsList: TStringList;
    begin
    ...
    end;
    end;
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2006
    Location
    Carlsbad, CA
    Posts
    2,057
    Rep Power
    383
    TStringList is defined in the classes unit.

    It looks as if you have not added this unit to your interface uses clause.

    Clive
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    23
    Rep Power
    0
    Originally Posted by clivew
    TStringList is defined in the classes unit.

    It looks as if you have not added this unit to your interface uses clause.

    Clive
    Thanks very much, Clive. Appreciate it. I had to play around a bit - and finally got it to work after I put the term "Classes" AND the declaration of the function under "uses" like so:

    unit CountGaps;

    interface

    uses
    Classes;
    function CountNumGaps(OriAlignment: TStringList): integer;

    implementation

    function CountNumGaps(OriAlignment: TStringList): integer;
    var
    //
    begin
    //
    end;


    I do not understand why the function is declared under "uses" here whereas all the functions and procedures are declared under "type" in my main unit. This brings me to a general question. I have not found a single book that really INTRODUCES the novice to Delphi programming. It is really frustrating to try and find answers to what I am sure are pretty simple questions when there is no one to talk to and when there is apparently no proper help in the form of documentation. Would appreciate it if you or anyone in this forum could point me to such a resource. I would hate to keep posting simple questions here and waste your time.

    Thanks very much!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2006
    Location
    Carlsbad, CA
    Posts
    2,057
    Rep Power
    383
    Don't worry about posting real questions that are not easily found in the help.
    I do not understand why the function is declared under "uses"
    It is not.
    The semi-colon after classes indicates the end of the uses section.

    Your function is just that. A function. It is not a type declaration.
    Unlike a type or a variable it does not have a section header (e.g. type or var).

    Not sure about books for the latest versions of Delphi.
    Marco Cantu used to write excellent Delphi books.
    You might try searching for him.

    HTH

    Clive
    Last edited by clivew; July 6th, 2012 at 01:14 PM. Reason: Added book suggestion
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    23
    Rep Power
    0
    Originally Posted by clivew
    Don't worry about posting real questions that are not easily found in the help.

    It is not.
    The semi-colon after classes indicates the end of the uses section.

    Your function is just that. A function. It is not a type declaration.
    Unlike a type or a variable it does not have a section header (e.g. type or var).

    Not sure about books for the latest versions of Delphi.
    Marco Cantu used to write excellent Delphi books.
    You might try searching for him.

    HTH

    Clive
    Thank you.

    I guess I am confused because all the procedures and functions are declared under "type" in my main unit. What makes this unit (my function) different?

    Regarding books, I guess I am looking for an introduction to the basics, not necessarily something on the latest versions. Yes, I have seen the names Marco Cantu and Zarco Gajic on the web. I will try and find some basic books by him/them. Thanks.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2006
    Location
    Carlsbad, CA
    Posts
    2,057
    Rep Power
    383
    I guess I am confused because all the procedures and functions are declared under "type" in my main unit.
    Of course I have not seen your main unit; but I am guessing that those procedures and functions are not
    declared (directly) under type. Rather, I am surmising, that they are procedures and
    functions within the class (type) that was declared.
    e.g. procedures and functions of the class TForm1.
    BTW: procedures and functions in a class are usually referred to as methods of the class.

    The class declaration is also terminated by a semi-colon and anything after it (and not within another class, would be like your function.

    Example pseudo-code:
    Code:
    unit myunit;
    
    interface
    uses
      system, windows, classes;
    
    type
    
       TmyNewClass = class(TPersistent)
        private
    
    
        public
           procedure doThis;
        end;
    
       function getThat: string;
    
    implementation
    uses
       anotherUnit;
    
    {Stand alone function implementation}
    function getThat: string;
    begin
        Result := 'That';
    end;
    
    {method of TmyNewClass implementation}
    procedure TmyNewClass.doThis;
    begin
       // do something here
    end;
    
    end;
    Note the difference in the implementation declarations between the class method and the stand alone function.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    23
    Rep Power
    0
    Originally Posted by clivew
    Of course I have not seen your main unit; but I am guessing that those procedures and functions are not
    declared (directly) under type. Rather, I am surmising, that they are procedures and
    functions within the class (type) that was declared.
    e.g. procedures and functions of the class TForm1.
    BTW: procedures and functions in a class are usually referred to as methods of the class.

    The class declaration is also terminated by a semi-colon and anything after it (and not within another class, would be like your function.

    Example pseudo-code:
    Code:
    unit myunit;
    
    interface
    uses
      system, windows, classes;
    
    type
    
       TmyNewClass = class(TPersistent)
        private
    
    
        public
           procedure doThis;
        end;
    
       function getThat: string;
    
    implementation
    uses
       anotherUnit;
    
    {Stand alone function implementation}
    function getThat: string;
    begin
        Result := 'That';
    end;
    
    {method of TmyNewClass implementation}
    procedure TmyNewClass.doThis;
    begin
       // do something here
    end;
    
    end;
    Note the difference in the implementation declarations between the class method and the stand alone function.
    You are right - the functions and procedures in my main unit are indeed declared under "TFrom1 = class(TForm)", that is under the class TForm1 (the form I created; that makes sense - everything in this main unit are under this form or class, whereas the stand-alone unit is not under any type). I think I am beginning to understand!

    When you say "class declaration is also terminated by a semi-colon" you are referring to the semi-colon after the "end" under "public" - correct? So this function, if declared and written in the same unit (as in your example), it can be used globally anywhere in the unit? On the other hand, if I create another unit outside of the main unit (what I have been calling a stand-alone unit), this can then be used by any unit as long as I have included the stand-alone unit in the same folder and included the name of the unit under "uses"? Or am I not getting this last part?

    Thanks.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2006
    Location
    Carlsbad, CA
    Posts
    2,057
    Rep Power
    383
    When you say "class declaration is also terminated by a semi-colon" you are referring to the semi-colon after the "end" under "public" - correct?
    Yes.
    So this function, if declared and written in the same unit (as in your example), it can be used globally anywhere in the unit?
    Any stand alone function declared in the interface section can be used in any unit
    that includes this unit in its uses clause.

    Any stand alone function that is implemented in the implementation section without being
    declared in the interface section can only be used within the implementation section
    of this unit AND only by code that follows it.

    Any methods of a class that are declared in the public section of a class can be called (used) by any code that includes the unit containing the class and create an instance of the class
    (there are some exceptions to the need to create rule - don't worry about them yet).

    Any methods of a class that are declared in the protected section of a class can be used only by descendents of the class.

    Any methods of a class that are declared in the private section of a class can be used only by other methods of the class.

    Clive
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    23
    Rep Power
    0
    Originally Posted by clivew
    Yes.


    Any stand alone function declared in the interface section can be used in any unit
    that includes this unit in its uses clause.

    Clive
    Thanks. I got this. I think this simply says that in order to use a stand-alone unit in another unit, it needs to be declared in the uses clause of that unit.
    Originally Posted by clivew

    Any stand alone function that is implemented in the implementation section without being
    declared in the interface section can only be used within the implementation section
    of this unit AND only by code that follows it.
    Clive
    Thanks. I think I got this one too. I believe it means that in order to use a stand-alone function (unit) in another unit, it needs to be declared in its own interface section, apart from the declaration in the uses clause of the unit it is going to be used in, of course. Else, it is a stand-alone function, period, meaning that it cannot be used by any other unit.

    Originally Posted by clivew



    Any methods of a class that are declared in the public section of a class can be called (used) by any code that includes the unit containing the class and create an instance of the class
    (there are some exceptions to the need to create rule - don't worry about them yet).

    Any methods of a class that are declared in the protected section of a class can be used only by descendents of the class.

    Any methods of a class that are declared in the private section of a class can be used only by other methods of the class.

    Clive
    I am still to see a program with methods declared in the public or private sections. Also, I have read that there are protected and public sections for some (most?) classes, but I am not sure I would know what they are and why they are protected. So it is clear that I need to read a lot more! Which is where the lack of a proper intro book is so frustrating. Thanks so much, though. You have been very patient and prompt with your answers.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2006
    Location
    Carlsbad, CA
    Posts
    2,057
    Rep Power
    383
    If you take a look at the the source code for classes declared and implemented in
    Delphi's own units (e.g. classes) you will see plenty of examples of using the
    private, protected and public sections of a class.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    23
    Rep Power
    0
    Originally Posted by clivew
    If you take a look at the the source code for classes declared and implemented in
    Delphi's own units (e.g. classes) you will see plenty of examples of using the
    private, protected and public sections of a class.
    Thanks, Clive. I just printed off the frist few chapters of Zarco Gajic's "Beginner's Guide to Delphi Programming." I will go through that to hopefully get a better idea of Delphi programming than what I have now. It seems fairly basic, so I hope it will be helpful. I will then post questions here if there is something I don't get. Thanks so much for your help. I really appreciate it.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    151
    Rep Power
    3
    For the sake of beginners who might read this thread, I would like to point out obvious errors.

    Originally Posted by clivew
    Any methods of a class that are declared in the public section of a class can be called (used) by any code that includes the unit containing the class and create an instance of the class
    (there are some exceptions to the need to create rule - don't worry about them yet).
    This is not true if you declare the class in implementation section.

    Originally Posted by clivew
    Any methods of a class that are declared in the protected section of a class can be used only by descendents of the class.
    Actually those protected methods still accessible for any codes in the same unit as long as the codes written after the declaration of the said class.

    Protected methods also accessible from other unit using "fake" descendant class and type cast any instance of the class to the fake class.


    Originally Posted by clivew
    Any methods of a class that are declared in the private section of a class can be used only by other methods of the class.

    Clive
    Wrong for the same reason with protected methods I mentioned earlier. Private methods still accessible throughout the same unit where the methods were declared as long as the accessing code written after the methods declaration.

    If you really want private methods to be be accessible only throughout the class, use strict private. Unfortunately not all Delphi version supports strict private.

    Similarly, you can use strict protected to limit your methods.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2006
    Location
    Carlsbad, CA
    Posts
    2,057
    Rep Power
    383
    Hi llaama2012,
    I am sorry that Luthfi seems to have taken as his/her mission to take me to task at every opportunity in an often less than polite manner.

    While these exceptions to the basic rules laid out are valid, in my judgement, it was premature to confuse
    you with them before you had a chance to absorb the basics.

    Just my opinion of course.

    Clive.
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    151
    Rep Power
    3
    Originally Posted by clivew
    Hi llaama2012,
    I am sorry that Luthfi seems to have taken as his/her mission to take me to task at every opportunity in an often less than polite manner.

    While these exceptions to the basic rules laid out are valid, in my judgement, it was premature to confuse
    you with them before you had a chance to absorb the basics.

    Just my opinion of course.

    Clive.
    Which part that made my post was not polite? Why can't you just admit that you have made errors in your posting instead of judging me of being impolite?
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    23
    Rep Power
    0
    Originally Posted by clivew
    Hi llaama2012,
    I am sorry that Luthfi seems to have taken as his/her mission to take me to task at every opportunity in an often less than polite manner.

    While these exceptions to the basic rules laid out are valid, in my judgement, it was premature to confuse
    you with them before you had a chance to absorb the basics.

    Just my opinion of course.

    Clive.
    Clive,

    Perhaps Lufthi's comments are valid. I don't know, for I don't yet understand them. However, I want to thank you for the time and effort you put into making Delphi at least somewhat less intimidating for me. I hope you will continue to help out beginners.

    Thanks again.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo