Can you build an expression inside of a custom code function in Report Builder? - vb.net

I need to provide my RDL files to teammates so that they can make minor customizations for each client. One of the ways I thought I might improve the efficiency is if I could build more complex expressions inside of custom code functions so that they can input some simple arguments and have the function handle the "heavy lifting" of adjusting the expression accordingly.
This is a very simple example, and not one I would take this step for, but I thought it the easiest place to start figuring out if I can make this work. For instance, in a tablix we want a count returned based on a value where the value is customized per client (and isn't a parameter).
=Count(iif(trim(Fields!Category.Value)="OPTIONA",1,nothing))
Is there a way I could build a function so that my teammates would just need to enter the following?
=Code.CustomFunction("OPTIONA")
My understanding is that the custom code in Report Builder can't query datasets, or at least not in the way that an expression within a tablix would be able to. I've built custom functions that work with the results of an expression added as the argument, but I can't seem to wrap my head around if there's a way to construct an expression within a custom function and pass it back to the expression.
For instance:
Public Function CustomFunction(field As String) As String
Dim customExpression As String = "Count(iif(trim(Fields!Category.Value)=" & field & ",1,nothing))"
Return customExpression
End Function
As expected, this just returns a string with the text of the expression, but not an executed expression. Is what I'm trying to achieve possible with Report Builder?
Or, as an alternative approach, can I somehow place variables at the beginning of an expression that are used later so that anyone else working on the expression just needs to worry about the beginning? Essentially create multiple custom functions and call them later on?
=Code.CustomFunction("OPTIONA",Count(iif(trim(Fields!Category.Value)=Code.CustFunc01(),1,nothing)))
Honestly not sure how I would go about building the functions themselves from here.

You can use a function instead of the related field. The function takes the field string as an argument and the filter string for which will increase the counter. Finally it returns the original field value
Private Dim Counter As Integer
Public Function SetCounter( Expr As String, Filter As String) As String
If Expr = Filter Then Counter = Counter + 1
Return Expr
End Function
Public Function GetCounter( ) As Integer
Return Counter
End Function
For the field value you can use the following expression (yellow color)
=Code.SetCounter( Fields!MyString.Value,"OPTION A")
To get the counter value you can either use the following expression calling a function (orange color)
= Code.GetCounter()
Or make the variable public and use Code.Counter as the expression

Related

VB.NET + LINQ: Save in a single class attribute the result of querying two columns from different tables

I have two tables: Estructura (with two fields I want: descripcion_morologica and interpretacion) and estrato (with two fields I want: descripcion_larga and interpretacion_explic). On the other side I hace a class in VS with the attributes descripcion and interpretacion. Both tables have a common field called id_excavacion, which I pass to the method as a parameter.
What I'm trying to achieve is to make a query which saves in the "descripcion" attribute the results of t1.descripcion_morfologica and t2.descripcion_larga and also saves in "interpretacion" the results of t1.interpretacion and t2.interpretacion_explic.
So far, I've tried like this:
'GET: api/Excavacions/ListadoUE/5
<Route("api/Excavacions/ListadoUE/{idExcavacion}")>
Function GetListadoUEs(ByVal idExcavacion As Integer) As IQueryable(Of ListadoUEDto)
Dim listado =
From estru In db.Estructura
Join estra In db.Estrato On estra.id_excavacion Equals estru.id_excavacion
Where estru.id_excavacion = idExcavacion
Select New ListadoUEDto With {
.Descripcion = estru.descripcion_morfologica And estra.descripcion_larga,
.Interpretacion = estru.interpretacion And estra.interpretacion_explic
}
End Function
But I only get null, despite the id I pass actually exists.
Thanks a lot in advance!!
You should almost certainly not be using And there. That is a Boolean operator, for combining True and False values. As is always the case, if you want to concatenate Strings then you use &, which is the string concatenation operator.
You should have Option Strict On and then the compiler would have warned you that you were doing something that doesn't make sense. You should turn it On in the project properties and also in the VS options, so it is On by default for future projects. That will force you to put more thought into what data types you use and, therefore, make you write better code.

Format - Expected Array

I keep getting an error when I try to format this number. I've done it in VBA before and I tried to change the SOPID to a variant, a string, and an integer.
Dim SOPID As Integer
SOPID = DMax("file_id", "tblSOP") + 1
'Output test data
MsgBox (format(SOPID, "000"))
I have no idea what I am doing wrong.
Assuming the code was pasted directly from your IDE, the casing of format is suspicious; that would be Format, unless there's a format variable or function that's in-scope, ...and that's being invoked here.
Look for a public (could be implicitly Public, or if it's in the same module then it could also be Private) format function procedure that expects an array argument: that's very likely what's being invoked here.
Rubberduck (free, open-source; I manage this project) can help you easily identify exactly what's being invoked and an inspection would tell you about any shadowed declarations, but to be sure you can fully-qualify the function call to avoid inadvertently invoking another function that's in scope:
MsgBox VBA.Strings.Format$(SOPID, "000")
Note that there are no parentheses around the argument list of a parameterized procedure call in VBA; the parentheses you have there are surrounding the first argument and making the expression be evaluated as a value that is then passed to the invoked function: this isn't necessary.
Also note the $: the VBA.Strings.Format function takes and returns a Variant; VBA.Strings.Format$ takes and returns a String. If you aren't dealing with any Null values (an Integer couldn't be Null), consider using the String-returning alias.

VB.net String Reverse Property different based on Env

I am debugging an application that runs on a server and users will access the application on another server. The application uses encryption and as part of the key, I am using the String.Reverse property.
Dim Mystring As String = "123abc"
Dim reverse = String.Format("{0},{1}", Mystring.Reverse)
The string reverse is different when I run it from one machine (RDP/Citrix Environment ASP.NET 4.6.1). The value is:
System.Linq.Enumerable+<ReverseIterator>d__a2`1[System.Char]
The same string, but ran from another machine (RPD non-Citrix Environment ASP.NET 4.5.2). The value of reverse is:
System.Linq.Enumerable+<ReverseIterator>d__73`1[System.Char]
Why are the values different in the different environments?
Look at this line first:
Dim reverse = String.Format("{0},{1}", Mystring.Reverse)
Specifically, this expression:
Mystring.Reverse
Reverse is a function, not a property, but it's missing the parentheses (). The trick here is the String.Format() method accepts the base Object type as an argument, and compiler is able to treat the MyString.Reverse expression as a delegate type that is convertible to object. The values you see in your output are the result of calling .ToString() on that function delegate. It's the type name for the function, rather than anything to do with the value of your MyString object. Since that type is dynamically and randomly generated at runtime, you'll see different values not only on different platforms, but different runs on the same computer.
In the VB6 era, it was normal to call methods without the parentheses. In the .Net world, always use parentheses when you call a method.
What you want is this:
Dim reverse As String = String.Format("{0},{1}", Mystring.Reverse())
Even here, you're missing the second argument to match the format string. I doubt you'll get the result you expect.
Finally, reversing a string as the key seems very wrong when it comes to encryption. You are using a real cyrptogrpahic algorithm from the System.Security.Cryptography library, right? Right!?
You are not outputting the value of the reversed String but the name of the type used to perform the reversal. That type is dynamically created and randomly named. The "d" in those two names means "dynamic" and the "a2" and "73" parts are random.
Basically, what you perceive to be an issue is not an issue. The problem is that you're not actually creating a String from the reversed output. You say "String.Reverse property but that is NOT a property. It is a method and it is not a member of the String class but rather an extension method on the IEnumerable(Of T) interface. You are treating your String as an enumerable list of Char values and reversing that. If you want a String from that then you need to create one, i.e.
MyReversedString = New String(Mystring.Reverse().ToArray())
That will push the contents of your iterator into an array and then create a new String object from that array.

Is there a way to use default arguments that are the result of a function call in VB.NET?

I have a whole slew of database access functions which assume a particular connection string. Within my application I call
myList = DB_MyTable.GetTableItems()
and within GetTableItems() I have something like
Dim connection As SqlConnection = MyDB.GetConnection
So the connection string is in one place in the code, and I call a method to get it.
What I'm running into now is I want to reuse the same database functions, but with a different connection string. I can rewrite all of the functions like DB_MyTable.GetTableItems() easily because they're generated from a script, but within the main application code I'll need to take care of every function call that now needs to know what connection string I want to use.
I tried changing the arguments to GetTableItems() like this:
Public Shared Function GetTableItems(Optional ByVal useThisString as String = MyDB.GetConnection) As List(Of MyItems)
in hopes of being able to pass in, by default, the string I'm already using in most of the code, but I got an error saying that the default value had to be a constant expression. This would mean peppering a specific connection string everywhere, which I don't want to do.
Is there a way to accomplish what I'm after, or do I need to make the connection string a required argument and change all of the calls in my application to match the new signature?
Thanks as always!
Can you make your default value an empty string? Then, in your functions, if the useThisString variable is blank, then use default, else use the one you passed in? A littler dirtier, but just barely.

VB.NET logical expression evaluator

I need to test a logical expression held in a string to see if it evaluate to TRUE or FALSE.(the strig is built dynamically)
For example the resulting string may contain "'dog'<'cat' OR (1>4 AND 4<6)". There are no variables in the string, it will logically evaluate. It will only contain simple operators = > < >< >= <= and AND , OR and Open and Close Brackets, string constants and numbers. (converted to correct syntax && || etc.)
I currently acheive this by creating a jscipt function and compiling it into a .dll. I then reference the .dll in my VB.NET project.
class ExpressionEvaluator
{
function Evaluate(Expression : String)
{
return eval(Expression);
}
}
Is there a simpler method using built in .NET functions or Lamdba expressions.
I tried the demo out for this project and you might like it over you current method of evaluating. Note, it doesn't use lamdba expressions or any build it .NET methods.
http://web1.codeproject.com/KB/vb/expression_evaluator.aspx?msg=1151870
try out: http://www.codeproject.com/KB/cs/ExpressionEval.aspx
More guidance :http://www.thefreakparade.com/2008/07/evaluating-expressions-at-runtime-in-net-c/
Good one: http://flee.codeplex.com/
Boolean Example which you are looking for : http://flee.codeplex.com/wikipage?title=BooleanExpression&referringTitle=Examples (ignore the variable adding part as you are not looking for variable)