"Illegal Function Call" Using Asc with Uninitialized String (80411)



The information in this article applies to:

  • Microsoft QuickBASIC 4.0
  • Microsoft QuickBASIC 4.0b
  • Microsoft QuickBASIC 4.5
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0
  • Microsoft BASIC Compiler for MS-DOS and OS/2 6.0b
  • Microsoft Basic Professional Development System for MS-DOS 7.0
  • Microsoft Basic Professional Development System for MS-DOS 7.1

This article was previously published under Q80411

SUMMARY

If you try to use the Asc function on a string that has been initialized to a value of null (""), you will correctly receive an "Illegal function call" error message. The string must be assigned to a value other than "" (the null string) for the Asc function to return the ASCII value of the first character in the string.

MORE INFORMATION

This information is documented in the QB.EXE 4.5 or QBX.EXE 7.0/7.1 online Help for the Asc function, but is not available when the error message is received when the Help option is chosen. Below are two code examples that reproduce the error. By assigning the string to any value, the Asc function works without error.

The following code examples will fail:

Code Error Example 1

PRINT ASC(x$)
				

Code Error Example 2

x$ = ""
PRINT ASC(x$)
				
The following code examples will work correctly:

Correct Code Example 1

x$=" "
PRINT ASC(x$)
				
Output: 32

Correct Code Example 2

x$="Pearl Jam"
PRINT ASC(x$)
				
Output: 80

To work around the error, check the length of the string with the Len function in an IF...THEN statement to ensure it is greater than zero before passing the string as an argument to the Asc function. The following code example demonstrates the workaround. The code allows you to only use the Asc function when something has been assigned to the string, otherwise it sets the Asc value to zero. Assigning Temp$ to CHR$(0) returns a 0, but assigning it "" gives you an "Illegal function call".
If LEN(Temp$) > 0 THEN
    ' Use the Asc Function
    AscVal = ASC(Temp$)
Else
    AscVal = 0
End if
PRINT AscVal
				

Modification Type:MinorLast Reviewed:8/16/2005
Keywords:KB80411