Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: initializing variables Post Reply Post New Topic
Author Message
hello
Groupie
Groupie
Avatar

Joined: 05 Feb 2014
Online Status: Offline
Posts: 85
Quote hello Replybullet Topic: initializing variables
     Posted: 20 Feb 2014 at 3:19am
Let's say you want to declare a variable and initialize it with something other than zero.

numberVar amount_last := 12;

but, the variable above gets changed with each record read in a formula field placed in the detail section of CR. How would you initialize it outside of the detail section?

Thanks for any help.
IP IP Logged
lolly54
Groupie
Groupie
Avatar

Joined: 25 Sep 2011
Online Status: Offline
Posts: 58
Quote lolly54 Replybullet Posted: 20 Feb 2014 at 3:41am
Usually I will put the {@Variable - initialize} in group header. So, the variable will be initialized to the defined value every time it loops through each group. 
IP IP Logged
hello
Groupie
Groupie
Avatar

Joined: 05 Feb 2014
Online Status: Offline
Posts: 85
Quote hello Replybullet Posted: 20 Feb 2014 at 4:00am
OK. I see now.

When referencing the new variable in other formulas, you reference it by the formula field name it was created in.

Thanks!
IP IP Logged
lolly54
Groupie
Groupie
Avatar

Joined: 25 Sep 2011
Online Status: Offline
Posts: 58
Quote lolly54 Replybullet Posted: 20 Feb 2014 at 4:55am
Let me be clearer...

Group Header
{@Variable - initialize}
WhilePrintingRecords;
NumberVar NoVar := 12; //setting the variable to 12

Details
{@Variable}
WhilePrintingRecords;
NumberVar NoVar;

NoVar := NoVar + 10; //whatever you want to do. In this sample, it will be 12 + 10

Group Footer
{@Variable - display}
WhilePrintingRecords;
NumberVar NoVar;

NoVar; //NoVar is 22


Hope it make sense. :)


Edited by lolly54 - 20 Feb 2014 at 4:56am
IP IP Logged
hello
Groupie
Groupie
Avatar

Joined: 05 Feb 2014
Online Status: Offline
Posts: 85
Quote hello Replybullet Posted: 20 Feb 2014 at 8:56am
Yes, that makes sense.

I thought I read somewhere that each time you declare a variable without specifying a value, CR initializes it to 0 or spaces(if a string). But I may have misunderstood.

In your example above, the first time NoVar is declared (in the Group header), it is initialized to 12 by the user.

The second time NoVar is declared (in the Detail), it doesn't get initialized by the user OR by CR but retains the previous value. And, again (in the Group Footer), NoVar gets declared but retains its previous value.

Does the WhilePrintingRecords statement have something to do with NoVar retaining its values, or is this the norm?

Thanks
IP IP Logged
lolly54
Groupie
Groupie
Avatar

Joined: 25 Sep 2011
Online Status: Offline
Posts: 58
Quote lolly54 Replybullet Posted: 21 Feb 2014 at 12:22am
Group Header  - you initialize it to whatever number you want the variable to starts with. I initialize it to 0 usually (as follow)
{@Variable - initialize}
WhilePrintingRecords;
NumberVar NoVar := 0;

Details - this is where you do the calculation
{@Variable}
WhilePrintingRecords;
NumberVar NoVar;

NoVar := NoVar + 1;

Just say I have two Details records, NoVar will be 2. Again, if there's three Details records, NoVar will be 3.

Group Footer - to display the variable
{@Variable - display}
WhilePrintingRecords;
NumberVar NoVar;

NoVar; //NoVar is 2 in this sample, as it has two details records. NoVar will be 3 if it has three details records.

Does it make sense?

(Can't answer the WhiePrintingRecords question... i don't quite understand too.. haha)
IP IP Logged
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum