954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Array of Managed Objects

Hi everyone,

I'm trying to pass data to managed object arrays. In this case i got no errors also no
correct output. So what's wrong in the code below ?
Please help.

PS. This code writes " MyDataType" on textBox1 ??!

*******************
*   MyDataType.h  *
*******************
using namespace System;

public ref class MyDataType 
{
  private:

  public: // all are public ok
  
  int id;
  String ^name; 

  MyDataType() : id(0) {}
  MyDataType(int value) : id(value) {}  
  ~MyDataType() {}

};
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) 
   {
array<MyDataType ^> ^arrayObject = gcnew array <MyDataType ^> (5);

arrayObject [0] = gcnew MyDataType;

arrayObject [0]->id = 1;
arrayObject [0]->name = "nut";

// Checking any array object content			 
textBox1->AppendText( Convert::ToString(arrayObject[0]));  

    }
smurfy
Newbie Poster
12 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

Your code is "converting" the object at element position 0 (an instance of MyDataType) to a string - but since it's an object, you are getting the type name.

I'm assuming you want "nut" to appear in the text box. If so, the code that does this is:

textBox1->AppendText(arrayObject[0]->name);

If you add a "toString" method to your "MyDataType" class, you could change your code to:

textBox1->AppendText(arrayObject[0]->toString());

Assuming, of course, that your "toString()" method returns a string with the data contained in the "^name" property.

mcriscolo
Posting Whiz in Training
218 posts since Apr 2008
Reputation Points: 57
Solved Threads: 64
 

Thank you.

smurfy
Newbie Poster
12 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: