Variant datatype and Records

rocatisrocatis Member Posts: 163
I want to create a function that takes a variety of parameters and handles several types of tables. My initial investigations suggest to me that this is not possible.
OBJECT Codeunit 66666 Variant Test
{
  OBJECT-PROPERTIES
  {
  }
  PROPERTIES
  {
    OnRun=BEGIN
            VariantFunction(1234);
            VariantFunction('Text');
            VariantFunction(Customer);
            VariantFunction(Vendor);
          END;

  }
  CODE
  {
    VAR
      Customer@1000000000 : Record 18;
      Vendor@1000000001 : Record 23;

    PROCEDURE VariantFunction@1000000000(_variant@1000000000 : Variant);
    VAR
      recRef@1000000001 : RecordRef;
    BEGIN
      IF _variant.ISRECORD THEN BEGIN
        recRef.GETTABLE(_variant);
        MESSAGE('%1',recRef.NUMBER);
      END ELSE
        MESSAGE('%1',_variant);
    END;

    BEGIN
    END.
  }
}
This fails when executing recRef.GETTABLE(_variant) which means that there is no way of knowing what kind of record was passed to the function. In other words, it really doesn't make sense to use the Variant datatype to store Record datatypes.

Okay, so I figured I'd use a workaround and pass a RecordRef datatype instead of a Record datatype. That kind of works (I can figure out which table was passed), but there's a catch: there is no Variant.ISRECORDREF function so now I have no way of knowing if it was indeed a RecordRef that was passed. I could of course assume that it's a RecordRef if it isn't any of the other datatypes.

Except, I wouldn't be able to handle FieldRef and KeyRef datatypes as there are no Variant.ISFIELDREF and Variant.ISKEYREF functions either ](*,)

Conclusion: the datatype Variant would be awesome if it worked. #-o
Brian Rocatis
Senior NAV Developer
Elbek & Vejrup

Comments

  • rdebathrdebath Member Posts: 383
    :lol: Sorry I sympathise, I do. Maybe you'l like this one too, it gives a wonderfully cryptic error message. :mrgreen:
    OBJECT Report 99929 Fd Up Variant
    {
      OBJECT-PROPERTIES
      {
        Date=23/07/08;
        Time=11:19:45;
        Modified=Yes;
        Version List=;
      }
      PROPERTIES
      {
        ProcessingOnly=Yes;
        OnPreReport=BEGIN
                      V := '<A....................................';
    
                      FD.TEXTMODE(TRUE);
                      FD.CREATETEMPFILE;
                      FD.WRITE(V);
                      FD.SEEK := 0;
                      FD.READ(V);
                      FD.CLOSE;
    
                      "What's V's Type";
                      V := 'jjjjjjjjjjjjj';
                    END;
    
      }
      DATAITEMS
      {
      }
      REQUESTFORM
      {
        PROPERTIES
        {
          Width=9020;
          Height=3410;
        }
        CONTROLS
        {
        }
      }
      CODE
      {
        VAR
          FD@1000000000 : File;
          V@1000000001 : Variant;
    
        PROCEDURE "What's V's Type"@1000000005();
        BEGIN
          IF V.ISRECORD THEN MESSAGE('V.ISRECORD')
          ELSE IF V.ISFILE THEN MESSAGE('V.ISFILE')
          ELSE IF V.ISACTION THEN MESSAGE('V.ISACTION')
          ELSE IF V.ISCODEUNIT THEN MESSAGE('V.ISCODEUNIT')
          ELSE IF V.ISAUTOMATION THEN MESSAGE('V.ISAUTOMATION')
          ELSE IF V.ISBOOLEAN THEN MESSAGE('V.ISBOOLEAN')
          ELSE IF V.ISOPTION THEN MESSAGE('V.ISOPTION')
          ELSE IF V.ISINTEGER THEN MESSAGE('V.ISINTEGER')
          ELSE IF V.ISDECIMAL THEN MESSAGE('V.ISDECIMAL')
          ELSE IF V.ISCHAR THEN MESSAGE('V.ISCHAR')
          ELSE IF V.ISTEXT THEN MESSAGE('V.ISTEXT')
          ELSE IF V.ISCODE THEN MESSAGE('V.ISCODE')
          ELSE IF V.ISDATE THEN MESSAGE('V.ISDATE')
          ELSE IF V.ISTIME THEN MESSAGE('V.ISTIME')
          ELSE IF V.ISBINARY THEN MESSAGE('V.ISBINARY')
          ELSE IF V.ISDATEFORMULA THEN MESSAGE('V.ISDATEFORMULA')
          ELSE IF V.ISTRANSACTIONTYPE THEN MESSAGE('V.ISTRANSACTIONTYPE')
          ELSE IF V.ISINSTREAM THEN MESSAGE('V.ISINSTREAM')
          ELSE IF V.ISOUTSTREAM THEN MESSAGE('V.ISOUTSTREAM')
          ELSE MESSAGE('V.NONEOFTHEABOVE');
        END;
    
        BEGIN
        END.
      }
    }
    
  • rocatisrocatis Member Posts: 163
    rdebath wrote:
    :lol: Sorry I sympathise, I do. Maybe you'l like this one too, it gives a wonderfully cryptic error message.
    Man, that's seriously f***** up. First I thought that the problem was caused by the initial assignment, as NAV really has no way of knowing what datatype is assigned to the variant (ever heard of typecasting, Microsoft?), but even if V is assigned the value by means of a variable with datatype text, NAV goes all ballistic.

    It is possible to get your code working with a minor workaround, however. If you change the READ statement to FD.READ(V2) (where V2=Variant) and then do a V := V2, \:D/

    Still doesn't make much sense though. I think I'll maintain my previous conclusion: the datatype Variant would be awesome if it worked #-o
    Brian Rocatis
    Senior NAV Developer
    Elbek & Vejrup
  • kinekine Member Posts: 12,562
    Yes, for me the Variant datatype is now finished from 60%, making it nearly unusable...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
Sign In or Register to comment.