This is a common programming issue with many computer languages. The simple solution
is, if you must use globals, apply a distinctive naming convention. I prefer to start
global names with "g_", myself.
------------------
Tom Hanlin
PowerBASIC Staff
Announcement
Collapse
No announcement yet.
DIM xx AS what..?
Collapse
X
-
DIM xx AS what..?
Following little prog shows the danger of using same variable names as
both Global and Local (first posted in another thread). Okay, so I took
it a bit further and tested other variants. In PBMAIN, hEdit declare gives
same result if declared: STATIC, DIM hEdit AS STATIC or DIM hEdit AS LOCAL.
All is well, sort of, until one test plain "DIM hEdit AS LONG" there.
Suddenly different result in DlgProc. How come? In some way, this kind
of indicates that plain DIM in routine acts same as DIM x AS GLOBAL, but
if I remove the global declare in the beginning, compiler protests, like
it should. Why is plain DIM different from for example LOCAL, or even
DIM x AS LOCAL .. ? Documentation says they all should be local to procedure..
Code:'¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ ' Declares '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ #COMPILE EXE #INCLUDE "WIN32API.INC" GLOBAL hEdit AS LONG DECLARE CALLBACK FUNCTION DlgProc() AS LONG '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ ' Create dialog and controls, etc '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ FUNCTION PBMAIN () AS LONG LOCAL hDlg AS LONG LOCAL hEdit AS LONG '<- test dim and see what happens.. DIALOG NEW 0, "Find the error..",,, 195, 50, %WS_CAPTION OR %WS_SYSMENU, 0 TO hDlg CONTROL ADD BUTTON, hDlg, %IDOK, "hEdit's value", 4, 4, 60, 14, %WS_TABSTOP hEdit = 55 DIALOG SHOW MODAL hDlg CALL DlgProc END FUNCTION '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ ' Main callback '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤ CALLBACK FUNCTION DlgProc() AS LONG IF CBMSG = %WM_COMMAND THEN IF CBCTL = %IDOK THEN MSGBOX "If no danger with mixed Local/Global, hEdit should be 55, but is: " & STR$(hEdit) END IF END IF END FUNCTION
------------------
[This message has been edited by Borje Hagsten (edited July 25, 2001).]Tags: None
Leave a comment: