• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1644
  • Last Modified:

using COleSafeArray

Hi

I am trying to use the COleSafeArray with the following code

COleSafeArray sa;

sa.CreateOneDim(VT_R8, 10);

long l;

for(l = 0; l < 10; l++)
{
      VARIANT val;
      VariantInit(&val);

      val.vt = VT_R8;
      val.dblVal = 33.45;
      sa.PutElement(&l, &val);
}

CString s;
CString ss;

for(l = 0; l < 10; l++)
{
      VARIANT val;
      VariantInit(&val);
      sa.GetElement(&l, &val);

      switch(val.vt)
                {
                case VT_R8:
                       TRACE("DOUBLE %2.2f\n", val.dblVal);
                       break;

                case VT_BSTR:
                       TRACE("BSTR %s\n",(CString)val.bstrVal);
                       break;

                case VT_R4:
              TRACE("FLOAT %2.2f\n",  val.fltVal);
                        break;

                case VT_EMPTY:
                       TRACE("<empty>\n");
                        break;
                }
}

The trace output is

DOUBLE -92559631349317831000000000000000000000000000000000000000000000.00
DOUBLE -92559631349317831000000000000000000000000000000000000000000000.00
DOUBLE -92559631349317831000000000000000000000000000000000000000000000.00
DOUBLE -92559631349317831000000000000000000000000000000000000000000000.00
DOUBLE -92559631349317831000000000000000000000000000000000000000000000.00
DOUBLE -92559631349317831000000000000000000000000000000000000000000000.00
DOUBLE -92559631349317831000000000000000000000000000000000000000000000.00
DOUBLE -92559631349317831000000000000000000000000000000000000000000000.00
DOUBLE -92559631349317831000000000000000000000000000000000000000000000.00
DOUBLE -92559631349317831000000000000000000000000000000000000000000000.00


Can someone please tell me what I am doing wrong???

Thanks
LittlePerson
0
LittlePerson
Asked:
LittlePerson
1 Solution
 
AndyAinscowCommented:
instead of VARIANT does it work with a COleVariant.
0
 
dxdat99Commented:
Hi there!
I think u would use like:
------
COleSafeArray sa;

sa.CreateOneDim(VT_R8, 10);

long l;

for(l = 0; l < 10; l++)
{
    double val = (33.45);
    sa.PutElement(&l, &val);
}

CString s;
CString ss;

for(l = 0; l < 10; l++)
{
    double t;
    sa.GetElement(&l, &t);
}
------
or u can ask me more

Good luck!!!
0
 
LittlePersonAuthor Commented:
I have tried the CreateOneDim option with the same results I'm afraid.
The problem here is I am using a Charting component which requires
that the ColeSafeArray be defined using Create(...)

I'll have a look again though.


Thanks
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
AndyAinscowCommented:
and COleVariant doesn't work?
0
 
aerowangCommented:
Oh,you are careless,the mistake you have taken is that you defin the "VARIANT val" twice,and every time you define it,it is a local variable!
So you shoule define it at first.My code is here,and it can be executed correctly on my computer!
Wish you good luck!
I'm also use COleSafeArray these days,are you familiar with Excel Automation with MFC,I wish we can exchange our experience.

COleSafeArray sa;
sa.CreateOneDim(VT_R8, 10);
long lindex[] = {0,1,2,3,4,5,6,7,8,9};
long l;
VARIANT val;
for(l = 0; l < 10; l++)
{
      
      VariantInit(&val);
      
      val.vt = VT_R8;
      val.dblVal = 33.45;
      sa.PutElement(&lindex[l], &val);
}

CString s;
CString ss;

for(l = 0; l < 10; l++)
{
      
      VariantInit(&val);
      sa.GetElement(&lindex[l], &val);
      
      switch(val.vt)
      {
                case VT_R8:
                       TRACE("DOUBLE %2.2f\n", val.dblVal);
                       break;

                case VT_BSTR:
                       TRACE("BSTR %s\n",(CString)val.bstrVal);
                       break;

                case VT_R4:
             TRACE("FLOAT %2.2f\n",  val.fltVal);
                        break;

                case VT_EMPTY:
                       TRACE("<empty>\n");
                        break;
                }
}
0
 
AndyAinscowCommented:
CAUTION.
If that is the problem try the initialisation with something like

for(l = 0; l < 10; l++)
{
     
     VariantInit(&val);
     
     val.vt = VT_R8;
     val.dblVal = 33.45 * (double)l;  //DIFFERENT values into each array member
     sa.PutElement(&lindex[l], &val);
}

then check what actually comes back
0
 
dxdat99Commented:
Hi!
-----
I think ... if u want to use u's code, u must change to

COleSafeArray sa;
sa.CreateOneDim(VT_VARIANT, 10);
------
Good luck!!!

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now