TStrings, TStringList default property Strings

Hi!


I am facing some problems with default properties. When I try to use them with TStrings, TStringList, I get an exception.

in ap_classes it looks like this:


With Scripter.DefineClass(TStrings) do
  begin
    DefineMethod('Create',0,tkClass,TStrings,__TStringsCreate,true,0,'');
    DefineMethod('Destroy',0,tkNone,nil,__TStringsDestroy,false,0,'');
    DefineMethod('Add',1,tkInteger,nil,__TStringsAdd,false,0,'S: string');
    DefineMethod('AddObject',2,tkInteger,nil,__TStringsAddObject,false,0,'S: string; AObject: TObject');
    DefineMethod('Append',1,tkNone,nil,__TStringsAppend,false,0,'S: string');
    DefineMethod('AddStrings',1,tkNone,nil,__TStringsAddStrings,false,0,'Strings: TStrings');
    DefineMethod('Assign',1,tkNone,nil,__TStringsAssign,false,0,'Source: TPersistent');
    DefineMethod('BeginUpdate',0,tkNone,nil,__TStringsBeginUpdate,false,0,'');
    DefineMethod('Clear',0,tkNone,nil,__TStringsClear,false,0,'');
    DefineMethod('Delete',1,tkNone,nil,__TStringsDelete,false,0,'Index: Integer');
    DefineMethod('EndUpdate',0,tkNone,nil,__TStringsEndUpdate,false,0,'');
    DefineMethod('Equals',1,tkVariant,nil,__TStringsEquals,false,0,'Strings: TStrings');
    DefineMethod('Exchange',2,tkNone,nil,__TStringsExchange,false,0,'Index1: Integer; Index2: Integer');
    DefineMethod('GetEnumerator',0,tkClass,TStringsEnumerator,__TStringsGetEnumerator,false,0,'');
    DefineMethod('GetText',0,tkVariant,nil,__TStringsGetText,false,0,'');
    DefineMethod('IndexOf',1,tkInteger,nil,__TStringsIndexOf,false,0,'S: string');
    DefineMethod('IndexOfName',1,tkInteger,nil,__TStringsIndexOfName,false,0,'Name: string');
    DefineMethod('IndexOfObject',1,tkInteger,nil,__TStringsIndexOfObject,false,0,'AObject: TObject');
    DefineMethod('Insert',2,tkNone,nil,__TStringsInsert,false,0,'Index: Integer; S: string');
    DefineMethod('InsertObject',3,tkNone,nil,__TStringsInsertObject,false,0,'Index: Integer; S: string; AObject: TObject');
    DefineMethod('LoadFromFile',1,tkNone,nil,__TStringsLoadFromFile,false,0,'FileName: string');
    DefineMethod('LoadFromStream',1,tkNone,nil,__TStringsLoadFromStream,false,0,'Stream: TStream');
    DefineMethod('Move',2,tkNone,nil,__TStringsMove,false,0,'CurIndex: Integer; NewIndex: Integer');
    DefineMethod('SaveToFile',1,tkNone,nil,__TStringsSaveToFile,false,0,'FileName: string');
    DefineMethod('SaveToStream',1,tkNone,nil,__TStringsSaveToStream,false,0,'Stream: TStream');
    DefineMethod('SetText',1,tkNone,nil,__TStringsSetText,false,0,'Text: PChar');
    DefineProp('Capacity',tkInteger,__GetTStringsCapacity,__SetTStringsCapacity,nil,false,0);
    DefineProp('CommaText',tkVariant,__GetTStringsCommaText,__SetTStringsCommaText,nil,false,0);
    DefineProp('Count',tkInteger,__GetTStringsCount,nil,nil,false,0);
    DefineProp('DefaultEncoding',tkVariant,__GetTStringsDefaultEncoding,__SetTStringsDefaultEncoding,nil,false,0);
    DefineProp('Delimiter',tkVariant,__GetTStringsDelimiter,__SetTStringsDelimiter,nil,false,0);
    DefineProp('DelimitedText',tkVariant,__GetTStringsDelimitedText,__SetTStringsDelimitedText,nil,false,0);
    DefineProp('Encoding',tkVariant,__GetTStringsEncoding,nil,nil,false,0);
    DefineProp('LineBreak',tkVariant,__GetTStringsLineBreak,__SetTStringsLineBreak,nil,false,0);
    DefineProp('Names',tkVariant,__GetTStringsNames,nil,nil,false,1);
    DefineProp('Objects',tkVariant,__GetTStringsObjects,__SetTStringsObjects,nil,false,1);
    DefineProp('QuoteChar',tkVariant,__GetTStringsQuoteChar,__SetTStringsQuoteChar,nil,false,0);
    DefineProp('Values',tkVariant,__GetTStringsValues,__SetTStringsValues,nil,false,1);
    DefineProp('ValueFromIndex',tkVariant,__GetTStringsValueFromIndex,__SetTStringsValueFromIndex,nil,false,1);
    DefineProp('NameValueSeparator',tkVariant,__GetTStringsNameValueSeparator,__SetTStringsNameValueSeparator,nil,false,0);
    DefineProp('StrictDelimiter',tkVariant,__GetTStringsStrictDelimiter,__SetTStringsStrictDelimiter,nil,false,0);
    DefaultProperty := DefineProp('Strings',tkVariant,__GetTStringsStrings,__SetTStringsStrings,nil,false,1);
    DefineProp('Text',tkVariant,__GetTStringsText,__SetTStringsText,nil,false,0);
    DefineProp('WriteBOM',tkVariant,__GetTStringsWriteBOM,__SetTStringsWriteBOM,nil,false,0);
  end;
  With Scripter.DefineClass(TStringList) do
  begin
    DefineMethod('Create',0,tkClass,TStringList,__TStringListCreate,true,0,'');
    DefineMethod('Destroy',0,tkNone,nil,__TStringListDestroy,false,0,'');
    DefineMethod('Add',1,tkInteger,nil,__TStringListAdd,false,0,'S: string');
    DefineMethod('AddObject',2,tkInteger,nil,__TStringListAddObject,false,0,'S: string; AObject: TObject');
    DefineMethod('Assign',1,tkNone,nil,__TStringListAssign,false,0,'Source: TPersistent');
    DefineMethod('Clear',0,tkNone,nil,__TStringListClear,false,0,'');
    DefineMethod('Delete',1,tkNone,nil,__TStringListDelete,false,0,'Index: Integer');
    DefineMethod('Exchange',2,tkNone,nil,__TStringListExchange,false,0,'Index1: Integer; Index2: Integer');
    DefineMethod('Find',2,tkVariant,nil,__TStringListFind,false,0,'S: string; Index: Integer').SetVarArgs([1]);
    DefineMethod('IndexOf',1,tkInteger,nil,__TStringListIndexOf,false,0,'S: string');
    DefineMethod('Insert',2,tkNone,nil,__TStringListInsert,false,0,'Index: Integer; S: string');
    DefineMethod('InsertObject',3,tkNone,nil,__TStringListInsertObject,false,0,'Index: Integer; S: string; AObject: TObject');
    DefineMethod('Sort',0,tkNone,nil,__TStringListSort,false,0,'');
    DefineProp('Duplicates',tkEnumeration,__GetTStringListDuplicates,__SetTStringListDuplicates,nil,false,0);
    DefineProp('Sorted',tkVariant,__GetTStringListSorted,__SetTStringListSorted,nil,false,0);
    DefineProp('CaseSensitive',tkVariant,__GetTStringListCaseSensitive,__SetTStringListCaseSensitive,nil,false,0);
    DefineProp('OwnsObjects',tkVariant,__GetTStringListOwnsObjects,__SetTStringListOwnsObjects,nil,false,0);
    DefaultProperty := DefineProp('Strings',tkVariant,__GetTStringsStrings,__SetTStringsStrings,nil,false,1);
  end;


In TStringList I tried to add the DefaultProperty too, but this had no effect. What's wrong?

Thanks in advance for your help,

Stefan.

I forgot to post my source code:


uses                   
  Classes, Graphics, Controls, Forms, Dialogs;

var                    
  myList : TStrings;
begin
  myList := TStringList.Create;
  myList.Add('Hallo');
  
    myList[0] := 'Hallo Welt';
  
     // this one works:
    //myList.Strings[0] := 'Hallo Welt!!!!!';
  
  
  ShowMessage(myList.Text);
  myList.Free;
end;

Hi Stefan,

 
we have fixed this internally and sent you the patch to your registered e-mail.
 
Wagner

now it works fine. Thank you!

Sorry for reacting on a very old post.
I'm having the same problem as above.
When I use:


aInput := TStringList.Create;
aInput.add('Test');
ShowMessage(aInput[0]);


I get an error saying 'RUNTIME ERROR Variable is not an array'

When I use:


aInput := TStringList.Create;
aInput.add('Test');
ShowMessage(aInput.Strings[0]);


I don't get an error and everything works.

Is it possible the patch you mentioned above has dissappeared again?

Can you provide the full script? Is the variable declared as TStrings or TStringList?

Using the IDEPro demo, If I just create an empty project, drop a button, and add the following code to the buttonclick event, it works fine:

procedure Button1Click(Sender: TObject);
var                     
  aInput: TStringList;
begin
  aInput := TStringList.Create;
  aInput.add('Test');
  ShowMessage(aInput[0])  ;
end;

Hi Wagner,

This is the full script:

function testscript: String;
var
  aInput: TStringList;
begin
  aInput := TStringList.Create;
  aInput.add('Test');
  Result := aInput[0];
end;


This gives the error mentioned above: Variable is not an array.

When I use the script:

function testscript: String;
var
  aInput: TStringList;
begin
  aInput := TStringList.Create;
  aInput.add('Test');
  Result := aInput.Strings[0];
end;


I don't get an error and the result of that script is the string 'Test'.

Sorry, works fine here. Again, I tested with the scripter IDEPro demo (which just opens the scripter IDE). From an empty application created by IDE, I just dropped a button, double click it to create an event, and then changed the script to the following one. Works fine.


{$FORM TForm2, Unit2.sfm}

uses
  Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;

function testscript: String;
var
  aInput: TStringList;
begin
  aInput := TStringList.Create;
  aInput.add('Test');
  Result := aInput[0];
end;

procedure Button1Click(Sender: TObject);
begin
  ShowMessage(testscript);
end;