Solved

using COleSafeArray

Posted on 2004-04-25
10
1,587 Views
Last Modified: 2013-11-20
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
Comment
Question by:LittlePerson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 10916182
instead of VARIANT does it work with a COleVariant.
0
 
LVL 2

Expert Comment

by:dxdat99
ID: 10925616
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
 

Author Comment

by:LittlePerson
ID: 10926481
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
Technology Partners: 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!

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 10926529
and COleVariant doesn't work?
0
 

Accepted Solution

by:
aerowang earned 50 total points
ID: 10937562
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
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 10937596
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
 
LVL 2

Expert Comment

by:dxdat99
ID: 10937619
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question