Microsoft KB Archive/45479

From BetaArchive Wiki
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Knowledge Base


TEXTWIDTH "Type Mismatch" Interpreted but Not When Compiled

Article ID: 45479

Article Last Modified on 11/21/2006

This article was previously published under Q45479

SUMMARY

The first reference to a variable cannot be in a toolbox-library routine. All variables used with library (MBLC) routines must be explicitly initialized (assigned to a certain value) before you use them with a library statement. If this is not done, incorrect program behavior may occur. QuickBASIC DOES NOT explicitly warn you about uninitialized variables passed to toolbox-library routines; thus, you must remember to initialize them.

An example below that uses the TextWidth routine shows the inconsistent symptoms if you fail to initialize passed parameters.

MORE INFORMATION

The interpreter in QuickBASIC 1.00 for Macintosh gives a "Type Mismatch" error for the following code, but the compiler in QuickBASIC fails to give you any error:

     TextWidth a$,size%
     print size%
                

The proper method to call the TextWidth function is to initialize the variables before invocation. This establishes a region of memory for the variables to be stored. This is also true with all other Toolbox Library Statements. The following code shows proper use of the TextWidth statement:

    a$="A"
     size%=0
     TextWidth a$, size%
     PRINT size%
                

For a related example, query on the following word in this knowledge base:

GetDirName


The following is taken from Page 420 of the "Microsoft QuickBASIC for Apple Macintosh: Language Reference" manual:

E.2.3.1 Initializing Variables

The first reference to a variable cannot be in a ToolBox statement. All variables used with library routines must be explicitly initialized (declared to be a certain value) before you use them with a library statement. This initialization requirement does not apply to array variables, since the elements of an array are automatically initialized to 0 when the array is dimensioned. However, arrays must be dimensioned or referenced in the source code before being referenced in a library call.



Additional query words: MQuickB

Keywords: KB45479