Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't get function to return Array Of String ??? 4

Status
Not open for further replies.

BobbaFet

Programmer
Feb 25, 2001
903
NL
Hello all,

Here's what I am trying to do: What I want is for my function to return an array of string. I've got a file which is loaded into a RichEdit called QuestionList. Each line contains a string formatted as such:

Q#####D#:<QUESTION>:<CORRECT ANSWER>:<ANSWER>:<ANSWER>:<ANSWER>

Q##### is the Question number
D# is the difficulty number (1,2 or 3 aka Easy, Normal or Hard
: is to seperate the fields from eachother

This code following I've written to assign the array resulting from this function to a global array (declared in the private section of the form for easy access) to get three arrays with the respective question difficulty.

Code:
function GetQuestions(Difficulty: integer): array of string;
var i,j: integer; Str: string;
begin
SetLength(Result,0); j := 0;
hard
for i := 0 to (Form1.QuestionList.Lines.Count - 1) do
        begin
        Str := Form1.QuestionList.Lines[i];
        if StrToInt(Str[8]) = Difficulty then
                begin
                j := Length(Result); Inc(j);
                SetLength(Result,j);
                Result[j - 1] := Str;
                end;
        end;
end;

But for some reason it won't allow me to return an array
from my function, is this simply not possible or am I missing something? If it's just not possible I suppose I could use a plain stringlist. I'm using Delphi 7 Enterprise.

The error I'm getting:
Identifier expected but 'ARRAY' found
+ I get on every line within the function in which i work with result 'Incompatible types'

[bobafett] BobbaFet [bobafett]

Everyone has a right to my opinion.
[/b]
Great Delphi Websites faq102-5352
 

The trick is to define a type. For example:

Code:
type
  TQuestionArray = array of string;
:
:
:
function GetQuestions(Difficulty: integer): TQuestionArray;
begin
:
end;
:
:
procedure TForm1.Button1Click(Sender: TObject);
var
  saQuestions: TQuestionArray;
  i:integer;
begin
  ListBox1.Clear;
  saQuestions := GetQuestions( 0 );
  for i := Low(saQuestions) to High( saQuestions ) do
    ListBox1.Items.Add( saQuestions[i] );
end;


 
why not use Tstringlist?

--------------------------------------
What You See Is What You Get
 
I think Zathras was trying to get across a general tip for returning arrays of any datatype. But in this particular case, because the user has an array of string, a TStringList is better suited.

Clive [infinity]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer."
Paul Ehrlich
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 

I agree, I would probably use a TStringList, but I was just trying to answer the question as asked.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top