如何在 c++ 中实例化托管类 (数据表)?
您好,我正在 C++ 中创建一个数据表,如下
当我尝试创建实例时,我收到编译时错误,因为无法为抽象类创建实例。 在这里我没有使用任何抽象关键字。
#include "stdio.h"
#include "conio.h"
#using <mscorlib.dll>
using namespace System::Data;
using namespace System::ComponentModel;
using namespace System;
[Serializable]
public __gc class Table :public MarshalByValueComponent,public IListSource,public ISupportInitialize,public System::Runtime::Serialization:: ISerializable
{
public:
void MakeTable();
void main()
{
Table *s=new Table();
s->MakeTable();
Console::WriteLine("Hai");
printf("hello");
getch();
}
};
void Table::MakeTable()
{
DataSet* myDataSet;
// Create a new DataTable.
DataTable* myDataTable = new DataTable(S"ParentTable");
// Declare variables for DataColumn and DataRow objects.
DataColumn* myDataColumn;
DataRow* myDataRow;
// Create new DataColumn, set DataType, ColumnName and add to DataTable.
myDataColumn = new DataColumn();
myDataColumn->DataType = System::Type::GetType(S"System.Int32");
myDataColumn->ColumnName = S"id";
myDataColumn->ReadOnly = true;
myDataColumn->Unique = true;
// Add the Column to the DataColumnCollection.
myDataTable->Columns->Add(myDataColumn);
// Create second column.
myDataColumn = new DataColumn();
myDataColumn->DataType = System::Type::GetType(S"System.String");
myDataColumn->ColumnName = S"ParentItem";
myDataColumn->AutoIncrement = false;
myDataColumn->Caption = S"ParentItem";
myDataColumn->ReadOnly = false;
myDataColumn->Unique = false;
// Add the column to the table.
myDataTable->Columns->Add(myDataColumn);
// Make the ID column the primary key column.
DataColumn* PrimaryKeyColumns[] = new DataColumn*[1];
PrimaryKeyColumns->Item[0] = myDataTable->Columns->Item[S"id"];
myDataTable->PrimaryKey = PrimaryKeyColumns;
// Instantiate the DataSet variable.
myDataSet = new DataSet();
// Add the new DataTable to the DataSet.
myDataSet->Tables->Add(myDataTable);
// Create three new DataRow objects and add them to the DataTable
for (int i = 0; i<= 2; i++){
myDataRow = myDataTable->NewRow();
myDataRow->Item[S"id"] = __box(i);
myDataRow->Item[S"ParentItem"] = String::Format( S"ParentItem {0}", __box(i));
myDataTable->Rows->Add(myDataRow);
}
Hi i am creating a Datatable in C++ as follows
when i try to create the instance i am getting a compile time error as cannot creating instance for abstract class. here i am not usng any abstract keyword.
#include "stdio.h"
#include "conio.h"
#using <mscorlib.dll>
using namespace System::Data;
using namespace System::ComponentModel;
using namespace System;
[Serializable]
public __gc class Table :public MarshalByValueComponent,public IListSource,public ISupportInitialize,public System::Runtime::Serialization:: ISerializable
{
public:
void MakeTable();
void main()
{
Table *s=new Table();
s->MakeTable();
Console::WriteLine("Hai");
printf("hello");
getch();
}
};
void Table::MakeTable()
{
DataSet* myDataSet;
// Create a new DataTable.
DataTable* myDataTable = new DataTable(S"ParentTable");
// Declare variables for DataColumn and DataRow objects.
DataColumn* myDataColumn;
DataRow* myDataRow;
// Create new DataColumn, set DataType, ColumnName and add to DataTable.
myDataColumn = new DataColumn();
myDataColumn->DataType = System::Type::GetType(S"System.Int32");
myDataColumn->ColumnName = S"id";
myDataColumn->ReadOnly = true;
myDataColumn->Unique = true;
// Add the Column to the DataColumnCollection.
myDataTable->Columns->Add(myDataColumn);
// Create second column.
myDataColumn = new DataColumn();
myDataColumn->DataType = System::Type::GetType(S"System.String");
myDataColumn->ColumnName = S"ParentItem";
myDataColumn->AutoIncrement = false;
myDataColumn->Caption = S"ParentItem";
myDataColumn->ReadOnly = false;
myDataColumn->Unique = false;
// Add the column to the table.
myDataTable->Columns->Add(myDataColumn);
// Make the ID column the primary key column.
DataColumn* PrimaryKeyColumns[] = new DataColumn*[1];
PrimaryKeyColumns->Item[0] = myDataTable->Columns->Item[S"id"];
myDataTable->PrimaryKey = PrimaryKeyColumns;
// Instantiate the DataSet variable.
myDataSet = new DataSet();
// Add the new DataTable to the DataSet.
myDataSet->Tables->Add(myDataTable);
// Create three new DataRow objects and add them to the DataTable
for (int i = 0; i<= 2; i++){
myDataRow = myDataTable->NewRow();
myDataRow->Item[S"id"] = __box(i);
myDataRow->Item[S"ParentItem"] = String::Format( S"ParentItem {0}", __box(i));
myDataTable->Rows->Add(myDataRow);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能没有在接口中实现成员(IListSource 需要 获取列表
方法),因此您的类是抽象的,直到您实现(或子类化然后实现)缺少的方法。
You probably didn't implement members in your interfaces (IListSource requires GetList
method) and therefore your class is abstract until you implement (or subclass and then implement) the missing methods.