Page 1 of 1

My First Experience with object oriented programming

PostPosted: June 16th, 2007, 6:16 pm
by Wardlow
I hear that OOP can clean up your code. Since it seems like I'm passing at times to functions 15-20 parameters , I said heck with it, I'll try it out.

My only ? is when I'm passing the object type, the function doesn't understand what the "type" is for example

MAIN Program
Code: Select all
program experimentwobject;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  OptConsts in 'OptConsts.pas',
  Unit2 in 'Unit2.pas';

var
MyFirst: OptTerms;

begin
Myfirst := OptTerms.Create;
MyFirst.ShowInfo;
WriteSomething(MyFirst);
readln;
MyFirst.Free;

end.


OptConsts (My class)
Code: Select all
unit OptConsts;

interface

type
OptTerms = class
 const COVERAGE: integer = 10;
 const COVERAGE2: integer = 20;
 const COVERAGE3: integer = 30;
 procedure ShowInfo;
end;

implementation

Procedure OptTerms.ShowInfo;
begin
  writeln(OptTerms.COVERAGE);
  writeln(OptTerms.COVERAGE2);
  writeln(OptTerms.COVERAGE3);
end;

end.


Unit2 (This is where i'm trying to pass a type OptTerms Paramter, and the function doesn't understand what a OptTerms Type is
Code: Select all
unit Unit2;

interface

procedure WriteSomething (x: OptTerm);

implementation
procedure WriteSomething (x: OptTerm);
begin
  writeln(x.COVERAGE);
end;


end.


So my ? is, in order to use the new object type can they only be used in the main program (where they are definied) and only the Unit/class in which they are defined. If so, that leaves me little use for OOP because I would like to pass a type OptTerms to many other fucntions, only problem is those functions don't understand what an OptTerms type is.

Thanks

PostPosted: June 18th, 2007, 10:22 am
by Kambiz
You must include OptConsts in the uses clause of Unit2.

Anyway, members OptTerms are defined as private, therefore you don't have access to them in another units.

If you are really going to learn programming in Delphi, I do suggest to invest some money and buy a good book.

PostPosted: June 18th, 2007, 9:38 pm
by Johnny_Bit
I'd say that OOP won't clean-up your code. Your efforts will be futile and the code will be miserable. As for now of course.
Now, to do something that you want to achieve you need to know just how do you want to deal with this whole OOP.

It's neither easy nor clear to learn. you'll need a book. A dozen books. And a practice. A lot of it.

You know why? Because OOP is way different than structural programming.

In a telegraphic short:
OOP is great when you use it, just use it as a programmer. When you need to create something in OOP, that's where the fun starts and hard part begnis.

PostPosted: June 21st, 2007, 11:56 pm
by Wardlow
thank you for the advice. I think I'm going to stick with structural programming now. I didn't realize it was quite intensive