CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    [RESOLVED] How to convert an STL/CLR vector to a .NET array

    The second sample in this MSDN article demonstrates how to convert an STL/CLR map<> to a .NET IDictionary<>. I tried to transpose this pattern to a conversion from a vector<> to an array<>, but this code fails to compile:

    Code:
    #include <cliext/adapter>
    #include <cliext/vector>
    
    // ...
    
    using namespace cliext;
    
    // ...
    
    // Return an array of moving holidays in a specific year
    
    array<DateTime> ^Holidays::MovingHolidays(int nYear)
    {
      vector<DateTime> ^vctHolidays=gcnew vector<DateTime>;
      DateTime datEasterSunday=EasterSunday(nYear);
    
      // Just a few for a first test:
    
      vctHolidays->push_back(datEasterSunday+TimeSpan(-2,0,0,0));  // Karfreitag
      vctHolidays->push_back(datEasterSunday);                     // Ostersonntag (of course... <g>)
      vctHolidays->push_back(datEasterSunday+TimeSpan(1,0,0,0));   // Ostermontag
    
      array<DateTime> ^adatHolidays=vctHolidays;
      return adatHolidays;
    }
    The last line before the return statement gives me an error C2440, telling me that a 'cliext::vector<_Value_t> ^' can't be converted into a 'cli::array<Type> ^'.

    I only had to go that detour at all because I didn't find a method like push_back or add for the CLI array

    There was, BTW, something that made me wonder in the MSDN sample code: It declares a stand-alone variable of type System::Collections::Generic::IDictionary<float, int>. I always were of the strong belief that these ISomethings could only be implemented as part of a "hosting" class.

    TIA for any advice about that!

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543

    Re: How to convert an STL/CLR vector to a .NET array

    If you want array, create array and not vector.

    array<DateTime>^ vctHolidays = gcnew array<DateTime>^(2);
    vctHolidays[0] = ...;
    vctHolidays[1] = ...;
    return vctHolidays;

  3. #3
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: How to convert an STL/CLR vector to a .NET array

    Quote Originally Posted by Alex F View Post
    If you want array, create array and not vector.
    Ok, but what if I don't know the number of entries in advance? This is the reason why I chose a vector. The requirement for a variable number of entries (unfortunately) is not apparent in the function as it is now, but the function is planned to be extended to be able to return the specific holidays for each one of the German federal states.

    So you might - reasonably - argue: Then why not return the vector? Answer: The collection of holidays is finally meant to be assigned to the BoldedDates property of a MonthCalendar control, so it has to be converted to an array sooner or later anyway, and I would prefer to confine details like that inside the class.

    The interface of the Holidays class is not yet carved in stone, however. It has just two static member functions so far, and one of them is even private.

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Red face Re: How to convert an STL/CLR vector to a .NET array

    Uuuh... Sometimes life could be so easy if I'd only look in the right place: The STL/CLR vector container has a method that does nothing more and nothing less than returning an array constructed out of the vector's contents. It's called - what a surprise: to_array().

    So the only thing I had to do was to replace the last two lines of the function body I posted above by

    Code:
      return vctHolidays->to_array();
    Sorry for wasting your time...

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to convert an STL/CLR vector to a .NET array

    Quote Originally Posted by Eri523 View Post
    Uuuh... Sometimes life could be so easy if I'd only look in the right place: The STL/CLR vector container has a method that does nothing more and nothing less than returning an array constructed out of the vector's contents. It's called - what a surprise: to_array().
    Yep, there's a lot to be learned by just looking around at the class methods in msdn and seeing what's there.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured