Hi people,
I need to change return type of a function. So function should return array values.In here, data type of the array values are not int,char or double. The data type of the array values are a kind of custom type.

So confused. Please help:scared::idea::(

void DataType_Class::insert_rowData_into_array()
{
   array<Custom_DataType^> ^Person = gcnew array<Custom_DataType^>(6);
   Person [1] = gcnew Custom_DataType;
   Person [2] = gcnew Custom_DataType;
   Person [3] = gcnew Custom_DataType;

SqlConnection ^sqlConn = gcnew SqlConnection( "DataSource");
sqlConn->Open();
SqlComm ^comSelect  ;
comSelect =  gcnew SqlComm ("SELECT * FROM PeopleInfo ", sqlConn);    
SqlDataReader ^reader ;
reader = comSelect->ExecuteReader();
int i = 1;
	while (reader->Read())  
	{
	Person [i]->id = Convert::ToInt32( reader["y_id"] );
	Person [i]->name = Convert::ToString( reader["y_name"] );
	Person [i]->weight = Convert::ToInt32( reader["y_weight"] );  
        i++;
	}
	sqlConn->Close();
}

Referred class :

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

public ref class Custom_DataType 
{
	public: 
        int id,weight,age;
	String ^name; 

};

The first thing I would do would be to move the individual record reader inside the object, then render the object capable of being searched in a collection of generics.

using namespace System;
using namespace System::Data::SqlClient;
using namespace System::Data;

public ref class Custom_DataType
{
public:
   int id;
   int age;
   int weight;
   String^ name;

   Custom_DataType()
   {
      id=0;
      age=0;
      weight=0;
   }

   Custom_DataType(IDataReader^ rdr)
   {
      id = int::Parse(rdr["ID"]->ToString());
      age = int::Parse(rdr["AGE"]->ToString());
      weight = int::Parse(rdr["WEIGHT"]->ToString());
   }

   virtual String^ ToString() override
   {
      return id.ToString()+'-'+age.ToString()+'-'+weight.ToString();
   }

   virtual bool Equals(Object^ obj) override
   {
      return this->ToString()->Equals(((Custom_DataType^)obj)->ToString());
   }

   virtual int GetHashCode() override
   {
      return this->ToString()->GetHashCode();
   }
};

Then I would make a Loader specifically that loaded a collection of those objects.
I like List<> objects (instead of arrays).

public ref class Custom_DataTypeLoader
{
public:
   virtual bool Load(List<Custom_DataType^>^ master, String^% strError)
   {
      bool blnRetVal = true;
      try
      {
         String^ strSQL = "SELECT * FROM PeopleInfo";
         SqlConnectionStringBuilder^ csb = gcnew SqlConnectionStringBuilder();
         SqlConnection ^conn = gcnew SqlConnection(csb->ToString());
         conn->Open();
         SqlDataReader ^rdr = (gcnew SqlCommand(strSQL, conn))->ExecuteReader();

	while (rdr->Read())  
	{
	   master->Add(gcnew Custom_DataType(rdr));
	}

         rdr->Close();
	conn->Close();
      }
      catch(Exception^ exc)
      {
         blnRetVal = false;
         strError = exc->Message;
      }

      return blnRetVal;
   }
};

Additionally, I would normally make that List<> another class called Custom_DataTypeMaster : public List<Custom_DataType^> and call Load from THAT, but for this example, I'll skip it and call Load directly from the loader.

int main(array<System::String ^> ^args)
{
    List<Custom_DataType^>^ lstCustomDataType = gcnew List<Custom_DataType^>(65);
    String^ strError = "";
 
    Custom_DataTypeLoader^ loader = gcnew Custom_DataTypeLoader();

    if(!loader->Load(lstCustomDataType, strError))
    {
       Console::WriteLine("Could not load: " + strError);
       return -1;
    }

    return 0;
}

THIS way, the loader is not bound TO the object and each piece of the code can be tested separately.

Also (typo) the columns from the database need to be changed to the y_id, y_name, y_weight as i your code.

Edited 4 Years Ago by thines01: typo

...so all of that was to say: Don't return the array. Modify it as a parameter to your loader, but the actual syntax you are seeking would look like this:

array<Custom_DataType^, 1>^ DataType_Class::insert_rowData_into_array()
{
   array<Custom_DataType^>^ arr = gcnew array<Custom_DataType^>(65);
   // fill it here
   return arr;
}
This question has already been answered. Start a new discussion instead.