Board index » delphi » Help!: 'Invalid variable reference'

Help!: 'Invalid variable reference'

Hi,

I'm fairly new to Pascal and have just written my first biggish program.

But when I try to compile (using Turbo Pascal 3.0) it i get this error msg:

'Invalid variable reference'

in this block of code (asterik marks where cursor stops):

Repeat
 randomize;
 QType := random(4);
  if QType = 0 then ADD(Col1,Col2,Level,AScore,AQ)
  *else if QType = 1 then SUB(Col1,Col2,SScore,SQ)
  else if QType = 2 then MULTI(Col1,Col2,MScore,MQ)
  else if QType = 3 then DIVIDE(Col1,Col2,DScore,DQ);
 QCount:=QCount+1;
 until QCount = Questions;

I don't know if I've given enough detail but if anyone can tell me what the
error message actually means and give any advice I'd be greatful.

Many thanks,

Tom

 

Re:Help!: 'Invalid variable reference'


hi tom,

"Tom Hart" <t...@hhturner.co.uk> schrieb im Newsbeitrag
news:966fni$e2f$1@news5.svr.pol.co.uk...

Quote
> Hi,

> I'm fairly new to Pascal and have just written my first biggish
program.

> But when I try to compile (using Turbo Pascal 3.0) it i get this error
msg:

> 'Invalid variable reference'

> in this block of code (asterik marks where cursor stops):

> Repeat
>  randomize;
>  QType := random(4);
>   if QType = 0 then ADD(Col1,Col2,Level,AScore,AQ)
>   *else if QType = 1 then SUB(Col1,Col2,SScore,SQ)
>   else if QType = 2 then MULTI(Col1,Col2,MScore,MQ)
>   else if QType = 3 then DIVIDE(Col1,Col2,DScore,DQ);
>  QCount:=QCount+1;
>  until QCount = Questions;

> I don't know if I've given enough detail but if anyone can tell me
what the
> error message actually means and give any advice I'd be greatful.

the error must be in the paramters of your procedure ADD();
in your definition the must be var-paramters.
i think you use them to receive results fomr your procedure.
there probably is a mismatch in declaration vs. actual parameters.

Quote

> Many thanks,
> Tom

hth
jochen

Re:Help!: 'Invalid variable reference'


Thanks for help,

I was using functions in some parts instead of procedures. But changing
them to back to procedures with variable parameters seems to have cured it.

Don't fully understand how functions work so I'd probably messed it up.

Anyway, fixed thanks to your help

Cheers,
Tom

Quote
"Jochen" <jote...@hotmail.com> wrote in message

news:966hje$k1dhq$1@ID-55934.news.dfncis.de...
Quote
> hi tom,

> "Tom Hart" <t...@hhturner.co.uk> schrieb im Newsbeitrag
> news:966fni$e2f$1@news5.svr.pol.co.uk...
> > Hi,

> > I'm fairly new to Pascal and have just written my first biggish
> program.

> > But when I try to compile (using Turbo Pascal 3.0) it i get this error
> msg:

> > 'Invalid variable reference'

> > in this block of code (asterik marks where cursor stops):

> > Repeat
> >  randomize;
> >  QType := random(4);
> >   if QType = 0 then ADD(Col1,Col2,Level,AScore,AQ)
> >   *else if QType = 1 then SUB(Col1,Col2,SScore,SQ)
> >   else if QType = 2 then MULTI(Col1,Col2,MScore,MQ)
> >   else if QType = 3 then DIVIDE(Col1,Col2,DScore,DQ);
> >  QCount:=QCount+1;
> >  until QCount = Questions;

> > I don't know if I've given enough detail but if anyone can tell me
> what the
> > error message actually means and give any advice I'd be greatful.

> the error must be in the paramters of your procedure ADD();
> in your definition the must be var-paramters.
> i think you use them to receive results fomr your procedure.
> there probably is a mismatch in declaration vs. actual parameters.

> > Many thanks,
> > Tom

> hth
> jochen

Re:Help!: 'Invalid variable reference'


Quote
> I'm fairly new to Pascal and have just written my first biggish program.

> Repeat
>  randomize;
>  QType := random(4);
>   if QType = 0 then ADD(Col1,Col2,Level,AScore,AQ)
>   *else if QType = 1 then SUB(Col1,Col2,SScore,SQ)
>   else if QType = 2 then MULTI(Col1,Col2,MScore,MQ)
>   else if QType = 3 then DIVIDE(Col1,Col2,DScore,DQ);
>  QCount:=QCount+1;
>  until QCount = Questions;

   I see you have an answer to your question, but I see some things which
also should be changed/redone which will make your code better.
  1. You have the Randomize call in a loop - _don't_ do that!  Randomize
should be called _once_ on a program, followed by as many Random calls as
you need.  The TP/BP compilers documentation specifically states this
fact, and it explains that calling it multiple times will produce a very
NON-random set of values.
  2. In QCount is an integer/Word data type, you should use Inc(QCount),
rather than "QCount := QCount+1".
  3. Your code seems that it would be better coded with a Case statement,
instead of the serial "If...Then" code you have.  The Case statement is
"made" for the sort of logic you have here, and I encourage you to learn
and use it for such situations.

Re:Help!: 'Invalid variable reference'


Quote
"Mike Copeland" <cm...@qwest.net> wrote in message

news:MPG.14f122d4df1c2c298979d@news.qwest.net...

Quote
> > I'm fairly new to Pascal and have just written my first biggish program.

> > Repeat
> >  randomize;
> >  QType := random(4);
> >   if QType = 0 then ADD(Col1,Col2,Level,AScore,AQ)
> >   *else if QType = 1 then SUB(Col1,Col2,SScore,SQ)
> >   else if QType = 2 then MULTI(Col1,Col2,MScore,MQ)
> >   else if QType = 3 then DIVIDE(Col1,Col2,DScore,DQ);
> >  QCount:=QCount+1;
> >  until QCount = Questions;

>    I see you have an answer to your question, but I see some things which
> also should be changed/redone which will make your code better.
>   1. You have the Randomize call in a loop - _don't_ do that!  Randomize
> should be called _once_ on a program, followed by as many Random calls as
> you need.  The TP/BP compilers documentation specifically states this
> fact, and it explains that calling it multiple times will produce a very
> NON-random set of values.
>   2. In QCount is an integer/Word data type, you should use Inc(QCount),
> rather than "QCount := QCount+1".
>   3. Your code seems that it would be better coded with a Case statement,
> instead of the serial "If...Then" code you have.  The Case statement is
> "made" for the sort of logic you have here, and I encourage you to learn
> and use it for such situations.

Yes I agree, But if he moves to a smarter compiler (i.e tp 7) then that's
generally taken care of..
the blah:=blah+1; becomes inc(blah,1) at the  compilation stage,but do if's
like that get translated into a case equivelent? I would assume no, unless
Qtype where a byte value. That's actually an intresting thing I'd like to
know. Dose TP optimize like that. ahh well..
The other thing I wanted to say was that functions are easy to work out.
thay are like a variable.

i.e.
program blahfunction;
var
test1,test2:string;
function blah(blahdata,blahdata2:string):string;
             { The string bit on the end tells you what kind of variable
blah is going to be refrenced as}
var
a:byte;
tmp:String;
begin
tmp:=blahdata+blahdata2; {Join the two strings}
blah:=tmp; {set blah to the result }
end;

begin {Main program}
writeln('Test program');
test1:='Hello ';
test2:='world'; {Some data}
test3:=blah(test1,test2);
writeln('Blah outputs : ',blah(test1,test2) ); { you can use blah just like
it's a variable}
writeln('Test3 is        : ',test3);                   {which means that you
can use it directley}
                                                               { or to set
another variable.}
{Just play around with them :) }
end.

I hope this helps.. it's off the top of my head :)

Re:Help!: 'Invalid variable reference'


Quote
"Vortex Vandel" <v...@spanner.net> wrote in message

news:6zNh6.16465$o85.82661@news-server.bigpond.net.au...

Quote

> "Mike Copeland" <cm...@qwest.net> wrote in message
> news:MPG.14f122d4df1c2c298979d@news.qwest.net...
> > > I'm fairly new to Pascal and have just written my first biggish
program.

> > > Repeat
> > >  randomize;
> > >  QType := random(4);
> > >   if QType = 0 then ADD(Col1,Col2,Level,AScore,AQ)
> > >   *else if QType = 1 then SUB(Col1,Col2,SScore,SQ)
> > >   else if QType = 2 then MULTI(Col1,Col2,MScore,MQ)
> > >   else if QType = 3 then DIVIDE(Col1,Col2,DScore,DQ);
> > >  QCount:=QCount+1;
> > >  until QCount = Questions;

> >    I see you have an answer to your question, but I see some things
which
> > also should be changed/redone which will make your code better.
> >   1. You have the Randomize call in a loop - _don't_ do that!  Randomize
> > should be called _once_ on a program, followed by as many Random calls
as
> > you need.  The TP/BP compilers documentation specifically states this
> > fact, and it explains that calling it multiple times will produce a very
> > NON-random set of values.
> >   2. In QCount is an integer/Word data type, you should use Inc(QCount),
> > rather than "QCount := QCount+1".
> >   3. Your code seems that it would be better coded with a Case
statement,
> > instead of the serial "If...Then" code you have.  The Case statement is
> > "made" for the sort of logic you have here, and I encourage you to learn
> > and use it for such situations.

> Yes I agree, But if he moves to a smarter compiler (i.e tp 7) then that's
> generally taken care of..
> the blah:=blah+1; becomes inc(blah,1) at the  compilation stage,but do
if's
> like that get translated into a case equivelent? I would assume no, unless
> Qtype where a byte value. That's actually an intresting thing I'd like to
> know. Dose TP optimize like that. ahh well..
> The other thing I wanted to say was that functions are easy to work out.
> thay are like a variable.

> i.e.
> program blahfunction;
> var
> test1,test2:string;
> function blah(blahdata,blahdata2:string):string;
>              { The string bit on the end tells you what kind of variable
> blah is going to be refrenced as}
> var
> a:byte;
> tmp:String;
> begin
> tmp:=blahdata+blahdata2; {Join the two strings}
> blah:=tmp; {set blah to the result }
> end;

> begin {Main program}
> writeln('Test program');
> test1:='Hello ';
> test2:='world'; {Some data}
> test3:=blah(test1,test2);
> writeln('Blah outputs : ',blah(test1,test2) ); { you can use blah just
like
> it's a variable}
> writeln('Test3 is        : ',test3);                   {which means that
you
> can use it directley}
>                                                                { or to set
> another variable.}
> {Just play around with them :) }
> end.

> I hope this helps.. it's off the top of my head :)

Thanks to you all for the advice. Changed the 'randomize', 'inc' and 'case
of'
parts. Still don't quite get the functions though. Understand them in
principle
but can't get them to work in my program, but I'll look into it.

Thanks again,

Tom

Re:Help!: 'Invalid variable reference'


Quote
Mike Copeland <cm...@qwest.net> wrote:
>   2. In QCount is an integer/Word data type, you should use Inc(QCount),
> rather than "QCount := QCount+1".

Actually, I'd advise against this, at least during testing, since "QCount :=
QCount + 1" generates run-time checks which "Inc(QCount)" does not.

And yes, I did once locate a bug in a program thanks to using the longer
form.

--
______________________________________________________________________
     The Scarlet Manuka,      |        Nitpickers' Party motto:
  Pratchett Quoter At Large,  |  "He who guards his lips guards his
 First Prophet of Bonni, is:  |  soul, but he who speaks rashly will
   sa...@maths.uwa.edu.au     |    come to ruin." -- Proverbs 13:3
______________________________|_______________________________________

Other Threads