Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable Scope

Status
Not open for further replies.

sbalun

Programmer
Feb 12, 2001
62
US
By default, I beleive that any variables declared in Crystal are considered "Global" in scope, meaning they are accessible anywhere in the main report. So I should be able to decalre a global vairable in one formula, and another fourmula will be able to use the contents placed in the variable by the first formula.

What I don't understand is why I have to redeclare a variable I specify as Global in any other formulas I want to use said variable. If I don't declare it again I get errors in the new formula.

This is not what I'm use to. I assumed that by declaring a variable to be global that I could reference that variable in any other function WITHOUT having to declare it again.

Am I doing something wrong in redeclaring the variable or is this just a peculiarity of Crystal Reports?

Thanks.

--scott
 
I asked myself the same question this morning. I came to the conclusion that it made sense, because Crystal Reports doesn't know in which formula the variable was first declared. The formulas aren't always read sequentially like in C++ or something like that. So declaring it everywhere makes sure it won't be "undefined".

I've read a few tutorials and that's how it works, you're not doing anything wrong.
 
...well good. At least I know I'm not going crazy. From the background I have in programming it just seemed counter intuative to have to redeclare a GLOBAL variable in every formula I used it in. But if that's the way it's done in Crystal than I'll just have to get used to doing it that way.

 
I never really gave it much thought but yes Global variables don't really act like VB types. I constantly redefine my variables in each formula for the main reason is to make certain they are defined.

I suppose you could set an "defining" formula in the report header which would set the definitions for variables throughout the main report. But I really don't like that myself...perhaps due to habit. I definately wouldn't define a variable in anyother way since formulas come and go with maintenance and I would not want to start "hunting" for where I first defined a particular variable.

I don't refer to variables as Local or Global the only ones deserving special attention IMHO are Shared types between Main and subreports. Otherwise they are just plain StringVar or NumberVar...etc.



Jim Broadbent
 
With a background in Cobol, and a Cobol-derived language called Unisys Linc, I too found it irritating that you have no way of defining your own working storage or setup data or whatever. But it's Crystal's design philosophy.

Madawc Williams
East Anglia
Great Britain
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top