Function with optional parameters

Function with optional parameters - Messages

#1 Posted: 7/16/2010 10:17:16 AM
Edward Ulle

Edward Ulle

20 likes in 182 posts.

Group: Moderator

How to create a plugin function that accepts multiple parameters. For example:

Es:=29000000 psi

x:=myfunc(a,b,28000000psi,...) ' Here I'm overriding the default with 28000000psi

or

x:=myfunc(a,b,...)

where the default third parameter is Es from SMath sheet and there are other optional parameters following the Es position and "..." means any number of following parameters.

So to the user it could be:

x:=myfunc(a,b )
x:=myfunc(a,b,c,d,e,f,g,h,i,j,k) and so on

You do it with augment(...)

ArgumentsCount puts empty parameter symbols.

By the way. The way I get Es is as follows. Let me know if there is an easier way.

Dim e As New Term("Es", TermType.Operand, 0)
arge.Add(e)
arg1 = Decision.Preprocessing(arge.ToArray, store)
Ed
#2 Posted: 7/16/2010 5:36:22 PM
Andrey Ivashov

Andrey Ivashov

2270 likes in 3734 posts.

Group: Super Administrator

Wrote

You do it with augment(...)


I did it also with concat(..) function. Please, check its code to see the logic:
            else if (root.Text == "concat"
            {
                string res = "";
                for (int i = 0; i < root.ChildCount; i++)
                {
                    args[i] = Decision.SymbolicCalculation(args[i], ref store);
                    if (!IsTextTerm(args[i]))
                        throw new Exception(GlobalParams.StringsErrors[(int)Error.ArgumentMustBeString]);
                    res += args[i][0].Text.Substring(1, args[i][0].Text.Length - 2);
                }
                result = new Term[] { new Term("\"" + res + "\"", TermType.Operand, 0) };
            }
            else if (root.Text == "findstr" && root.ChildCount == 2)
            {

Wrote

The way I get Es is as follows. Let me know if there is an easier way.


I advise you to discover store object - it is context of current evaluation operation. Check its Shared field to see all information about worksheet definitions.

Regards.
#3 Posted: 7/16/2010 5:45:42 PM
maweilian

maweilian

5 likes in 103 posts.

Group: User

Andrey,

This is good. I hope that we can see more discussion like this on plugin development. I am looking forward to learning from you and others who know alot more than myself.

Sometime in the future, I would suggest that you might want to consider a forum section that would be reserved for discussions about developing plugins. Those threads that have been started already (such as this one) could be moved to that section.

Just a suggestion.

Will
Will Massie Mechanical Engineer Oregon, USA
#4 Posted: 7/16/2010 5:52:09 PM
Andrey Ivashov

Andrey Ivashov

2270 likes in 3734 posts.

Group: Super Administrator

Hello Will. You're absolutely right, it will be good if separate forum section will be created for plugin development related discussions when we will have enough topics about it.

Regards, Andrey Ivashov.
#5 Posted: 7/16/2010 7:54:51 PM
Edward Ulle

Edward Ulle

20 likes in 182 posts.

Group: Moderator

I was concerned about the ArgumentsCount of TermInfo but needlessly.

Public Function ExpressionEvaluation(ByVal root As SMath.Manager.Term, ByVal args()() As SMath.Manager.Term, 
                                     ByRef store As SMath.Manager.Store, ByRef result() As SMath.Manager.Term) 
                                 As Boolean Implements SMath.Manager.IPluginLowLevelEvaluation.ExpressionEvaluation
    Dim i as Integer
    If root.Type = TermType.Function And root.Text = "test" Then
        For i = args.GetLowerBound(0) To args.GetUpperBound(0)
            ' Do something with args(i)
        Next
        ' If you run out of input parameters the rest are defaults
        Return (True)
    End If
    Return False
End Function
Ed
  • New Posts New Posts
  • No New Posts No New Posts