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 

How do I add a field to an existng component?

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi VCL Components Writing
View previous topic :: View next topic  
Author Message
Robert Rowlands
Guest





PostPosted: Thu Aug 07, 2003 8:43 pm    Post subject: How do I add a field to an existng component? Reply with quote



I am not good on creating new classes.

I am trying to add a new string field to an existing component. I
understand I need to inherit from the component and somehow create the class
(or do I register it?).

I have tried the following but I get an Exception at runtime.

In the Interface section of the form I have put

type
MyAppointment = class(TAppointment)
Protected

Public
MyNewString : string;
end;

And in the Initialization

initialization
RegisterClass(MyAppointment);

I want to be able to reference the new field as follows

MyAppointment.MyNewString := 'MyText';


Could someone be kind enough to point me in the right direction?

Thanks.

Rob.



Back to top
Ignacio Vazquez
Guest





PostPosted: Thu Aug 07, 2003 8:51 pm    Post subject: Re: How do I add a field to an existng component? Reply with quote



"Robert Rowlands" <rob (AT) notathome (DOT) com> wrote in message
[email]3f32ba5e (AT) newsgroups (DOT) borland.com[/email]...
Quote:
MyAppointment.MyNewString := 'MyText';

From the code that you've given, MyAppointment is a class, not a class
instance. You can't assign values to fields on a class, you have to use a
class instance. You can create a class instance from a class by using the
..Create method.

Cheers,
Ignacio

--
No, don't send me e-mail directly. No, just don't.



Back to top
Kurt Barthelmess
Guest





PostPosted: Fri Aug 08, 2003 10:34 am    Post subject: Re: How do I add a field to an existng component? Reply with quote



"Robert Rowlands" <rob (AT) notathome (DOT) com> wrote:

Quote:
I am not good on creating new classes.

You need to understand the difference between a class and what is
called an "instance" of that class. So when you see:

var
Form1: TForm1;

"TForm1" is a class; "Form1" will be an instance of that class.

Quote:
I am trying to add a new string field to an existing component. I
understand I need to inherit from the component and somehow create the class
(or do I register it?).

type
MyAppointment = class(TAppointment)
Protected

Public
MyNewString : string;
end;

That's all you need to do. Whenever an instance of MyAppointment is
created, it will have a field named MyNewString.

Quote:
And in the Initialization

initialization
RegisterClass(MyAppointment);

Unless the new class is going to be streamed or added to the IDE's
palette, it does not need to be registered. If TAppointment was
registered, you'll probably want to register MyAppointment as well.
btw... by convention, new classes are usually prefixed with the letter
"T", as in TMyAppointment.

Quote:
MyAppointment.MyNewString := 'MyText';

That's the equivalent of writing:

TForm1.Width := 99;

which doesn't work obviously. See the distinction between a class and
an instance of a class here? If you had:

var
OneOfMyAppointments: MyAppointment;
....
OneOfMyAppointments.MyNewString := 'M yText';

that would be ok (assuming OneOfMyAppointments has been initialized.)

Hope that helps.

Good luck.

Kurt


Back to top
Robert Rowlands
Guest





PostPosted: Sat Aug 09, 2003 8:17 am    Post subject: Re: How do I add a field to an existng component? Reply with quote

Thank you Kurt.

Should I be able to see all the private/public/published items of the parent
Class in the Object Treeview window?

When my form Unit has focus (where I have the code to create
TMyAppointment), TMyAppointment is shown as a Class but I can only see the
JobNo property and not all of those of the parent, TAppointment.

Thanks

Rob.



"Kurt Barthelmess (TeamB)" <kbarthelmess (AT) compuserve (DOT) com> wrote

Quote:
"Robert Rowlands" <rob (AT) notathome (DOT) com> wrote:

I am not good on creating new classes.

You need to understand the difference between a class and what is
called an "instance" of that class. So when you see:

var
Form1: TForm1;

"TForm1" is a class; "Form1" will be an instance of that class.

I am trying to add a new string field to an existing component. I
understand I need to inherit from the component and somehow create the
class
(or do I register it?).

type
MyAppointment = class(TAppointment)
Protected

Public
MyNewString : string;
end;

That's all you need to do. Whenever an instance of MyAppointment is
created, it will have a field named MyNewString.

And in the Initialization

initialization
RegisterClass(MyAppointment);

Unless the new class is going to be streamed or added to the IDE's
palette, it does not need to be registered. If TAppointment was
registered, you'll probably want to register MyAppointment as well.
btw... by convention, new classes are usually prefixed with the letter
"T", as in TMyAppointment.

MyAppointment.MyNewString := 'MyText';

That's the equivalent of writing:

TForm1.Width := 99;

which doesn't work obviously. See the distinction between a class and
an instance of a class here? If you had:

var
OneOfMyAppointments: MyAppointment;
...
OneOfMyAppointments.MyNewString := 'M yText';

that would be ok (assuming OneOfMyAppointments has been initialized.)

Hope that helps.

Good luck.

Kurt





Back to top
Kurt Barthelmess
Guest





PostPosted: Sat Aug 09, 2003 12:01 pm    Post subject: Re: How do I add a field to an existng component? Reply with quote

"Robert Rowlands" <rob (AT) notathome (DOT) com> wrote:

Quote:
Should I be able to see all the private/public/published items of the parent
Class in the Object Treeview window?

Only the published ones.

Quote:
When my form Unit has focus (where I have the code to create
TMyAppointment), TMyAppointment is shown as a Class but I can only see the
JobNo property and not all of those of the parent, TAppointment.

Either TAppointment has to publish them, or TMyAppointment. As a
descendant, TMyAppointment can "promote" a property of TAppointment to
published status by simply writing:

property MyAncestorsProperty;

in the published section.

Good luck.

Kurt


Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi VCL Components Writing 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.