?
Solved

using COleSafeArray

Posted on 2004-04-25
10
Medium Priority
?
1,616 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
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
LVL 44

Expert Comment

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

Accepted Solution

by:
aerowang earned 200 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

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

764 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