IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
Prism
Search Prism Group:

Advanced
-Collapse +Expand Prism To/From
To/FromCODEGuides
-Collapse +Expand Prism Study Test
PRESTWOODCERTIFIED
-Collapse +Expand Prism Store
PRESTWOODSTORE

Prestwood eMagazine

May Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingDelphi PrismLanguage Basics   Print This     
  From the October 2015 Issue of Prestwood eMag
 
Prism Language Basics:
Delphi Prism Custom Routines (method, procedure, function)
 
Posted 11 years ago on 11/28/2008 and updated 2/22/2009
Prism Code Snippet:
 A flashcard from our Prism Flashcards Library
 A code snippet from our Prism Code Snippets Page

KB101661



Languages Focus: Custom Routines

For non-OOP languages, a custom routine is a function, procedure, or subroutine and for pure OOP languages, a custom routine is a class method. Hybrid languages (both non-OOP and OOP) combine both.

Delphi Prism Custom Routines

In Prism, everything is within a class (just like with C#, VB.Net, and Java). So you create class methods using the method keyword. Alternatively, you can use procedure or function if you want the compiler to enforce returning or not returning a value.

[function/procedure] RoutineName : ReturnType; 

As with C++, your custom routine must come before it's first usage or you have to prototype it in the Interface section.

Syntax Example:
method Form1.IntroduceYourself;
begin
  MessageBox.Show("Hello, I do not have a name yet.");
end;

procedure Form1.SayHello(pName: String);
begin
  MessageBox.Show("Hello " + pName);
end;
 
function Form1.Add(p1, p2 : Double): Double;
begin
  Result := p1 + p2;
end;

Old School: Delphi for .Net Step-by-Step Example

In this old-school Delphi for .Net example (before Delphi Prism), notice that you can use global functions and procedures. This was changed with Prism where everything is within a class.

1. Add SysUtils to the uses clause so we can use the VCL.Net IntToStr command.

interface
uses
  System.Drawing, System.Collections, System.ComponentModel,
  System.Windows.Forms, System.Data, Borland.Vcl.SysUtils;

2. Also in the interface section, prototype our two custom routines.

type
  //...default form class code here.
procedure SayHello(pName: String);
function Add(a, b : Integer) : integer;

3. Add the two custom routines to the implementation section.

implementation
 
//...Delphi generated code here.

procedure SayHello(pName: String);
begin
  MessageBox.Show('Hello ' + pName);
end;
 
function Add(a, b : Integer) : integer;
begin
  result := a + b;
end;

end.

4. Call the two custom routines from a button click event:

procedure TWinForm.Button2_Click(sender: System.Object; e: System.EventArgs);
begin
  SayHello('Mike');
  MessageBox.Show(IntToStr(Add(3, 4)));
end;

More Info


Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1202A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #101661 Counter
19394
Since 11/28/2008
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2019 Prestwood IT Solutions.   [Security & Privacy]