CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Guest

    How to set values in VARIANT data type

    Hai,

    I am using MS Chart control in VC. To assign values, SetChartData(const VARIANT &) function has to be called with VARIANT data type as parameter. I want to pass array of integers.

    so i have written code as follows

    short num[5]={20,10,30,40,25};
    VARIANT vari;
    vari.vt = VT_I2|VT_BYREF;
    vari.piVal = num;
    SetChartData(vari);

    But i am getting run time exception error "bad function argument".

    can any one help me please
    thanks in advance
    chandru


  2. #2
    Join Date
    May 1999
    Posts
    318

    Re: How to set values in VARIANT data type

    The method you use is correct if the function wait for a pointer to integer and not for an array.

    For an array you must use the type VT_ARRAY|VT_I2 and the value must be a pointer to SAFEARRAY (parray).
    Look for SAFEARRAY structure in guide lines.

    I hope this can help.








  3. #3
    Guest

    Re: How to set values in VARIANT data type

    Hai,
    thanks for your reply. But now i am finding difficulty in setting values for SAFEARRAY type. If you have any example code, can you please help me. for eg with short num[5]={100,200,150,300,100};

    thanks
    Chandru


  4. #4
    Join Date
    May 1999
    Posts
    318

    Re: How to set values in VARIANT data type

    OK
    Here is an example for your array :


    short num[5]={100,200,150,300,100};
    SAFEARRAY array;
    array.cbElements = sizeof(short);
    array.cDims = 1;
    array.rgsabound[0].cElements = 5;
    array.pvData = (void *)num;




    OOPS IF you receive a precedent response the next method ONLY works with a byte array and not a short integer array. Use the first method for short integer array (above).

    THE FOLLOWINGS METHOD WORKS ONLY FOR BYTES ARRAY.
    (but could be adapted for short integer array by changing 'vt' field and the parray->cbElements and the parray->rgsabound[0].cElements as above ).

    Now to pass a array to a variant a good practice is to use CByteArray and COleVariant classes.

    Here is an example :


    #define ARRAYDIM 5
    short num[ARRAYDIM]={100,200,150,300,100};
    CByteArray array;
    char *ptr;
    int i;
    COleVariant variant;
    // fill array
    ptr =(char *)num;
    array.SetSize(ARRAYDIM*sizeof(short));
    for ( i=0; i<ARRAYDIM; i++ )
    array.SetAt(i,*ptr++);
    // Operator = of COleVariant
    variant=array;





    I hop this could help.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured