1

I have an array that is being created by parsing a semi-colon delimited list.

The splitting of the string into an array works and if I use a for-in and display each element I get the correct results.

procedure badSizeOfDemo(arrayString: String);
var
  semiColonSplitLine: TStringList;
begin
  semiColonSplitLine:= TStringList.Create;
  semiColonSplitLine.StrictDelimiter := true;
  semiColonSplitLine.Delimiter:= ';';
  semiColonSplitLine.DelimitedText:= arrayString;

  showMessage('arrayString: ' + arrayString);
  showMessage('size of split line: ' + IntToStr(SizeOf(semiColonSplitLine)));
end;

With the above code I always get the size of the array as '4' even when the arrayString contains ''.

Am I missing something fundamental here?

FURTHER PROCESSING following switch to using .Count

After splitting the array like this I then do a for-in and check that every element can be converted to a number before building an array of integers.

This essentially works, but then when I try to get the size of the integer array I get an illegal qualifier error.

procedure badSizeOfDemo(arrayString: String);
var
  semiColonSplitLine: TStringList;
  myElement: String = '';
  myIntegerArray: array of integer;
  count: Integer = 0; 
begin
  semiColonSplitLine:= TStringList.Create;
  semiColonSplitLine.StrictDelimiter := true;
  semiColonSplitLine.Delimiter:= ';';
  semiColonSplitLine.DelimitedText:= arrayString;

  showMessage('arrayString: ' + arrayString);
  showMessage('size of split line: ' + IntToStr(semiColonSplitLine.Count));

  for myElement in semiColonSplitLine do
  begin
    Try
        showMessage('field from split line: ' + myElement);
        myIntegerArray[count]:=StrToInt(myElement);
    except
      On E : EConvertError do
        ShowMessage('Invalid number encountered');
    end;
    count:=count+1;
  end;
  showMessage('myIntegerArray now has ' + myIntegerArray.Count + ' elements');

end;
Fat Monk
  • 2,077
  • 1
  • 26
  • 59
  • 3
    You are getting the size of the `TStringList` object reference, which is size of a pointer (4B). If you are looking for a way to get count of the items in your string list, use `semiColonSplitLine.Count` instead. – TLama Nov 10 '14 at 14:43
  • Thanks @TLama, however `.Count` only seems to work on my initial array.. I am further processing (now added to the OP) into an integer array and getting the same incorrect with `SizeOf` (understandably) but an `illegal qualifier` error with `.Count` – Fat Monk Nov 10 '14 at 15:00
  • 3
    You need to differentiate between an array and a string list. `semiColonSplitLine` is a string list (which is what you've initially referred to in your question), not an array, and to get a string list item count you need to use `Count`. To get length of your `myIntegerArray` array you need to use `Length(myIntegerArray)`. – TLama Nov 10 '14 at 15:06
  • 1
    That's got it, thanks again. I'll read up a bit and post a summary of the differences as an answer for future reference. – Fat Monk Nov 10 '14 at 16:08

0 Answers0