Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.5
Hello,
I have a class which accepts more then 15 parameter in constructor and all parameters are used to assign value to it's own properties.
 
The problem is that, this class has become very static and in future if I want to intialise some more properties then I need to increase the number of parameter in constructor and do many changes in existing code.
 
How should I design this class so it can be easily extensible?
 
Thanks.
Maddie
Posted 10-May-11 4:44am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Just an idea. Object and Collection Initializers[^]
 
Object initializers let you assign values to any accessible fields or properties of an object at creation time without having to explicitly invoke a constructor.
public class Cat
{
    public int Age;
    public string Name;
}
 
Cat cat = new Cat { Age = 10, Name = "Fluffy" };
 
Instead of:
public class Cat
{
    public int Age;
    public string Name;
 
    public Cat(int age, string name)
    {
      this.Age = age;
      this.Name = name;
    }
}
 
Cat cat = new Cat(10, "Fluffy");
 
If Cat.Age and Cat.Name is read only. Then change Age and Name to
public readonly int Age;
public readonly string Name;
 
This way, you cannot change the value any more when. Only on create of a new Cat class.
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

No need to do that at all. Just use the C# property initialization syntax.
 
class Foo
{
    public int PropInt { get; set; }
 
    public string PropString { get; set; }
}
 
void Bar()
{
    Foo foo = new Foo()
    {
        PropInt = 10,
        PropString = "hello"
    };
}
  Permalink  
v2
Comments
   
5 - proposed as answer
Kim Togo at 10-May-11 10:06am
   
Good answer, my 5.
thatraja at 10-May-11 13:54pm
   
Fine answer.....
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

As an example of what Nish said, given the class below:
 
public class MyClass
{
   public string Prop1 { get; set; }
   public string Prop2 { get; set; }
   public string Prop3 { get; set; }
 
   public MyClass()
   { 
      Prop1 = "";
      Prop2 = "";
      Prop3 = "";
   }
}
 
Call it like this:
 
MyClass myClass = new MyClass();
 
or
 
MyClass myClass = new MyClass(){Prop1="1"};
 
or
 
MyClass myClass = new MyClass(){Prop1="1", Prop3="3"};
In the example above, any property you don't initialize will have the value "" (I don't believe in allowing uninitialized variables).

  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 562
1 Sergey Alexandrovich Kryukov 484
2 Maciej Los 325
3 DamithSL 233
4 Mathew Soji 195
0 OriginalGriff 7,168
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,461
3 Manas Bhardwaj 4,876
4 Maciej Los 4,450


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 10 May 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100