<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<!--
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/
*
* Copyright (C) 2012, Peter Johnson (www.delphidabbler.com).
*
* $Rev$
* $Date$
*
* Help topic describing class and advanced record snippet kind.
-->
<html>
<head>
<meta http-equiv="Content-Type" content="text-html; charset=Windows-1252">
<title>
Class and Advanced Record Snippets
</title>
<link rel="stylesheet" href="../css/codesnip.css" type="text/css">
</head>
<body>
<object
type="application/x-oleobject"
classid="clsid:1e2a7bd0-dab9-11d0-b93a-00c04fc99f9e"
>
<param name="ALink Name" value="SnippetClass">
</object>
<h1>
<a name="snippet_class"></a>Class & Advanced Record Snippets
</h1>
<p>
A class (or advanced record) snippet, defines a <em>single</em> Pascal
class or advanced record (i.e. record with methods).
</p>
<p>
The source code must conform to the following rules:
</p>
<ul class="spaced">
<li>
Each snippet must begin with the <strong>type</strong> keyword as the
first non-white-space text in the source code. Comments should not
preceed it. Ideally the <strong>type</strong> keyword should be on a
line on its own.
</li>
<li>
Next follow all the class declarations. Addition <strong>type</strong>
keywords may be used.
</li>
<li>
Method implementations follow once <em>all</em> the class / record types
have been declared. Note that an implementation must be provided for all
classes and record types.
</li>
<li>
The <strong>implementation</strong> keyword must not be used to separate
the declaration and the implementation: <em>CodeSnip</em> works out
where the implementation starts.
</li>
</ul>
<p>
<strong class="warning">Warning:</strong> It is an error to use this
snippet kind for records or classes that have no methods – use <a
href="snippet_type.htm"
>simple type definition</a> instead. It is also an error to define more
than one class per snippet.
</p>
<p>
Here are examples valid class and advanced record snippets:
</p>
<p>
Example 1
</p>
<pre class="source-code">type
TMyClass = class(TObject)
private
fField: string;
public
constructor Create(AFoo: string);
function Foo: string;
end;
constructor TMyClass.Create(AFoo: string);
begin
inherited Create;
fField := AFoo;
end;
function TMyClass.Foo: string;
begin
Result := fField;
end;
</pre>
<p>
Example 2
</p>
<pre class="source-code">
type
TPointEx = record
X, Y: Integer;
constructor Create(AX, AY: Integer);
end;
constructor TPointEx.Create(AX, AY: Integer);
begin
X := AX;
Y := AY;
end;</pre>
<p>
Class and Advanced Record snippets may refer to other classes or advanced
records, <a href="snippet_routine.htm">routines</a>,
<a href="snippet_type.htm">simple type definitions</a> or
<a href="snippet_constant.htm">constants</a>, providing they are defined
in Delphi units or elsewhere in the database. The snippet's units and
dependency references should indicate where to find referenced types,
constants and routines. <a href="snippet_freeform.htm">Freeform</a>
and <a href="snippet_unit.htm">unit</a> snippets may not be referenced.
</p>
<p>
It is important that <em>CodeSnip</em> can understand the code because it
must be able to separate the declaration and implementation parts when
generating a unit containing the snippet.
</p>
</body>
</html>