如何从类继承DataRow并将其初始化? [英] How to inherit DataRow from a class and initialize it?

查看:150
本文介绍了如何从类继承DataRow并将其初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在继承要在我的编码中实现的Typed Datarow时遇到问题.我的问题是,从一个类继承Typed Datarow然后在另一个类中对其进行初始化的正确方法是什么?

例如:

I''m having problem to inherit Typed Datarow to be implemented in my coding. my question is, what is the right method to inherit a Typed Datarow from a class and then initialize it in another class?

eg:

Public class ClassA : DataSet1.DataRow1
{
    public ClassA(DataRowBuilder rb) : base (rb)
    {
        //TODO
    }
}




但是我怎么想让这个构造函数起作用呢?

我在:
遇到错误




but how am i suppose to get this constructor works?

i got error at :

Public class ClassB
{
    private void DoWork()
    {
        ClassA newRow = new ClassA(); <--- Error ("Cannot resolve construtor")
    }
}

推荐答案

您发布的代码中存在两个问题.

1.类不能从实例继承.
你写了
There are two problems in the code you posted.

1. Classes cannot inherit from instances.
You wrote
Public class ClassA : DataSet1.DataRow1


这使类ClassA继承自DataSet1.DataRow1.按照通常的命名约定,看起来DataRow1是DataRow类型的对象.
所以做吧


This makes the class ClassA inherit from DataSet1.DataRow1. From the usual naming conventions, this looks like DataRow1 is an object of type DataRow.
So make it

Public class ClassA : DataRow

代替.

2.使用不存在的构造函数不起作用
您正在尝试通过

instead.

2. Using a non-existent constructor doesn''t work
You are trying to create an instance of ClassA via

new ClassA();

创建ClassA实例;您使用的构造器没有取任何参数.但是在给定的代码中,ClassA的唯一构造函数采用一个类型为DataRowBuilder的参数.
如果您不提供任何构造函数,则编译器会静默创建一个不带参数的构造函数.这就是为什么您无需事先编写就可以在简单的类中使用它的原因. 提供任何构造函数后,便会禁用此快捷方式.所以您有两个选择:
a)编写另一个没有参数或
的构造函数 b)在ClassB.DoWork()中,使用现有的ClassA构造函数,例如

You use a constructor that doesn''t take any parameters. But in your given code, the only constructor for ClassA takes one parameter of type DataRowBuilder.

If you don''t provide any constructors, the compiler silently creates one without parameters. That''s why you can use it in simple classes without writing it beforehand.
This shortcut is disabled as soon as you provide any constructors. So you''re left with two options:
a) Write another constructor that works without parameters or
b) In ClassB.DoWork() use the existing ClassA constructor like

new ClassA(someDataRowBuilderThatIsValidInThisScope);


您需要在classA中有一个没有参数的默认构造函数.
由于已使用参数(DataRowBuilder)定义了构造函数,因此原始构造函数不可用.

否则,您可以在创建classA的实例时传递一个新的DataRowBuilder 对象.
You need to have a default constructor with no parameters in classA.
Since you have defined a constructor with a parameter (DataRowBuilder) the original constructor is unavailable.

Or else, you can pass a new DataRowBuilder object when you create an instance of classA.


从外观上,您试图从一个特定的实例继承一个类.一个DataSet以及它的行的特定实例(无论如何它都没有-集合有表,有表)

您不能这样做:继承不适用于特定实例,它适用于泛型类型.

例如,您可以说福特福克斯是从福特类继承而来的类,而福特类又继承自汽车类:

By the looks of it, you are trying to inherit a class from a specific instance of a DataSet, and a specific instance of it''s rows (which it dowesn''t have anyway - a set has tables, which have rows)

You can''t do that: inheritance does not work from a specific instance, it works from generic types.

For example, you can say that a Ford Focus is a class inherited from the Ford class, which inherits from the Car class:

public class Car{}
public class Ford : Car {}
public class Focus: Ford {}
public class Mondeo : Ford {}


但是您不能说福特福克斯是您的姨妈本田(Ethyl)本田协议继承下来的一门课.

您需要回到基础知识,再看一遍课程笔记-您还不了解类和实例,还没有继承得多!


But you can''t say that a Ford Focus is a class inherited from your aunt Ethyl''s Honda accord.

You need to go back to basics and look again at your course notes - you do not understand classes, and instances, much less inheritance yet!


这篇关于如何从类继承DataRow并将其初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆