System::String^ array

creating a 10 elements array

Page 1 of 1

4 Replies - 5606 Views - Last Post: 04 April 2011 - 03:22 AM

#1 bvds2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-December 10

System::String^ array

Posted 25 December 2010 - 12:01 PM

How can I initialize an array of type System::String^ of 10 elements(using the keyword new or gcnew)?
I tried this but it doesn't work:
System::String^ a=new array[10];
.
thanks.
Is This A Good Question/Topic? 0
  • +

Replies To: System::String^ array

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 3872
  • View blog
  • Posts: 11,405
  • Joined: 18-April 07

Re: System::String^ array

Posted 25 December 2010 - 12:19 PM

It is a bit convoluted how they do this, but you essentially use "array" to create an array to managed reference types...

array<String^>^ strArray = gcnew array<String^>(10);

// Store a String object in it
strArray[0] = gcnew String("hello world!");



That should get you all fixed up. Enjoy! :)
Was This Post Helpful? 0
  • +
  • -

#3 bvds2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 22-December 10

Re: System::String^ array

Posted 26 December 2010 - 10:37 AM

Thanks ,that was very helpful,but what if I want to initialize a 2 dimensional array and put some strings inside?
Was This Post Helpful? 0
  • +
  • -

#4 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: System::String^ array

Posted 03 January 2011 - 08:50 AM

Well, this is kinda of a late answer, but ...
In order to initialize a 2D array of strings, you can do this (recommended, easier):
array<System::String^, 2>^ example = gcnew array<System::String^, 2>(3, 5);
And that's all. "example" is a 2D array of strings, and it is not actually an array of arrays, but a rectangular array. Let's analyze this line of code a little bit. array<System::String^, 2>^ example - that 2 represents the dimension of our array, which is .. 2. gcnew array<System::String^, 2>(3, 5); - again, when we initialize the array, we must specify the size. 3 represents the number of rows in the array, and 5, number of columns. Using it is simple. Some example: example[0, 1] = "Example01" - pushes "Example01" at row 0 and column 1.
Earlier in the topic I told you that the above initialization method does not create an array of arrays, but a rectangular, managed array. This is basically a new, improved syntax, that took over the old initialization method, which created an array of arrays. As you can see, even the access to 2D array elements was changed. Now you have 2dArrayName[index_row, index_col] instead of 2dArrayName[index_row][index_col]. I'm gonna show you how to create an array of arrays too:
// Declaration: example is a jagged array, that can contain up to three arrays (3 rows).
array<array<System::String^> ^>^ example = gcnew array<array<System::String ^> ^>(3);
// Initialize each row. example[i] is nothing more than a 1D array, that can hold 5 elements.
// Therefore, we will have 5 columns.
for (int i = 0; i < 3; i++)
    example[i] = gcnew array<System::String ^>(5);

example[0][1] = "Example01";
// etc .. etc..


That is ugly. More complicated, as you have to initialize the array, row-by-row. In the above example, "example" is an array of arrays, containing 3 rows and 5 columns.
Was This Post Helpful? 0
  • +
  • -

#5 gpyrounakis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-April 11

Re: System::String^ array

Posted 04 April 2011 - 03:22 AM

Hello,


I want to return from a function a 2D System String array.

How do I define it?

DEFINE#
String^,2^ user_preferences(void); /// Not right....


Initialize#
array<System::String^, 2>^ users_profile = gcnew array<System::String^, 2>(99,4); 
users_profile = user_preferences(); // Call it and return a String^ 2D array


Thank you in advance
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1