BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Newbie question: Writing your own class

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> comp.lang.pascal.delphi.misc
View previous topic :: View next topic  
Author Message
Morten Taarland
Guest





PostPosted: Mon Jan 16, 2006 12:35 am    Post subject: Newbie question: Writing your own class Reply with quote



Hi, I am trying to get into delphi7, haven't used any kind of pascal for
years. This may be stupid question, but I am struggling, so any help would
be appreciated.

I want to create my own class.
In a unit I do

TTest = class
private
Definition: String;

public
procedure Set_Definition(NewDef: String);
end;

var
myTest: TTest; (actually, this is declared and called from another unit,
but just to show there is an instance of the class created)

implementation

procedure TTest.Set_Definition(NewDef: String);
begin
Definition:=NewDef;
end;


To me that is a simple example of encapsulation of the class property
Definition which is not accessible outside the object. To change its value,
call Set_Definition.
However when I do this, I get an EAccessViolation, and it happens when I try
to assign a value to Definition. Comment it out and it runs fine.

So what am I doing wrong already? I didn't even get started yet, hehe.
Appreciate your attention, ladies and gents.

Morten


Back to top
Rob Kennedy
Guest





PostPosted: Mon Jan 16, 2006 2:54 am    Post subject: Re: Newbie question: Writing your own class Reply with quote



Morten Taarland wrote:
Quote:
In a unit I do

TTest = class
private
Definition: String;

public
procedure Set_Definition(NewDef: String);
end;

var
myTest: TTest; (actually, this is declared and called from another unit,
but just to show there is an instance of the class created)

implementation

procedure TTest.Set_Definition(NewDef: String);
begin
Definition:=NewDef;
end;


To me that is a simple example of encapsulation of the class property
Definition which is not accessible outside the object. To change its value,
call Set_Definition.

In the code above, "Definition" is not a property. It is a field.
Properties are declared with the keyword "property."

Quote:
However when I do this, I get an EAccessViolation, and it happens when I try
to assign a value to Definition. Comment it out and it runs fine.

Comment what out? You haven't shown what it is you were trying to do
when you got the exception. I can make a *guess* at what you were doing,
but it's better if you don't leave things like that to chance.

You need to create an instance of TTest, just as you would create an
instance of TStringList or TForm1.

Before you get too deep into writing your own classes, you should be at
least a little comfortable with _using_ classes.

--
Rob

Back to top
Jamie
Guest





PostPosted: Mon Jan 16, 2006 4:41 am    Post subject: Re: Newbie question: Writing your own class Reply with quote



Morten Taarland wrote:

Quote:
Hi, I am trying to get into delphi7, haven't used any kind of pascal for
years. This may be stupid question, but I am struggling, so any help would
be appreciated.

I want to create my own class.
In a unit I do

TTest = class
private
Definition: String;

public
procedure Set_Definition(NewDef: String);
end;

var
myTest: TTest; (actually, this is declared and called from another unit,
but just to show there is an instance of the class created)

implementation

procedure TTest.Set_Definition(NewDef: String);
begin
Definition:=NewDef;
end;


To me that is a simple example of encapsulation of the class property
Definition which is not accessible outside the object. To change its value,
call Set_Definition.
However when I do this, I get an EAccessViolation, and it happens when I try
to assign a value to Definition. Comment it out and it runs fine.

So what am I doing wrong already? I didn't even get started yet, hehe.
Appreciate your attention, ladies and gents.

Morten


there are 2 things.

#1.
you must dynamically create objects/Classes on the fly, they are
not static.
for example.
Var
myClassInstance :TTest;
Begin
myClassInstance := Ttest.create;
// now we can set the string.
myClassInstance.SetDefinition(Some old farty string some where);
// that's it..
// P.S.
// you must free this class at some point , especially before you
leave a procedure/function block if the new object isn't going to be
past to some other code for handling.
------
But! the proper way that you should be doing this if Properties is
what you want (because all your doing is calling a member method there).

in your public section of your class you do this.

Property Def:String read definition write Set_Definition;

this simply directs the compiler to generate code in the background
to do what your doing manually.

so in the class after you create the instance you do this.
myClassINstance.Def := 'Some new string value';
or
SomeStringSomewhere := MyClassInstance.Def;


etc
--
Real Programmers Do things like this.
http://webpages.charter.net/jamie_5


Back to top
alanglloyd@aol.com
Guest





PostPosted: Mon Jan 16, 2006 8:04 am    Post subject: Re: Newbie question: Writing your own class Reply with quote

I'm not sure where you're coming from (what languages) but some basic
concepts of Delphi OO construction and use are ...

A class is a template for a actual instance of an object. The instance
is made by a class's Create constructor method which allocates memory
for that object. The instance's memory is freed by a Destroy destructor
method, which is not called externally. That is done by a Free method
(called externally) which checks for non-nullity of the instance before
calling Destroy.

Classes "descend" from other classes, and the single original
pre-historic ancestor of all classes is TObject (note that " = class;"
is the same as " = class(TObject)").

Classes have methods (procs & functions), properties and fields.

A single chunk of executable code is provided for all instances of an
object (a reference to the instance is provided by a hidden parameter
named "Self"). Properties have separate storage for every instance.
Properties are accessed either directly of via an "accessor method".
Field are variable internal to the object.

So your class should/could be ...

TTest = class(TObject)
private
FSomeDef : string;
FDefinition : string;
function GetDefinition : string; // accessor get method
procedure SetDefinition(AValue : string); // accessor set method
public
property Definition : string read GetDefinition write
SetDefinition; // access by accessor method
property SomeDef : string read FSomeDef write FSomeDef; // direct
access
end;

constructor TTest.Create;
begin
inherited Create; // not necessary for Tobjec descendants but for
every other descendant of TObject
end;

function TTest.GetDefinition : string;
begin
Result := FDefinition;
end;

procedure TTest.SetDefinition(AValue : string);
begin
if AValue <> FDefinition then
FDefinition := AValue;
end;

var
MyTest : TTest; // variable to hold an instance reference

procedure TForm1.Button1Click(Sender : TObject); // some initiating
event handler
begin
MyTest := TTest.Create;
MyTest.Definition := 'MyDefinition';
MyTest.SomeDef := 'Some Def';
end;

procedure TForm1.Button2Click(Sender : TObject); // some other
displaying event handler
begin
ShowMessage('Definition : ' + MyTest.Definition + #13 +
'SomeDef : ' + MyTest.SomeDef);
end;

procedure TForm1.FormClose(Sender : TObject);
begin
MyTest.Free; // free the instance you have created - a TObject (your
class' parent) has a Free method
end;

The form you are doing this test on is assumed to be Form1. "Get",
"Set", "F", and "T" are prefixes used by convention in Delphi.

You will see that the set & get methods or the "F"-prefixed field
variables are used to hide direct access to the property. The "private"
and "public" keywords are used by the compiler to limit access to the
fields and methods (hiding access to elements of the object are
paradigms of OO).

Enjoy <g>.

Alan Lloyd

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> comp.lang.pascal.delphi.misc All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.