Groups > Borland > Delphi Object Oriented design > Re: TCollection problem




TCollection problem

TCollection problem
5 Feb 2008 05:34:42 -0700
Hello all

This is my test code:

var
  Languages: TLanguages;
begin
  Languages := TLanguages.Create(TLanguage);
  Languages.AddWithValues(0,'EN','English','English');
  Languages.AddWithValues(1,'NL','Dutch','Dutch');
  Languages.AddWithValues(2,'DE','German','German');
  Languages.AddWithValues(9999,'AL','All','All');
end;

Problem: The first param of AddWithValues does not get assigned 
internally.
When tracing the code in the TCollection/TCollectionItem definition 
below I do not see FLangID getting a value at point (* *)

unit uLanguages;

interface

Uses
  Classes;

type
  TLanguage = class(TCollectionItem)
  private
    FLangID      : Word;
    FLangSID,
    FLangName,
    FEngAdjective: ShortString;
    FForCurrUser : Boolean;
    procedure SetLangID(Value: Word);
    procedure SetLangSID(Value: ShortString);
    procedure SetLangName(Value: ShortString);
    procedure SetEngAdjective(Value: ShortString);
    procedure SetForCurrUser(Value: Boolean);
  published
    property LangID      : Word        read FLangID       write 
SetLangID;
    property LangSID     : ShortString read FLangSID      write 
SetLangSID;
    property LangName    : ShortString read FLangName     write 
SetLangName;
    property EngAdjective: ShortString read FEngAdjective write 
SetEngAdjective;
    property ForCurrUser : Boolean     read FForCurrUser  write 
SetForCurrUser;
  end;

type
  TLanguages = class(TCollection)
  private
    function  GetItem(Index: Integer): TLanguage;
  protected
  public
    function Add: TLanguage;
    function AddWithValues(LngID: Word; LngSID,LngName,EngAdj: 
ShortString): TLanguage;
    property Items[ID: Integer]: TLanguage read GetItem; default; { 
'default' allows us to access the array directoy, without .Items }
  end;


implementation



Uses
  SysUtils;

procedure TLanguage.SetEngAdjective(Value: ShortString);
begin
  FEngAdjective := Value;
end;

procedure TLanguage.SetForCurrUser(Value: Boolean);
begin
  FForCurrUser := Value;
end;

procedure TLanguage.SetLangID(Value: Word);
begin
  FLangID := LangID;
end;

procedure TLanguage.SetLangName(Value: ShortString);
begin
  FLangname := Value;
end;

procedure TLanguage.SetLangSID(Value: ShortString);
begin
  Assert(Length(Value)=2,'SetLangSID - Language SID is not 2 characters 
long: ' + Value);
  FLangSID := Value;
end;



function TLanguages.Add: TLanguage;
begin
  result := inherited Add as TLanguage;
end;

function TLanguages.AddWithValues(LngID: Word; LngSID, LngName, EngAdj: 
ShortString): TLanguage;
begin
  result := inherited Add as TLanguage;
  Result.LangID      := LngID;  (* *)
  Result.LangSID     := LngSID;
  Result.LangName    := LngName;
  Result.EngAdjective:= EngAdj;
  Result.ForCurrUser := false;
end;

function TLanguages.GetItem(Index: Integer): TLanguage;
begin
  Result := inherited Items[Index] as TLanguage;
end;

end.


As an addiotional question:
I have no need for the Add method and would like to override it (with 
what AddWithValues now does) but Add is not virtual/dynamic. Can this be 
done?

Thanks in advance
Post Reply
Re: TCollection problem
5 Feb 2008 07:20:12 -0700
> Replace this:
> 
> >> procedure TLanguage.SetLangID(Value: Word);
> >> begin
> >>   FLangID := LangID;
> >> end;
> 
> With this:
> 
> procedure TLanguage.SetLangID(Value: Word);
> begin
>     FLangID := Value;
> end;
> 

aaargh
What a stupid typo ;-)
I actually had a macro running for repeated edits; must have choked 
somewhere. Thanks guys!

Post Reply
Re: TCollection problem
Tue, 5 Feb 2008 08:57:39 -0500
On 5 Feb 2008 05:34:42 -0700, Jan Doggen wrote:

So have you tried stepping through your property setter and tracing
the values?

> procedure TLanguage.SetLangID(Value: Word);
> begin
>   FLangID := LangID;
> end;


> I have no need for the Add method and would like to override it (with 
> what AddWithValues now does) but Add is not virtual/dynamic. Can this be 
> done?

You should just be able to rename your 'AddWithValues' to 'Add'. If
you want to keep the other 'Add' method then add an 'overload
directive to them both.

Why are you using shortstring instead of string?

-- 
Marc Rohloff [TeamB]
Post Reply
Re: TCollection problem
Tue, 05 Feb 2008 12:06:21 -030
Replace this:

 >> procedure TLanguage.SetLangID(Value: Word);
 >> begin
 >>   FLangID := LangID;
 >> end;

With this:

procedure TLanguage.SetLangID(Value: Word);
begin
    FLangID := Value;
end;

Marc is right, you are assigning FLangId with LangId property. That 
should create an infinite loop.

Leonardo M. Ramé
http://leonardorame.blogspot.com


Marc Rohloff [TeamB] escribió:
> On 5 Feb 2008 05:34:42 -0700, Jan Doggen wrote:
> 
> So have you tried stepping through your property setter and tracing
> the values?
> 
>> procedure TLanguage.SetLangID(Value: Word);
>> begin
>>   FLangID := LangID;
>> end;
> 
> 
>> I have no need for the Add method and would like to override it (with 
>> what AddWithValues now does) but Add is not virtual/dynamic. Can this
be 
>> done?
> 
> You should just be able to rename your 'AddWithValues' to 'Add'. If
> you want to keep the other 'Add' method then add an 'overload
> directive to them both.
> 
> Why are you using shortstring instead of string?
Post Reply
about | contact