Board index » delphi » OOP isn't so hard

OOP isn't so hard

unit Unit1;

interface

uses
   SysUtils, Types, Classes, Variants, QTypes, QGraphics, QControls,
QForms,
   QDialogs, QStdCtrls;

type
   TForm1 = class(TForm)
     Button1: TButton;
     Label1: TLabel;
     procedure Button1Click(Sender: TObject);
   private
     { Private declarations }
   public
     { Public declarations }
   end;  //class

   TPiddyJob = class
     Troll,Programmer,BurgerFlipper : Boolean;
     JobCount : integer;
     procedure AddOccupations(T,P,B : boolean);
     function NumberOfJobs(Count : integer) : string;
   end;  //class

var
   Form1: TForm1;

implementation

{$R *.xfm}
var
   PiddyJobs : TPiddyJob;

//-------------------------------
procedure TForm1.Button1Click(Sender: TObject);
begin
    PiddyJobs := TPiddyJob.Create;
    with PiddyJobs do begin
      Troll := False;
      Programmer := True;
      BurgerFlipper := false;
      AddOccupations(Troll,Programmer,BurgerFlipper);
      label1.Caption := NumberOfJobs(JobCount);
      PiddyJobs.Free;
    end;
end;
//*******************************
{ TPiddyJob }
//*******************************
procedure TPiddyJob.AddOccupations(T, P, B : boolean);
begin
   with PiddyJobs do begin
     JobCount := 0;
     if Troll = True then Inc(JobCount);
     if Programmer = True then Inc(JobCount);
     if BurgerFlipper = True then Inc(JobCount);
   end;
end;
  //-------------------------------
function TPiddyJob.NumberOfJobs(Count : integer): string;
begin
   Result := IntToStr(JobCount);
end;
//*******************************
{ End TPiddyJob }
//*******************************
end.

{Comment?}

piddy

 

Re:OOP isn't so hard


 procedure TPiddyJob.AddOccupations(T, P, B : boolean);
 begin
    with PiddyJobs do
    begin
      JobCount := 0;
      if T then Inc(JobCount);
      if P then Inc(JobCount);
      if B then Inc(JobCount);
    end;
 end;

Re:OOP isn't so hard


Quote
> //*******************************
> { TPiddyJob }
> //*******************************
> procedure TPiddyJob.AddOccupations(T, P, B : boolean);
> begin
>    with PiddyJobs do begin
>      JobCount := 0;
>      if Troll = True then Inc(JobCount);
>      if Programmer = True then Inc(JobCount);
>      if BurgerFlipper = True then Inc(JobCount);
>    end;
> end;
> {Comment?}

This should probably ?? be:

//*******************************
{ TPiddyJob }
//*******************************
procedure TPiddyJob.AddOccupations(T, P, B : boolean);
begin
   with PiddyJobs do begin
     JobCount := 0;
     if T = True then Inc(JobCount);
     if P = True then Inc(JobCount);
     if B = True then Inc(JobCount);
   end;
end;

Re:OOP isn't so hard


function NumberOfJobs(Count : integer) : string;

Count parameter never used...
NumberOfJobs should be a read-only property with a "get" function . You
don't even need the JobCount variable.

Re:OOP isn't so hard


Quote
"piddy" <piddy...@veggienetcom.net> wrote in message

news:3e6f813a@newsgroups.borland.com...

TJob = (jTroll, jProgrammer, jBurgerFlipper);
TJobs = set of TJob;

TPiddyJob = class
private
   fJobs : TJobs;
   fJobCount : integer;

  procedure SetJobs(Value : TJobs);
public
  property JobCount : integer read fJobCount;
  property Jobs : TJobs read fJobs write SetJobs;
end;

procedure TPiddyJob.SetJobs(Value : TJobs);
var i : TJob;
begin
  fJobCount := 0;
  fJobs := Value;

  for i := Low(TJob) to High(TJob) do
    if (x in Value) then Inc(fJobCount);
end;

MUCH BETTER ;-)

Re:OOP isn't so hard


Quote
"Alessandro Federici" <alef@[nospam]remobjects.com> wrote in message

news:3e6f8765$1@newsgroups.borland.com...

[...]

Quote
> procedure TPiddyJob.SetJobs(Value : TJobs);
> var i : TJob;
> begin
>   fJobCount := 0;
>   fJobs := Value;

>   for i := Low(TJob) to High(TJob) do
>     if (x in Value) then Inc(fJobCount);

Shouldn't that be "if (i in Value) then"?

Quote
> end;

--

 A: Top-posters.
 Q: What is the most annoying thing on Usenet?

Re:OOP isn't so hard


"Frederic Gelinas" <frederic_geli...@hotmail.com> wrote

Quote
> procedure TPiddyJob.AddOccupations(T, P, B : boolean);
>  begin
>     with PiddyJobs do
>     begin
>       JobCount := 0;
>       if T then Inc(JobCount);
>       if P then Inc(JobCount);
>       if B then Inc(JobCount);
>     end;
>  end;

Thank you! Comparing booleans to constants bugs me. Where to stop?

If ((X=False)=True)=False ...

Re:OOP isn't so hard


Quote
"DRS" <d...@removethis.ihug.com.au> wrote in message

news:3e6f889a@newsgroups.borland.com...

Quote
> Shouldn't that be "if (i in Value) then"?

yeah yeah, slipped out during the refactoring <G>

Re:OOP isn't so hard


Quote
Alessandro Federici wrote:
> "piddy" <piddy...@veggienetcom.net> wrote in message
> news:3e6f813a@newsgroups.borland.com...

> TJob = (jTroll, jProgrammer, jBurgerFlipper);
> TJobs = set of TJob;

> TPiddyJob = class
> private
>    fJobs : TJobs;
>    fJobCount : integer;

>   procedure SetJobs(Value : TJobs);
> public
>   property JobCount : integer read fJobCount;
>   property Jobs : TJobs read fJobs write SetJobs;
> end;

> procedure TPiddyJob.SetJobs(Value : TJobs);
> var i : TJob;
> begin
>   fJobCount := 0;
>   fJobs := Value;

>   for i := Low(TJob) to High(TJob) do
>     if (x in Value) then Inc(fJobCount);
> end;

> MUCH BETTER ;-)

It's unreadable. If you want unreadable use C++.

Hope this helps!

piddy

Re:OOP isn't so hard


Quote
"piddy" <piddy...@veggienetcom.net> wrote in message

news:3e6f8e59@newsgroups.borland.com...

Quote
> It's unreadable. If you want unreadable use C++.

Ask yourself a few questions before making comments like this:

1) How much code would your approach require when you need to add a job?
2) How can you dynamically list all the available jobs in a combo box?
3) How much code would you need to select 3 persons (out of an array) that
only have 2 of the 3 jobs set?
4) How much time did you spend looking at the VCL code which uses code that
is virtually identical to what I used
in my sample?

Hope this helps! <BG>

Re:OOP isn't so hard


Quote
> It's unreadable.

Only for someone that doesn't know OOP.
It is a much better design like this!
Go to page: [1] [2] [3] [4] [5] [6] [7] [8]

Other Threads