## My First Experience with object oriented programming

Please discuss general Delphi programming topics here.

### My First Experience with object oriented programming

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';varMyFirst: OptTerms;beginMyfirst := OptTerms.Create;MyFirst.ShowInfo;WriteSomething(MyFirst);readln;MyFirst.Free;end.

OptConsts (My class)
Code: Select all
unit OptConsts;interfacetypeOptTerms = class const COVERAGE: integer = 10; const COVERAGE2: integer = 20; const COVERAGE3: integer = 30; procedure ShowInfo;end;implementationProcedure 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;interfaceprocedure WriteSomething (x: OptTerm);implementationprocedure 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

Posts: 38
Joined: May 31st, 2007, 10:08 pm

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

Kambiz

Posts: 2428
Joined: March 7th, 2003, 7:10 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

Posts: 455
Joined: June 15th, 2003, 9:56 am

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

Posts: 38
Joined: May 31st, 2007, 10:08 pm