Page 1 of 1
Custom Functions in the dictionary
Posted: Mon Jan 05, 2009 2:26 pm
by johnham
I have a couple proprietary functions that I would like my customers to have access to within the designer. I know there is a way to reference external assemblies and .NET code from the report designer but is there a way for me to add a custom code fragment and register it as a function in the dictionary's function list so it is easily available to my customers?
Custom Functions in the dictionary
Posted: Mon Jan 05, 2009 6:36 pm
by johnham
I figured it out! Here's a quick rundown for those needing a little reference.
All you gotta do is come up with a class with a bunch of static methods that correspond to the functions you want to display in the function list. Then you do this:
Code: Select all
///////////
// Data
///////////
#region Data
string Category;
string[] ParamNames;
Type[] ParamTypes;
string[] ParamDescrips;
#endregion
///////////
// Code
///////////
#region Code
Category = "PRM Functions";
ParamNames = new string[2];
ParamTypes = new Type[2];
ParamDescrips = new string[2];
ParamNames[0] = "PRMDate";
ParamDescrips[0] = "Integer value representing a PRM Date to be converted";
ParamTypes[0] = typeof(int);
ParamNames[1] = "PRMTime";
ParamDescrips[1] = "Integer value representing a PRM Time to be converted.";
ParamTypes[1] = typeof(int);
StiFunctions.AddFunction(Category, "ToSQLDateTime", "ToSQLDateTime", "Converts from PRM Standard Date and Time to an SQL DateTime",
typeof(POSitiveSoftware.Common), typeof(DateTime), "Returns an SQL DateTime representing the PRM Date and Time provided.",
ParamTypes, ParamNames, ParamDescrips);
#endregion
The last function call does all the magic. You give it a category name and function name, types for return and parameter types and labels. You also give it a "function type" which is the name of the static class that contains the function that corresponds to the function name (ex. POSitiveSoftware.Common).
I would consult the docs for some of this but I find that the product changes so quickly that the docs usually are quite outdated so I come to the forums to get my questions answered. I hope you guys don't mind. This one was easy for me to figure out as I have another piece of code which adds predefined variables to the dictionary.
Thanks again stimulsoft. You guys are always 10 steps ahead of me.
Custom Functions in the dictionary
Posted: Mon Jan 05, 2009 8:52 pm
by Vital
Hello Johnham,
Also you can register xml file with custom functions:
Code: Select all
StiFunctionsXmlParser.LoadFunctionsFromXml(typeof(StiReport).Assembly, "Stimulsoft.Report.Dictionary.FunctionsDate.xml");
Thank you.
Custom Functions in the dictionary
Posted: Tue Jan 06, 2009 6:05 pm
by johnham
One more question.
I added my custom function using my code fragment above. This worked when I created a variable in the dictionary and had it simply run my function against some dummy values.
However, today I went into the datasource editor. I added a parameter to my datasource and then used the expression editor to browse for my function in the function library. I double clicked on it and filled in the details. It looked something like so:
When I tried to preview my test report, it gave me "c:\Users\jhamilton\AppData\Local\Temp\assdilax.0.cs(687,69) : error CS0103: The name 'ToPRMDate' does not exist in the current context"
To fix this I had to go in and edit the parameter again and fully qualify the function name. It looks like this:
Code: Select all
POSitiveSoftware.Common.ToPRMDate(dtpEndDate.Value)
This is a fine solution for me but when my customers go to use my custom functions from the function library I don't know how they will know the namespace and classname to add. It also concerns me that it worked fine as an expression in the variables but not in the datasource parameters.
Do I need to register my assembly/class somewhere or is this a problem that needs to be fixed?
Custom Functions in the dictionary
Posted: Wed Jan 07, 2009 2:35 am
by mace242
Vital wrote:Hello Johnham,
Also you can register xml file with custom functions:
Code: Select all
StiFunctionsXmlParser.LoadFunctionsFromXml(typeof(StiReport).Assembly, "Stimulsoft.Report.Dictionary.FunctionsDate.xml");
Thank you.
Is there any way to make this work with functions that I have added to a report file? We have a standard report template that our customers use and there are several static functions added to this that they can call. using the load from xml or the code method I cannot add these to the function list. I can only add functions in other classes.
Custom Functions in the dictionary
Posted: Wed Jan 07, 2009 1:09 pm
by Vital
Hello Hohnham,
johnham wrote:One more question.
I added my custom function using my code fragment above. This worked when I created a variable in the dictionary and had it simply run my function against some dummy values.
However, today I went into the datasource editor. I added a parameter to my datasource and then used the expression editor to browse for my function in the function library. I double clicked on it and filled in the details. It looked something like so:
When I tried to preview my test report, it gave me "c:\Users\jhamilton\AppData\Local\Temp\assdilax.0.cs(687,69) : error CS0103: The name 'ToPRMDate' does not exist in the current context"
To fix this I had to go in and edit the parameter again and fully qualify the function name. It looks like this:
Code: Select all
POSitiveSoftware.Common.ToPRMDate(dtpEndDate.Value)
This is a fine solution for me but when my customers go to use my custom functions from the function library I don't know how they will know the namespace and classname to add. It also concerns me that it worked fine as an expression in the variables but not in the datasource parameters.
Do I need to register my assembly/class somewhere or is this a problem that needs to be fixed?
Its a bug. We don't check parameter value for functions. Please check next build.
Thank you.
Custom Functions in the dictionary
Posted: Wed Jan 07, 2009 1:13 pm
by Vital
Hello,
mace242 wrote:Vital wrote:Hello Johnham,
Also you can register xml file with custom functions:
Code: Select all
StiFunctionsXmlParser.LoadFunctionsFromXml(typeof(StiReport).Assembly, "Stimulsoft.Report.Dictionary.FunctionsDate.xml");
Thank you.
Is there any way to make this work with functions that I have added to a report file? We have a standard report template that our customers use and there are several static functions added to this that they can call. using the load from xml or the code method I cannot add these to the function list. I can only add functions in other classes.
You need provide assembly which contain report functions together with your reports. Also you can place code of your function directly in your report code.
Thank you.
Custom Functions in the dictionary
Posted: Sun Jan 17, 2010 11:06 pm
by kiwitarget
does the methods in the custom function class have to be ' static'? then how about Unit Test?
Custom Functions in the dictionary
Posted: Mon Jan 18, 2010 8:02 am
by Edward
Hi
Yes, the method must be static, there is no other way for it to be registered.
Thank you.