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

Stimulsoft Reports.NET discussion
Post Reply
sbd_jeff
Posts: 27
Joined: Fri Nov 08, 2013 9:39 pm

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

Post 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.
Attachments
STIEXAMPLE2.zip
Sample Report with schema+data
(3.24 KiB) Downloaded 209 times
sbd_jeff
Posts: 27
Joined: Fri Nov 08, 2013 9:39 pm

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

Post 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!
HighAley
Posts: 8430
Joined: Wed Jun 08, 2011 7:40 am
Location: Stimulsoft Office

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

Post 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.
Post Reply