C# "error CS1729: 'XXClass' does not contain a constructor that takes 0 arguments"的解决方案
2016-08-25 17:08
471 查看
出现这种错误的原因时,没有在子类的构造函数中指出仅有带参构造函数的父类的构造参数。
具体来讲就是:
当子类要重用父类的构造函数时, C# 语法通常会在子类构造函数后面调用 : base( para_type, parameter).
假设父类有一个参数个数为1的构造函数, 没有 0 参构造函数。 子类想要重用这个构造函数, 如果没有写 :base(para_type, parameter), 就会有这个错误。
因为如果没写, VS 会认为子类是继承父类的 0 参构造函数, 但是由于父类并没有定义 0 参构造函数, 所以就会报错。
另外, 可以在base()中调用一个静态方法来修改子类构造函数的参数在传递给父类构造函数。 如:
参考文章
1.changtianshuiyue. C# does not contain a constructor that takes no parameter , 2014-9。
具体来讲就是:
当子类要重用父类的构造函数时, C# 语法通常会在子类构造函数后面调用 : base( para_type, parameter).
假设父类有一个参数个数为1的构造函数, 没有 0 参构造函数。 子类想要重用这个构造函数, 如果没有写 :base(para_type, parameter), 就会有这个错误。
因为如果没写, VS 会认为子类是继承父类的 0 参构造函数, 但是由于父类并没有定义 0 参构造函数, 所以就会报错。
另外, 可以在base()中调用一个静态方法来修改子类构造函数的参数在传递给父类构造函数。 如:
class ParentClass { public ParentClass(string Name) {} } class ChildClass { public ChildClass(string firstName, string familyName):base( CombineName(firstName, familyName)) { } static string ConbineName(string firstName, string familyName) { return string.Format("{0},{1}", firstName, familyName); } }
参考文章
1.changtianshuiyue. C# does not contain a constructor that takes no parameter , 2014-9。
相关文章推荐
- does not contain a constructor that takes 0 arguments 解析之C#构造函数
- Base class does not contain a constructor that takes '0' argument
- Base class does not contain a constructor that takes '0' argument
- editor does not contain a main type的解决方案[ZT]
- editor does not contain a main type的解决方案
- Web Service调用Nhibernate数据访问异常:xx cannot be serialized because it does not have a parameterless constructor
- editor does not contain a main type的解决方案
- does not contain handler parameter named 'method'. This may be caused by whitespace in the label text.异常及其解决方案
- editor does not contain a main type" 错误解决方法
- editor does not contain a main type的解决方案
- Eclipse editor does not contain a main type的解决方案
- android 报错column '_id' does not exist的解决方案
- 在VS2010中调用COM Interop DLL操作Excel通过get_Range去获取Range时,会发生Object does not contain a definition for get_Range的错误。其原因和解决方案
- 解决方案:"Client does not support authentication protocol requested by server”错误
- Unity does not contain a definition for "controller"
- java" selection does not contain a main type"的问题处理方法
- editor does not contain a main type的解决方案
- "editor does not contain a main type" 错误解决方法
- 关于C#中的错误:The type 'X' does not exists in the "Y"
- CMake Error: The source directory "/root/mysql-5.5.29-linux2.6-x86_64" does not appear to contain CM