Click to See Complete Forum and Search --> : How to set values in VARIANT data type
April 25th, 1999, 11:45 PM
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
eric33
April 26th, 1999, 02:11 AM
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.
April 26th, 1999, 04:20 AM
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
eric33
April 26th, 1999, 06:20 AM
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.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.