简体   繁体   中英

IntToStr undeclared identifier error

I want to display certain integers in a memo, listbox or whatever. I've tried a memo, listbox and showmessage() but each throws incompatibility error between string and integers.

This is where it starts going wrong:

Memo1.Lines.Add('Physical Functioning: '+PhysFunc div PhysCount);

PhysFunc and PhysCount are integers calculated further up. I've tried, eg:

Memo1.Lines.Add('Physical Functioning: '+IntToStr(PhysFunc div PhysCount));

and:

Memo1.Lines.Add(IntToStr('Physical Functioning: '+PhysFunc div PhysCount));

and:

Memo1.Lines.Add(('Physical Functioning: '+PhysFunc div PhysCount).AsString);

And the same things with a ListBox and showmessage(). But none work. IntToStr returns E2003 undeclared identifier and .AsString returns the error, integer does not contain a member 'AsString'. Are there any alternatives?

I'm using an SDI application in Delphi - could that be the problem?

Thanks

You are calling the Add method of TStrings which receives a single parameter of type string . You need to concatenate a string and an integer. In order to do so you must convert the integer into a string.

You attempted to do that using IntToStr which is a perfectly reasonable approach. But that failed because the compiler could not locate IntToStr . Now, IntToStr is declared in the SysUtils unit and so you must add that unit to your uses clause.

However, I'd probably use Format here. Like IntToStr , this function is declared in SysUtils which you must use.

Memo1.Lines.Add(Format('Physical Functioning: %d', [PhysFunc div PhysCount]));

For more details on format read the documentation for format strings .

The first line of code doesn't work because PhysFunc div PhysCount is an integer and it is not converted to a string.

The second line of code should work, but I would check the actual values of PhysFunc and PhysCount . Is it possible that PhysCount is, somehow, equal to 0?

The third line of code doesn't work because IntToStr expects integers and you gave him, among others, a string.

The fourth line of code doesn't work because, as you said, integer does not contain a member 'AsString'

You also said:

IntToStr returns E2003 undeclared identifier

That could also be the problem: one of the two variables is not declared. Make sure that both of them are declared in the var section.

To conclude:

  1. Check to see if both variables are declared;
  2. Check to see if PhysCount <>0 at the time of the evaluation.

Good luck!

如果包含SysUtils,只需看看用途。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM