Page 1 of 1

Switch() in Expression versus Switch() in a variable

Posted: Fri Apr 04, 2014 2:57 pm
by sbd_jeff
Hi Stimulsoft,
I am trying to assign a value to a variable ("TaxCharge") using an Expression. The value is conditional, so I am using Switch(Condition,value...).

My code is:

Code: Select all

Switch(Products.Taxable > 0,Products.UnitPrice * Products.UnitsInStock * TaxRate,
Products.Taxable == 0,0)
When I attempt this in a variable, I get a compilation error with this output:
; expected

Code: Select all

public virtual decimal TaxCharge
        {
            get
            {
                return Stimulsoft.Report.Dictionary.StiFunctionsProgrammingShortcut.Switch(Products.Taxable > 0.Products.UnitPrice * Products.UnitsInStock * TaxRate.
Products.Taxable == 0.0)m;
            }
        }
If I put the same in a text field as an expression, it will output the desired result:

Code: Select all

{Switch(Products.Taxable > 0,Products.UnitPrice * Products.UnitsInStock * TaxRate,
Products.Taxable == 0,0)}
Is it possible to do conditional calculations in the Switch function within a variable?
Am I doing something wrong?

I have attached an example with the expression field and the non-compiling "TaxCharge" variable. If you clear out "TaxCharge" you'll see the expression field works.

Thanks.

Re: Switch() in Expression versus Switch() in a variable

Posted: Fri Apr 04, 2014 6:05 pm
by sbd_jeff
Just an update,
I stumbled upon the error above because I was attempting to summarize using variables.
A coworker pointed out that I can achieve the desired result by using expression fields and summarizing the expression.

To solve my issue above, I simply needed the code below in an expression:

Code: Select all

{Sum(Switch(Products.Taxable > 0,Products.UnitPrice * Products.UnitsInStock * TaxRate,
Products.Taxable == 0,0))}
Who knew?

I'm still curious as to why it doesn't work in a variable, but either way, I can move forward.

Thanks!

Re: Switch() in Expression versus Switch() in a variable

Posted: Mon Apr 07, 2014 5:56 am
by HighAley
Hello.

The Init by option of the variable in set to Value. In this case it could be init by constant value only.

Thank you.