[英]Passing more than 10 parameters between classes in C#?
I am a new to C#, I need a small help on how can I pass multiple parameters between the classes? 我是C#的新手,我需要一个小小的帮助,如何在类之间传递多个参数? Below is a small example but my parameters will more than the 10. Is there another way to this? 下面是一个小例子,但我的参数将超过10.还有另一种方法吗?
public StreamStructure(String name, string id, string classname, int number)
{
this.name = name;
this.id = id;
this.classname = classname;
this.number = number;
}
List ------ 清单------
List<abc> don = new List<abc>();
foreach (XmlElement abc_cdb in abc_cdbs)
{
abc.Name = abc_cdb.GetAttribute("NAME");
abc.Id = abc_cdb.GetAttribute("id");
abc.Clssname = abc_cdb.GetAttribute("classname");
abc.number = Convert.ToInt32(abc_cdb.GetAttribute("number"));
don.Add(abc);
}
I have used as suggested in ans but I am trying to create a list in C# my first record gets replaced with the 2nd one, since the fields in MyDTO are defined as public. 我已按照ans中的建议使用了但是我试图在C#中创建一个列表,我的第一个记录被第二个记录替换,因为MyDTO中的字段被定义为public。 Do you have any idea how to fix this? 你知道如何解决这个问题吗?
Sure, use DTO's (data transfer objects). 当然,使用DTO(数据传输对象)。 That is, create a class that has all the fields you want to send and use an instance of it as a parameter. 也就是说,创建一个包含要发送的所有字段的类,并将其实例用作参数。 Added bonus is that your method signature won't change even if you change your DTO class. 额外的好处是,即使您更改了DTO课程,您的方法签名也不会改变。
You could pass a domain object that represents the item you are manipulating. 您可以传递表示您正在操作的项目的域对象。
public class Widget
{
public string Name {get;set;}
public int Id {get;set;}
public string ClassName {get;set;}
public int Number {get;set;}
}
var myWidget = new Widget();
myWidget.Name = "Blue Widget";
//etc
StreamStructure(myWidget);
You are probably better off using C# Initializers or a Data Transfer Object than a large number of constructor parameters. 使用C#Initializers或数据传输对象可能比使用大量构造函数参数更好。 Or combine the two. 或者将两者结合起来。
public class MyDTO
{
String Name { get; set; }
String Id { get; set; }
String ClassName { get; set; }
int Number { get; set; }
}
var MyDTO = new MyDTO()
{
Name = Name,
Id = Id,
ClassName = ClassName,
Number = Number
}
var stream = new StreamStructure(MyDTO)
To create a list of these objects as in your example, create a new DTO within the loop body. 要在示例中创建这些对象的列表,请在循环体中创建新的DTO。
var don = new List<MyDTO>();
foreach (XmlElement abc_cdb in abc_cdbs)
{
var abc = new MyDTO()
{
Name = abc_cdb.GetAttribute("NAME");
Id = abc_cdb.GetAttribute("id");
ClassName = abc_cdb.GetAttribute("classname");
Number = Convert.ToInt32(abc_cdb.GetAttribute("number"));
};
don.Add( abc );
}
You should write a new class that contains the properties you want to pass to the method, and change your method to include just that new class. 您应该编写一个包含要传递给方法的属性的新类,并将您的方法更改为仅包含该新类。
For your example, write a new class like this: 对于您的示例,编写一个这样的新类:
public class RequestObject
{
public string Name { get; set; }
public string ID { get; set; }
public string ClassName { get; set; }
public int Number { get; set; }
}
Then change your method like this: 然后像这样改变你的方法:
public StreamStructure(RequestObject requestObject)
{
//DoStuff
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.