Forum > General

How do I define or make a Class??

(1/3) > >>

wpflum:
I'm trying to get my head wrapped around making a class object that has properties and functions and most of what I see for example seems to be going right over my head, smacking me on the point as it goes over  :)

I figured if I can give a simple example of what I'm looking for maybe someone can point me in the right direction by showing how it should be done.

Lets say I have 3 variables, Name, Type and Date and I want to have them in an object/class that will also allow me to validate the Type and Date.  How would I go about setting something like that up??

I can see how to use them, something like ' Test.Name := 'xxxxxx' '  to store name and so on and then some thing like 'if Test.CheckType then Showmessage('Good type') '

The Types could just be in an array line type[1] := 'Apple', Type[2] := 'Orange'  for the example but eventually I'll be using a database to hold the data to check against.

I have a general Idea of how an object works but I'm just not sure the correct way to actually MAKE one.  :-[

Bill

jagorath:
Welcome to Pascal wpflum, this is an example for defining and creating an object like the one you describe, hope it helps.

type
  TSomeType = (stApple, stOrange);

TSomeClass = class
private
  FName: string;
  DateCreated: TDateTime;
  ObjType: TSomeType;
  procedure SetName(Value: string);
public
  property Name: string read FName write SetName;
  property Created: TDateTime read FDateCreated;
  property ObjType: TSomeType read FObjType write FObjType;
  constructor Create;
end;

const
  TSomeTypeNames: array[TSomeType] of string = ('Apple', 'Orange');

implementation

procedure TSomeClass.SetName(Value: string);
begin
  if Value <> EmptyStr then
   FName := Value
else
  raise Exception.Create('Name cannot be blank');
end;

procedure TSomeClass.Create;
begin
  FName := EmptyStr;
  FDateCreated := Now;
  FObjType := stApple;
end;

end.

{ Somewhere else in project }
var
  MyFruit: TSomeClass;
begin
  MyFruit := TSomeClass.Create;
  try
    Apple.Name := 'My Orange';
    Apple.Type := stOrange;
    ShowMessage(TSomeTypeNames[Apple.ObjType]);
  finally
    MyFruit.Free;
  end;
end;

mas steindorff:
besides jagorath's nice example, here is a link for "class" basics for delphi.  almost all of the basic code for delphi works in FP.

http://www.delphibasics.co.uk/RTL.asp?Name=Class

wpflum:

--- Quote from: jagorath on September 07, 2010, 09:19:34 pm ---Welcome to Pascal wpflum, this is an example for defining and creating an object like the one you describe, hope it helps.

type
  TSomeType = (stApple, stOrange);

TSomeClass = class
private
  FName: string;
  DateCreated: TDateTime;
  ObjType: TSomeType;
  procedure SetName(Value: string);
public
  property Name: string read FName write SetName;
  property Created: TDateTime read FDateCreated;
  property ObjType: TSomeType read FObjType write FObjType;
  constructor Create;
end;

const
  TSomeTypeNames: array[TSomeType] of string = ('Apple', 'Orange');

implementation

procedure TSomeClass.SetName(Value: string);
begin
  if Value <> EmptyStr then
   FName := Value
else
  raise Exception.Create('Name cannot be blank');
end;

procedure TSomeClass.Create;
begin
  FName := EmptyStr;
  FDateCreated := Now;
  FObjType := stApple;
end;

end.

{ Somewhere else in project }
var
  MyFruit: TSomeClass;
begin
  MyFruit := TSomeClass.Create;
  try
    Apple.Name := 'My Orange';
    Apple.Type := stOrange;
    ShowMessage(TSomeTypeNames[Apple.ObjType]);
  finally
    MyFruit.Free;
  end;
end;


--- End quote ---

Let me go step by step to ask questions.

Ok, here I'm going to show how much of a newbie I am to Pascal, I haven't yet used a 'Type' procedure, what exactly does the line 'TSomeType = (stApple, stOrange);'  do??

The statements

private
  FName: string;
  DateCreated: TDateTime;
  ObjType: TSomeType;
  procedure SetName(Value: string);

are all contained by the class and do not 'leak' out into the real world, correct?

The statements
public
  property Name: string read FName write SetName;
  property Created: TDateTime read FDateCreated;
  property ObjType: TSomeType read FObjType write FObjType;
  constructor Create;

are what the public sees of the class meaning myclass.xxxxxxx where xxxxxxx would be Name, Created,ObjType, Create, Correct??

The Create function is there to initialize the object, correct??

Is a the 'Constructor Create;'  used when the item is a function and not a property or is that something special for the 'Create' name??

Bear with me as the past couple of days I've been having Sinus problems which means lack of quality sleep which equals a head stuffed with cotton balls.  I tried a few times over the last day or so to formulate my questions but ended up staring at a blank screen with glazed over eyes.  :(

I'm trying to take advantage of the power of Pascal instead of just writing around stuff I don't 'get' using simpler but much more of code to get to the needed ends.  This time I'd like to get a better grip on the 'fancier stuff'   ;D

wpflum:
Another question popped up when I looked at the code to use the class,
'var
  MyFruit: TSomeClass;
begin
  MyFruit := TSomeClass.Create;'

I can see where you need to set the MyFruit to be a TSomeClass but why do you have to set it to 'MyFruit := TSomeClass.Create;' , since it is now a TSomeClass can't you just do something like 'MyFruit.Create;' ?????

Navigation

[0] Message Index

[#] Next page

Go to full version