简体   繁体   中英

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? Below is a small example but my parameters will more than the 10. Is there another way to this?

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. Do you have any idea how to fix this?

Sure, use DTO's (data transfer objects). 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.

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. 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.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM