Feature Request: if/elseif/else and/or switch statement support in expressions

1 Answer 135 Views
Expressions Report Designer (standalone)
Ryan
Top achievements
Rank 1
Ryan asked on 29 Dec 2021, 02:57 PM

I am often finding that I need to display different text based on the values of multiple parameters. For example consider having CODE and DESC fields.

  • When both are non-null you want to display "CODE : DESC" (Note the separator)
  • When only DESC is null you want to display "CODE : ---"
  • When only CODE is null you want to display "--- : DESC"
  • When both are null you want to display "Unavailable"

While we can perform these checks with nested IIFs or nested ternaries it is very clumsy. If/elseif/else statements, or better yet, switch expressions would go a long way to making more readable expressions.

 


1 Answer, 1 is accepted

Sort by
0
Milen
Telerik team
answered on 05 Jan 2022, 01:15 PM

Hi Ryan,

Thank you for your input. I completely agree that supporting if/else would give more expressibility to the expression engine. However, without supporting assignment (which means state) it would not yield shorter expressions. And we do not want to basically make a programming language, but something more similar to the Excel functions. To demonstrate my statement here is the solution of the sample task you described:

First with if/else without a state (C#):

if(CODE != null)
{
	if(DESC != null)
	{
		return $"{CODE} : {DESC}";
	}
	else
	{
		return $"{CODE} : ---";
	}
}
else
{
	if(DESC != null)
	{
		return $"--- : {DESC}";
	}
	else
	{
		return "Unavailable";
	}
}

With state, it would be much shorter (C#):

if(DESC == null && CODE == null)
{
	return "Unavailable";
}
var d = DESC ?? "---";
var c = CODE ?? "---";
return $"{c} : {d}";

And with the expressions as they are today:

= IIF(CODE IS NULL And DESC IS NULL, "Unavailable", IsNull(CODE, "---") + " : " + IsNull(DESC, "---"))

or the shorter notation:

= IIF(CODE IS NULL And DESC IS NULL, "Unavailable", (CODE ?? "---") + " : " + (DESC ?? "---"))

References here and here.

That said, if you need the full expressibility of if/else statements and programming state I would recommend introducing a user-defined function. What we need to improve here is to be able to write C# code in the report designer (report model) itself that gets compiled and executed at runtime. If you find this valuable please add it to the product feedback portal.

Additionally, having a switch-like functionality in the expression engine would be beneficial. I am wondering what function-like syntax would not be confusing for the users?!

Regards,
Milen
Progress Telerik

Brand new Telerik Reporting course in Virtual Classroom - the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products. Check it out at https://learn.telerik.com/.
Ryan
Top achievements
Rank 1
commented on 07 Jan 2022, 02:12 PM | edited

Hello Milen,

The ability to write C# code in the report, or any other established language, would be immensely beneficial. We are in the process of converting 20+ Crystal Reports documents to Telerik Reporting and while on the whole Telerik Reporting is a better experience one thing we are sorely missing is the ability write small code snippets with or without state.

Readability is so important for understanding how things work and expressions can sometimes get complicated pretty quickly. Where applicable we move logic to the SQL in our data source, or in calculated fields, but that only shifts the problem elsewhere.

I will add this request to the product feedback portal.

---

For switch like functionality, if you are modeling after excel, something like excel's SWITCH or IFS functions would be helpful in increasing readability. The former is great for simpler cases where you just want to check against a few literal values and return different results, the latter is great for evaluating against different expressions.

V/r,

Ryan

 

 

 

 

Milen
Telerik team
commented on 18 Jan 2022, 09:50 AM

Hi Ryan,

Thank you for the ideas. Those match my thinking as well for a suitable syntax of the functions. Yes, compiled code that is written inline would be much more convenient although it would bring security considerations. It would be great if you log those as two separate items in the product feedback portal to track the interest from other users as well. 

Regards

Tags
Expressions Report Designer (standalone)
Asked by
Ryan
Top achievements
Rank 1
Answers by
Milen
Telerik team
Share this question
or