User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Pascal and Delphi section within the Software Development category of DaniWeb, a massive community of 455,965 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,617 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Pascal and Delphi advertiser: Programming Forums
Views: 1605 | Replies: 15 | Solved
Reply
Join Date: Nov 2007
Posts: 87
Reputation: squidd is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
squidd squidd is online now Online
Junior Poster in Training

Re: A few questions to finish up my project

  #11  
Nov 20th, 2007
ok i did the following:

lined out
//function StringReverse(const AString : string): string;
because of the error

then this:

  1. procedure TMainForm.ReverseOrderButtonClick(Sender:TObject);
  2. var
  3. s : string;
  4. begin
  5. s := Reversestring(s);
  6. Caption := ReverseString( caption )
  7. end;

and it literally reversed the entire line on my form header! LOL

Didnt do the listview, just the header.. Which I thought was funny. Why is it doing the reversal to the form header instead of the list I loaded? Funny, but not what I needed.. lol
Last edited by squidd : Nov 20th, 2007 at 8:18 pm.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: A few questions to finish up my project

  #12  
Nov 20th, 2007
Ah.

The difference is whether the function is a method of the TMainForm class or not.

A quick tutorial:

Forward declarations vs. automatic declarations
Normally you might have a program that looks like this:
  1. program foo;
  2.  
  3. function triple_it( x: real ): real;
  4. begin
  5. triple_it := x *3
  6. end;
  7.  
  8. var
  9. number: real;
  10.  
  11. begin
  12. write( 'Please enter a number to be tripled> ' );
  13. readln( r );
  14. writeln( r, ' tripled is ', triple_it( r ) )
  15. end.
In this little program, we have both declared and defined a function named "triple_it". By declaring, we mean that we have told the compiler that it exists. By defining, we mean that we have told the compiler all the details about it. Typically, as in this example, things are declared at the same time they are defined. That is, by defining a thing we implicitly declare it as well.

In Pascal, a thing must be at least declared before it is used.

Sometimes, however, we need to tell a program that something exists before we actually define it. Hence, we need to use a forward declaration. Here's a simplistic example lifted off of the Wikipedia (and repaired for correctness):
  1. program oddness;
  2.  
  3. function is_it_even( n: integer ): boolean; forward;
  4.  
  5. function is_it_odd( n: integer ): boolean;
  6. begin
  7. if n = 1
  8. then is_it_odd := TRUE
  9. else is_it_odd := is_it_even( n -1 )
  10. end;
  11.  
  12. function is_it_even;
  13. begin
  14. if n = 1
  15. then is_it_even := FALSE
  16. else is_it_even := is_it_odd( n -1 )
  17. end;
  18.  
  19. var x: integer;
  20.  
  21. begin
  22. write( 'Please enter a whole number greater than zero> ' );
  23. readln( x );
  24. writeln( 'The statement "', x, ' is odd" is ', is_it_odd( x ), '.' );
  25. writeln( 'The statement "', x, ' is even" is ', is_it_even( x ), '.' )
  26. end.
Notice how we used the function "is_it_even" before we defined it? We told the compiler about it beforehand using the forward keyword. That is, we declared it. Thereafter we only needed to define it somewhere in the program.

This is how a unit works. They have an interface section, wherein things are declared, and an implementation section, wherein things are defined.
(If anyone here is using Extended Pascal and wants to know how this looks in a module, post here and I'll post with the details.) Here's a unit that provides the two functions:
  1. unit oddstuff;
  2.  
  3. interface
  4.  
  5. function is_it_even( n: integer ): boolean;
  6. function is_it_odd( n: integer ): boolean;
  7.  
  8. implementation
  9.  
  10. function is_it_odd;
  11. begin
  12. if n = 1
  13. then is_it_odd := TRUE
  14. else is_it_odd := is_it_even( n -1 )
  15. end;
  16.  
  17. function is_it_even;
  18. begin
  19. if n = 1
  20. then is_it_even := FALSE
  21. else is_it_even := is_it_odd( n -1 )
  22. end;
  23.  
  24. end.
And the program that uses it:
  1. program oddness;
  2. uses oddstuff;
  3. var x: integer;
  4. begin
  5. write( 'Please enter a whole number greater than zero> ' );
  6. readln( x );
  7. writeln( 'The statement "', x, ' is odd" is ', is_it_odd( x ), '.' );
  8. writeln( 'The statement "', x, ' is even" is ', is_it_even( x ), '.' )
  9. end.

One thing to note is that in the definition of the procedures/functions, you only needed to name it. You did not need to specify all the parameters and the like. Typically Borland IDEs and variations will duplicate the procedure/function header with the definition. Both have to match exactly. But it is not strictly necessary to duplicate the declaration with the definition.


Classes
A class is always described in two parts: a declaration part and a definition part. (This separation is necessary, but I'll not go into details as to why here.) The declaration looks like you are used to seeing:
  1. type
  2. tMatrix = class
  3. private
  4. f_rows, f_cols: integer;
  5. f_elements: array of array of extended;
  6. public
  7. constructor create( rows, cols: integer );
  8. function get_element( row, col: integer ): extended;
  9. procedure set_element( row, col: integer; value: extended );
  10. end;
In the definition, you must fully qualify the name of the methods of the class, otherwise the compiler thinks you are talking about some other procedure named (e.g.) "set_element" --not the method of the class.

So our definitions may be:
  1. constructor tMatrix.create( rows, cols: integer );
  2. begin
  3. f_rows := rows;
  4. f_cols := cols;
  5. setLength( f_elements, f_rows, f_cols )
  6. end;
  7.  
  8. function tMatrix.get_element( row, col: integer ): extended;
  9. begin
  10. result := f_elements[ row, col ]
  11. end;
  12.  
  13. procedure tMatrix.set_element( row, col: integer; value: extended );
  14. begin
  15. f_elements[ row, col ] := value
  16. end;
In each instance, I had to prefix the name of the class to the name of the method, so that the forward declaration is satisfied. If I had just said:
function get_element( row, col: integer ): extended;
  begin
  result := f_elements[ row, col ]
  end;
The compiler would have complained, saying: I don't have a function named "get_element". (The only thing I've got so far is a class named "tMatrix".)

Since the get_element function is a member of tMatrix, we must use its full name.

Well, I hope this explains things better.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: A few questions to finish up my project

  #13  
Nov 20th, 2007
Oy, after all that typing you beat me to the punch with another Q.

Second tutorial: scope.

Just as the functions and procedures above all belonged to tMatrix, so do the variables. Remember how you can do something like:
  1. program McGonagalls_notes;
  2. type
  3. tStudent = record
  4. name: string;
  5. grade: integer
  6. end;
  7.  
  8. var
  9. my_student: tStudent;
  10.  
  11. begin
  12. with my_student do
  13. begin
  14. name := 'Hermione Granger';
  15. grade := 100
  16. end
  17. end.
Professor McGonagall remembered that when she is only thinking of one particular student, she doesn't need to remember all the information about where or how the student is referenced --she can just concentrate on that particular student. If she had preferred, she could have just said:
my_student.name := 'Hermione Granger';
my_student.grade := 100;
It is the exact same thing, but written using different scoping rules (by using the with statement).

The scope is what you can see at the moment. In an earlier example I created a unit to return whether a number is odd or even. Now, consider the following:
  1. program oddness;
  2. uses oddstuff;
  3.  
  4. procedure is_it_odd( n: integer ): boolean;
  5. begin
  6. result := FALSE;
  7. end;
  8.  
  9. var x: integer;
  10.  
  11. begin
  12. write( 'Please enter a whole number greater than zero> ' );
  13. readln( x );
  14. writeln( 'The statement "', x, ' is odd" is ', is_it_odd( x ), '.' );
  15. writeln( 'The statement "', x, ' is even" is ', is_it_even( x ), '.' )
  16. end.
Now, the question is, "Which is_it_odd function gets called? The one in the unit or the one that doesn't work?"

The answer is, "Which one is in the current scope?" The one that doesn't work is. To use the one in the unit instead of the bad one, I'd have to specify the correct scope:
writeln( 'The statement "', x, ' is odd"', is oddstuff.is_it_odd( x ), '.' );

The same thing is happening to your caption. In the example I gave you, I was careful to specify that I wanted a specific buttons's caption. In your example, you left it to the current scope. Since the procedure is a method of the TMainForm, the current scope is that of the main form, so when you say "caption" it finds the one in the current scope --and changes the main form's caption.

[EDIT] As an addendum, you should not that "is_it_even" works fine. That's because the scope inside the "is_it_even" function is inside that "oddstuff" unit. So is_it_even calls the correct is_it_odd function. [/EDIT]

Hope this helps.
Last edited by Duoas : Nov 20th, 2007 at 9:27 pm.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: A few questions to finish up my project

  #14  
Nov 21st, 2007
Argh. Missed a key:

[EDIT] As an addendum, you should note that "is_it_even" works fine. That's because the scope inside the "is_it_even" function is inside that "oddstuff" unit. So is_it_even calls the correct is_it_odd function. [/EDIT]
Reply With Quote  
Join Date: Nov 2007
Posts: 87
Reputation: squidd is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
squidd squidd is online now Online
Junior Poster in Training

Re: A few questions to finish up my project

  #15  
Nov 21st, 2007
WOW Duoas! Thank you for all the work you did there! I am reading it, then rereading it.. and them some more...

I will post back when I get this down.

Thanks again!
Reply With Quote  
Join Date: Nov 2007
Posts: 87
Reputation: squidd is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 1
squidd squidd is online now Online
Junior Poster in Training

Re: A few questions to finish up my project

  #16  
Nov 27th, 2007
ok Duoas, I have read that about 10 times now over thanksgiving and whatnot and have a pretty good understanding of that now. Thanks for the help there!

Very good information Duoas. Very well written and laid out for the beginner to understand.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Pascal and Delphi Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Pascal and Delphi Forum

All times are GMT -4. The time now is 9:05 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC