•
•
•
•
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
![]() |
•
•
Join Date: Nov 2007
Posts: 87
Reputation:
Rep Power: 2
Solved Threads: 1
ok i did the following:
lined out
//function StringReverse(const AString : string): string;
because of the error
then this:
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
lined out
//function StringReverse(const AString : string): string;
because of the error
then this:
delphi Syntax (Toggle Plain Text)
procedure TMainForm.ReverseOrderButtonClick(Sender:TObject); var s : string; begin s := Reversestring(s); Caption := ReverseString( caption ) 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.
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation:
Rep Power: 13
Solved Threads: 193
Ah.
The difference is whether the function is a method of the TMainForm class or not.
A quick tutorial:
Normally you might have a program that looks like this:
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):
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:
And the program that uses it:
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.
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:
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:
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:
The compiler would have complained, saying:
Since the get_element function is a member of tMatrix, we must use its full name.
Well, I hope this explains things better.
The difference is whether the function is a method of the TMainForm class or not.
A quick tutorial:
•
•
•
•
Forward declarations vs. automatic declarations
Pascal Syntax (Toggle Plain Text)
program foo; function triple_it( x: real ): real; begin triple_it := x *3 end; var number: real; begin write( 'Please enter a number to be tripled> ' ); readln( r ); writeln( r, ' tripled is ', triple_it( r ) ) end.
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):
Pascal Syntax (Toggle Plain Text)
program oddness; function is_it_even( n: integer ): boolean; forward; function is_it_odd( n: integer ): boolean; begin if n = 1 then is_it_odd := TRUE else is_it_odd := is_it_even( n -1 ) end; function is_it_even; begin if n = 1 then is_it_even := FALSE else is_it_even := is_it_odd( n -1 ) end; var x: integer; begin write( 'Please enter a whole number greater than zero> ' ); readln( x ); writeln( 'The statement "', x, ' is odd" is ', is_it_odd( x ), '.' ); writeln( 'The statement "', x, ' is even" is ', is_it_even( x ), '.' ) end.
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:
Pascal Syntax (Toggle Plain Text)
unit oddstuff; interface function is_it_even( n: integer ): boolean; function is_it_odd( n: integer ): boolean; implementation function is_it_odd; begin if n = 1 then is_it_odd := TRUE else is_it_odd := is_it_even( n -1 ) end; function is_it_even; begin if n = 1 then is_it_even := FALSE else is_it_even := is_it_odd( n -1 ) end; end.
Pascal Syntax (Toggle Plain Text)
program oddness; uses oddstuff; var x: integer; begin write( 'Please enter a whole number greater than zero> ' ); readln( x ); writeln( 'The statement "', x, ' is odd" is ', is_it_odd( x ), '.' ); writeln( 'The statement "', x, ' is even" is ', is_it_even( x ), '.' ) 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
Pascal Syntax (Toggle Plain Text)
type tMatrix = class private f_rows, f_cols: integer; f_elements: array of array of extended; public constructor create( rows, cols: integer ); function get_element( row, col: integer ): extended; procedure set_element( row, col: integer; value: extended ); end;
So our definitions may be:
Pascal Syntax (Toggle Plain Text)
constructor tMatrix.create( rows, cols: integer ); begin f_rows := rows; f_cols := cols; setLength( f_elements, f_rows, f_cols ) end; function tMatrix.get_element( row, col: integer ): extended; begin result := f_elements[ row, col ] end; procedure tMatrix.set_element( row, col: integer; value: extended ); begin f_elements[ row, col ] := value end;
function get_element( row, col: integer ): extended; begin result := f_elements[ row, col ] end;
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.
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation:
Rep Power: 13
Solved Threads: 193
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:
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:
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:
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:
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.
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:
Pascal Syntax (Toggle Plain Text)
program McGonagalls_notes; type tStudent = record name: string; grade: integer end; var my_student: tStudent; begin with my_student do begin name := 'Hermione Granger'; grade := 100 end end.
my_student.name := 'Hermione Granger'; my_student.grade := 100;
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:
Pascal Syntax (Toggle Plain Text)
program oddness; uses oddstuff; procedure is_it_odd( n: integer ): boolean; begin result := FALSE; end; var x: integer; begin write( 'Please enter a whole number greater than zero> ' ); readln( x ); writeln( 'The statement "', x, ' is odd" is ', is_it_odd( x ), '.' ); writeln( 'The statement "', x, ' is even" is ', is_it_even( x ), '.' ) end.
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.
•
•
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation:
Rep Power: 13
Solved Threads: 193
![]() |
•
•
•
•
•
•
•
•
DaniWeb Pascal and Delphi Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Final Year Project Ideas (Computer Science and Software Design)
- advice needed..please see (ASP.NET)
- Calling a PHP function from HTML menu (PHP)
- Pascal starter. (Pascal and Delphi)
- help with getting soda machine program running (C++)
- help with this prog (C++)
- Urgent help (Networking Hardware Configuration)
Other Threads in the Pascal and Delphi Forum
- Previous Thread: Shorting code lines within one loop
- Next Thread: Problem with dbExpress



Linear Mode