My First Experience with object oriented programming

Please discuss general Delphi programming topics here.

My First Experience with object oriented programming

Postby Wardlow » June 16th, 2007, 6:16 pm

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
Wardlow
Junior Member
Junior Member
 
Posts: 38
Joined: May 31st, 2007, 10:08 pm

Postby Kambiz » June 18th, 2007, 10:22 am

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.
Kambiz
User avatar
Kambiz
Administrator
Administrator
 
Posts: 2421
Joined: March 7th, 2003, 7:10 pm

Postby Johnny_Bit » June 18th, 2007, 9:38 pm

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.
Thou shalt write the code, not connect the bricks.
Johnny_Bit
VIP Member
VIP Member
 
Posts: 455
Joined: June 15th, 2003, 9:56 am

Postby Wardlow » June 21st, 2007, 11:56 pm

thank you for the advice. I think I'm going to stick with structural programming now. I didn't realize it was quite intensive
Wardlow
Junior Member
Junior Member
 
Posts: 38
Joined: May 31st, 2007, 10:08 pm


Return to Delphi Programming

Who is online

Users browsing this forum: No registered users and 1 guest

cron