Solved

TStrings property in custom component

Posted on 1998-03-08
8
584 Views
Last Modified: 2010-04-06
Hi

I am writing my first visual component, and struck against a problem with creating and using a property of type TStrings. Below are the relevant excerpts from my code.

type
  TMyMenu = class(TCustomControl)
  private
    FItems : TStrings;
    procedure SetItems(AItem : TStrings);

  published
    property Items : TStrings read FItems write SetItems;

implementation

procedure TMyMenu.SetItems(AItem : TStrings);
begin
     FItems := AItem;
end;


(I also found, quite by accident, a mention of array properties in the help files - but I couldn't get that to work either :(     )

I would appreciate any help with this, maybe an example or links to examples. Even better, a tutorial or how-to guide.

Thanks
0
Comment
Question by:sderaadt
8 Comments
 
LVL 5

Expert Comment

by:inter
ID: 1360287
Hi

If the problem is just using TStirngs, here are the guidlines, but if you are problem with deriving custom control please indicate what exactly you want:

1 - TStrings has just a wrapper to the strings allocated as TStringList. So it has no storage abitliy. But it is assignement compatible with TStringList which can store the strings.
2 - So you can declare your components private variable as FStrings : TStrings but to be able to store strings in it you should do
   FStrings := TStringList.Create;
Actually, if you have the VCL source code just look at the TListBox implementation and see how it stores the list of items in its TStrings field.
3 - The following example you gave is problematic.
    procedure TMyMenu.SetItems(AItem : TStrings);
    begin
         FItems := AItem;
    end;
If you do such thing you loose some memory since the items you have already created are not disposed off from the memory. Instead, the components and classes that store something has a method called Assign(..): The correct code for it is as follows:

    procedure TMyMenu.SetItems(AItem : TStrings);
    begin
         FItems.Assign(AItem);
    end;

4 - When  creating  a new component ALWAYS override the constructor and destructor even if you do not do anything in them

May these help
Regards,
Igor
0
 
LVL 5

Expert Comment

by:ronit051397
ID: 1360288
Igor I right. Use TStringList instead of TStrings.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1360289
Well, nothing to be said anymore.... Everything is said. And now I've said my part too... C.U.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:sderaadt
ID: 1360290
Thankyou inter

I've solved the problem, thanks to your tips.

You need to answer the question so you can get the points - or shall I delete the question?
0
 
LVL 5

Expert Comment

by:inter
ID: 1360291
Dear sderaadt,

If you lack of points, you may delete the question. No problem for me just notify to do so...

Sincerely,
Igor
0
 

Author Comment

by:sderaadt
ID: 1360292
inter

I have enough points and you deserve them, since you solved the problem :)

sderaadt
0
 
LVL 5

Accepted Solution

by:
inter earned 200 total points
ID: 1360293
Dear sderaadt,

Sorry for misinterpreting your comment before


Thanks
Igor

0
 

Author Comment

by:sderaadt
ID: 1360294
inter

Thats alright :)

Thanks for the tips.

sderaadt
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question