Article ID: 80411 - Last Review: August 16, 2005 - Revision: 2.1

"Illegal Function Call" Using Asc with Uninitialized String

This article was previously published under Q80411

On This Page

Expand all | Collapse all

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
				

APPLIES TO
  • Microsoft QuickBasic 4.0
  • Microsoft QuickBASIC 4.0b
  • Microsoft QuickBasic 4.5 for MS-DOS
  • Microsoft BASIC Compiler 6.0
  • Microsoft BASIC Compiler 6.0b
  • Microsoft BASIC Professional Development System 7.0
  • Microsoft BASIC Professional Development System 7.1
Keywords: 
KB80411
Retired KB ArticleRetired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
 

Article Translations