0

I have declared a vector like this for managed type.
My question is how it is possible to declare a number of elements as you do for native like this. OneVector(10) ?

If I write OneVector(10) below this will not work. Either if I write OneVector.AddRange(10).
Thanks...

public: System::Collections::Generic::List<double> OneVector;
2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by Radical Edward
0

> My question is how it is possible to declare a number of elements as you do for native like this. OneVector(10) ?
It isn't possible, but you can get close by using one of the overloads for the List<> class that expects an IEnumerable<T> object:

using namespace System;
using namespace System::Collections::Generic;

#include <iostream>

int main()
{
  array<int>^ src = {1, 2, 3, 4, 5, 6};
  List<int>^ numbers = gcnew List<int>((IEnumerable<int>^)src);

  for each (int value in numbers)
    std::cout << value << '\n';
}

The cast is a necessary evil, and Edward would guess that's why AddRange didn't work because it's the same thing in a different place:

using namespace System;
using namespace System::Collections::Generic;

#include <iostream>

int main()
{
  array<int>^ src = {1, 2, 3, 4, 5, 6};
  List<int>^ numbers = gcnew List<int>();

  numbers->AddRange((IEnumerable<int>^)src);

  for each (int value in numbers)
    std::cout << value << '\n';
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.