Solved

using COleSafeArray

Posted on 2004-04-25
10
1,520 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
10 Comments
 
LVL 44

Expert Comment

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

Expert Comment

by:dxdat99
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
and COleVariant doesn't work?
0
 

Accepted Solution

by:
aerowang earned 50 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

771 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now