如何在 c++ 中实例化托管类 (数据表)?

发布于 2024-07-16 19:24:58 字数 2469 浏览 6 评论 0原文

您好,我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌上青苔 2024-07-23 19:24:58

您可能没有在接口中实现成员(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文