本文介绍了将类传递为具有参数化构造函数的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个带参数化构造函数的类,例如 public 类公司 { 私有 string _name; public 公司(字符串名称) { 此 ._ name = name; } } 现在我在我的页面上调用此公司类 public string CompanyDetails(Company objcompany) { // 某些逻辑 } 但是当我将公司类传递给CompanyDetails方法,抛出异常没有参数为此对象定义的构造函数,这很明显我相信这是因为我有参数化构造函数。 如何解决这个问题?我错过了什么或者不是通过课程的正确方法吗? 感谢指导:)解决方案 似乎有一些synctax错误 你的班级名称是公司,你提到的构造函数说公司名称 参考文章 C#中的构造函数介绍 [ ^ ] 用以下代替代码 public 类公司 { 私有 string _name; public 公司() { } public 公司(字符串名称) { 此 ._ name =姓名; } } 取决于您如何将公司类传递给CompanyDetails方法。 因为您已经使用参数声明了构造函数,系统不再自动为您生成无参数构造函数。 因此,如果您尝试创建公司类的新实例没有传递一个字符串它会查找无参数版本并报告错误。 这有几种方法: 1如果名称是强制性的,则保持原样,并在创建新实例时提供新名称。 2)将字符串转换为可选参数: public CompanyName( string name = 匿名) { 此。 _name = name; } 3)编写一个无参数构造函数来设置默认字符串值。 我试过你的逻辑工作正常...... 命名空间 ConsoleApplication3 { public class 公司 { private string _name; // BEFORE // public CompanyName(字符串名称) // { // this._name = name; // } 公开公司(字符串名称) { this ._ name = name; } } class 计划 { public string CompanyDetails(Company objcompany) { // 某些逻辑 return 你好; } 静态 void Main( string [] args) {公司ob = 新公司( sasank); 程序obj = 新程序(); // 将公司对象传递给CompanyDetails方法 Console.WriteLine(obj。 CompanyDetails(OB)); // o / p hello } } } I am having a class with parameterized constructor e.gpublic class Company{ private string _name; public Company(string name) { this._name = name; }}now i am calling this Company Class on my pagepublic string CompanyDetails(Company objcompany){ //Some Logic}But when i pass Company class to CompanyDetails method its throwing exception "no parameter less constructor defined for this object", which is obvious i believe this is because i have parameterized constructor .How to solve this prob? am i missing something or its not the right way to pass class?Thanks for guiding :) 解决方案 It seems there is some synctax errorYour class name is Company where as you mentioned constructor saying CompanyNameRefer the article An Intro to Constructors in C#[^]Replace the code with the belowpublic class Company{ private string _name; public Company() { } public Company(string name) { this._name = name; }} Depends on how you "pass Company class to CompanyDetails method".Because you have declared a constructor with a parameter, the system no longer generates a parameterless constructor for you automatically.So if you try to create a new instance of your Company class without passing it a string it looks for a parameterless version and reports an error.There are a couple of ways round this:1) If the name is compulsory, then leave it as it is, and provide a new when you create the new instance.2) Convert your string to an optional parameter:public CompanyName(string name = "Anonymous"){ this._name = name;}3) Write a parameterless constructor that sets a default string value.I tried your logic its just work fine...namespace ConsoleApplication3{ public class Company { private string _name;//BEFORE// public CompanyName(string name)// {// this._name = name;// } public Company(string name) { this._name = name; }} class Program { public string CompanyDetails(Company objcompany) { //Some Logic return "hello"; } static void Main(string[] args) { Company ob = new Company("sasank"); Program obj = new Program(); //Passing Company objects to CompanyDetails method Console.WriteLine(obj.CompanyDetails(ob)); // o/p hello } }} 这篇关于将类传递为具有参数化构造函数的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-03 21:58