Using variant parameter with var check

Cem_KaraerCem_Karaer Member Posts: 281
Hello,

I want to create a generic function that will accept any type of variable as a first parameter and a text variable parameter. The text parameter will be tried to be evaluated to the type of the first parameter.

For example:
TestFunction(DateVariable,'13.02.2012')

DateVariable should be set to 13022012D

TestFunction(IntegerVariable,'345')

IntegerVariable should be set to 345

ect..

For this I defined the first parameter as variant and worked as desired. But because it is not a var variable, I cannot get the evaluated value outside TestFunction. So I defined it as var, but NAV wants only variant type in this case.

Is it a solvable problem or restrictions of NAV prevent such a generic functionality?
Cem Karaer @ Pargesoft
Dynamics NAV Developer since 2005

Comments

  • krikikriki Member, Moderator Posts: 9,112
    Something like this:
    CASE TestFunction('13.45') of
      'DECIMAL': SomeDecimal := ResultFunctionDecimal();
      'INTEGER': SomeInteger := ResultFunctionInteger();
      ...
    END;
    

    In TestFunction you have something like
    FUNCTION TestFunction(...)
    BEGIN
      ...
      DecimalValue := ....; //DecimalValue is a GLOBAL variable
    END;
    
    FUNCTION ResultFunctionDecimal()
    BEGIN
      EXIT(DecimalValue);
    END;
    
    Regards,Alain Krikilion
    No PM,please use the forum. || May the <SOLVED>-attribute be in your title!


  • MarkHamblinMarkHamblin Member Posts: 118
    Could use the "original" variant data type - a string ;)

    Something like:
    TestFunction(MyVar : Variant) : Text[200]
    BEGIN
      ... do stuff with variant ...
    
      EXIT(FORMAT(MyVar));
    END;
    
    // call function like:
    EVALUATE(MyInt, TestFunction(1234));
    EVALUATE(MyText, TestFunction('Some Text'));
    
    // or
    MyInt := 1234;
    EVALUATE(MyInt, TestFunction(MyInt));
    ... etc.
    
    
  • David_SingletonDavid_Singleton Member Posts: 5,479
    Just use a FilterField in a table of the correct type, it will correctly evaluate text to the required format.
    David Singleton
Sign In or Register to comment.