如何在c ++(DATATABLE)中实例化托管类?

时间:2009-04-13 07:59:18

标签: .net c++-cli

您好我正在使用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);
    }

1 个答案:

答案 0 :(得分:0)

您可能没有在接口中实现成员(IListSource需要GetList 方法)因此你的类是抽象的,直到你实现(或子类,然后实现)缺少的方法。