tags:

views:

85

answers:

1

Does anyone have any suggestions as to how to go about designing a container class, in C#, that has upwards of thirty variables contained within it?

I have written it out as a set of variables, as there are a few different types, eg string or DateTime, however I am thinking that it would perhaps be better to house them all in a dictionary of objects with their property names as keys?

Cheers, Ed

+5  A: 

If all the fields are required, then they are required. Using a dictionary can only add overhead and incur extra memory costs. A dictionary approach may be useful in a few specific scenarios, such as property bag implementations, and sparse event handlers (since events are the most obvious sparse data).

If the fields are used sparsely (i.e. often not more than a few are in use), then perhaps push the related fields into a number of inner classes (per group of related fields) - i.e. instead of:

class Foo {
    string userName;
    int userId;

    string orderReference;
    DateTime orderDate;

    ...
}

something like:

class Foo {
     class UserInfo { // move this outside if it makes sense to re-use it
          public int Name {get;set;}
          public int Id {get;set;}
     }
     UserInfo user;

     class OrderInfo { // move this outside if it makes sense to re-use it
         public string Reference {get;set;}
         public string Date {get;set;}
     }
     OrderInfo order;
}

Then when you have to store the user data, create a new UserInfo; if you don't need the user data, leave user null.

Marc Gravell
That is effectively the implementation I am already driving at, so thanks for salving my conscience! However, the 30+ variables are on the base class already, then we split up the REST (Yes, this build is insane, and it's not my job to correct that, think eBay but for a few specialist products) into seperate sub-classes, each with another 15+ properties! There's no way to make it nice or pretty, so I'm going to just keep with the multiple properties approach!
Ed Woodcock