Page 1 of 1

Initialize global/shared/protected variables on declaration

Posted: Fri May 30, 2003 10:16 pm
by Revolver
It would be nice to be able to do this:

Code: Select all

Declare MyProc()

OpenConsole()
Global MyVar = 5
PrintN("MyVar="+ Str(MyVar))
MyProc()
CloseConsole()
End

Procedure MyProc()
  Shared    MyVar   = 10
  Protected ProcVar = 12
  PrintN("MyVar="+   Str(MyVar))
  PrintN("ProcVar="+ Str(ProcVar))
EndProcedure
All too often I find my procedures' upper portions getting cluttered up by lots of lines from having to declare variables, then initialize them on 2 separate lines each. It would be great to do it all on one line each and save space.

Re: Initialize global/shared/protected variables on declarat

Posted: Fri May 30, 2003 10:51 pm
by tinman
Revolver wrote:It would be great to do it all on one line each and save space.
DefType.l foo : foo = 1

That's on one line.

Posted: Sat May 31, 2003 1:03 am
by HarryO
I thought this was legal!

;
;Vars & Structures
;

x_var.w = 0
y_var.w = 0

;
;

Am I doing something wrong?

Or have I discovered an undocumented feature?

HarryO

Posted: Sat May 31, 2003 1:12 am
by tinman
HarryO wrote:I thought this was legal!
Yes, but you cannot do what Revolver is asking. To be able to use the keywords Global, Shared and Protected when you declare the variable and also set it's value at the same time.
Or have I discovered an undocumented feature?
No, the variables chapter in the reference manual explains that you can create a variable by using it. When you do "x=8" you are using it and creating it at the same time.

Re: Initialize global/shared/protected variables on declarat

Posted: Sat May 31, 2003 3:33 am
by Revolver
tinman wrote:DefType.l foo : foo = 1
Clever, but still wastes space and typing time to emulate something many other languages have