For loops in Crystal Reports 2008

Go To StackoverFlow.com

0

I'm trying to perform a very basic depreciation calculation based on the age of an asset using a For loop in Crystal 2008, and cannot get it working for the life of me.

The loop looks like this:

NumberVar AssetValue := {CIID.Currency4};
NumberVar DepreciationPercentage := {vw_DepreciationValues.Percent};
NumberVar AssetAge := DateDiff("yyyy",{CIID.Date4},CurrentDate);
Numbervar i := 0;

for i := 0 to AssetAge do
(
    AssetValue = AssetValue - ((AssetValue/100)*DepreciationPercentage);
    i = i + 1;
);
AssetValue;

For some reason, it always outputs AssetValue as the same number that went in....almost like it gets reset after being run.

I've tested the depreciation formula outside of the loop, and it works fine. I've also verified that the i counter is getting incremented properly by the loop.

Anyone got a clue as to where I'm going wrong? I've even tried creating a custom function using private variables, but it made no difference.

Thanks in advance!

2012-04-04 16:36
by Hoppertron


3

Typos:

AssetValue = AssetValue - ((AssetValue/100)*DepreciationPercentage);
           ^--- equality test
i = i + 1
  ^-- ditto

it should be := to do an assignment.

2012-04-04 16:38
by Marc B
I like the ^-- bit - craig 2012-04-04 17:46
Argh, you're kidding me! I thought those were only for declarations. Glad you cleared that up for me though, as I was wondering how it would know the difference between [=] and [==] - Hoppertron 2012-04-04 20:15
So many languages, so many ways of doing things. Crystal syntax gives me a headache most days. The declaration bit is 'NumberVar' to specify the variable's type. But it's always := for assignment, regardless of where you're doing it - Marc B 2012-04-04 20:33
Ads