Solved

using COleSafeArray

Posted on 2004-04-25
10
1,557 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
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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Host to IP 7 78
either24  challenge 19 97
twoTwo  challenge 35 104
Authenticate using sesu from script 7 36
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

832 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