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();
}
};
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
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;
}
};
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
Additionally, I would normally make that List<> another class called Custom_DataTypeMaster : public List 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.
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
...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;
}
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402