Recent

Author Topic: How do I define or make a Class??  (Read 11426 times)

wpflum

  • Sr. Member
  • ****
  • Posts: 287
How do I define or make a Class??
« on: September 07, 2010, 08:10:44 pm »
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

  • New Member
  • *
  • Posts: 34
Re: How do I define or make a Class??
« Reply #1 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;

mas steindorff

  • Hero Member
  • *****
  • Posts: 560
Re: How do I define or make a Class??
« Reply #2 on: September 07, 2010, 09:39:30 pm »
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
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

wpflum

  • Sr. Member
  • ****
  • Posts: 287
Re: How do I define or make a Class??
« Reply #3 on: September 09, 2010, 03:38:07 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;


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

  • Sr. Member
  • ****
  • Posts: 287
Re: How do I define or make a Class??
« Reply #4 on: September 09, 2010, 03:44:19 pm »
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;' ?????

mas steindorff

  • Hero Member
  • *****
  • Posts: 560
Re: How do I define or make a Class??
« Reply #5 on: September 10, 2010, 01:12:28 am »
Quote
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??
this creates a varable type that can only store these two values.  this forces you to pick from the list.

Another question popped up when I looked at the code to use the class,
...
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;' ?????

All class should have a .create call that will allocate the object's memory, set it's default values, and or create child objects as needed.  they also need to be .free when you are done with them.  

The Tforms and Tedits you've been using also have these calls but they are call automatically at program start and end.
« Last Edit: September 10, 2010, 01:19:48 am by mas steindorff »
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

mas steindorff

  • Hero Member
  • *****
  • Posts: 560
Re: How do I define or make a Class??
« Reply #6 on: September 10, 2010, 01:30:03 am »
Quote
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??

some guidelines.  a simple variable can just be define with the var keyword and it is done.  these include integers, records, and the older "TObject"s that you should use Classes for nowadays (now that I said it, forget this use of the tobject key word and assume we are talking about classes from now on. >:D It's use as a key word has confused a lot of people).  A class may contain other objects (classes) that you don't know about or it may change how it functions depending on how it is created so always call .create if there is one.
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

eny

  • Hero Member
  • *****
  • Posts: 1648
Re: How do I define or make a Class??
« Reply #7 on: September 10, 2010, 12:13:32 pm »
The Wiki is a good place to start...
All posts based on: Win10 (Win64); Lazarus 3_4  (x64) 25-05-2024 (unless specified otherwise...)

wpflum

  • Sr. Member
  • ****
  • Posts: 287
Re: How do I define or make a Class??
« Reply #8 on: September 10, 2010, 02:59:54 pm »
Quote
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??

some guidelines.  a simple variable can just be define with the var keyword and it is done.  these include integers, records, and the older "TObject"s that you should use Classes for nowadays (now that I said it, forget this use of the tobject key word and assume we are talking about classes from now on. >:D It's use as a key word has confused a lot of people).  A class may contain other objects (classes) that you don't know about or it may change how it functions depending on how it is created so always call .create if there is one.



I haven't used a constructor before, or yet for that matter, and didn't know what is was being used for, I had thought after a bit of googling that is was for some kind of records thingy  :)


A class within a class within a class within a class.....Ow my brain hurts.  %)

I understand the reason for the Create function I just don't understand why you have to set the class equal to it instead of just calling the Create function from the class itself.  I did try it and got a runtime error so I guess it doesn't work like that but I don't understand WHY it doesn't work. :-[


JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4650
  • I like bugs.
Re: How do I define or make a Class??
« Reply #9 on: September 10, 2010, 04:48:50 pm »
I understand the reason for the Create function I just don't understand why you have to set the class equal to it instead of just calling the Create function from the class itself.  I did try it and got a runtime error so I guess it doesn't work like that but I don't understand WHY it doesn't work. :-[

It assigns a valid object instance to the variable. The constructor has allocated memory and everything for it.
Before the assignment the variable contains Nil or garbage, and you can't call any function through it.

Juha
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

wpflum

  • Sr. Member
  • ****
  • Posts: 287
Re: How do I define or make a Class??
« Reply #10 on: September 10, 2010, 05:05:44 pm »
Ok, that makes sense, I think, I was assuming that when you declared it with the MyStuff:MyClass;  line that at that point the variable MyStuff would be a copy of MyClass.  What you seem to be saying is that all that does is give some general references to the MyStuff object but doesn't actually assign it the info from the class itself.  Does that mean if you assign it like this,

Var
MyStuff:Tobject;
Begin
MyStuff := Myclass.Create;
.....

and as long as the Myclass was a descendant of a TObject that this should work??


 

mas steindorff

  • Hero Member
  • *****
  • Posts: 560
Re: How do I define or make a Class??
« Reply #11 on: September 10, 2010, 06:48:29 pm »

Does that mean if you assign it like this,

Var
MyStuff:TXobject;
Begin
MyStuff := Myclass.Create;
.....

and as long as the Myclass was a descendant of a TXObject that this should work??
the problem with this is what if Myclass has a new variable or a function that tXobject does not?  would you think you can call MyStuff.Newfun if it has been defined as a TXObject?
 (try it and find out)[/color]
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

 

TinyPortal © 2005-2018