views:

43

answers:

6

I have a group of 13 properties in a class. I created a struct for these properties and passed it to another class.

I need to add another 10 groups of these 13 properties. So thats 130 properties in total.

What do I do?

1.I could add all 130 properties to the struct. Will this affect performance and readability

2.I could create a list of structs but don't know how to access an item eg. to add to the list:

listRowItems.Add(new RowItems(){a=1, b=1, c=1, d=1...});

listRowItems.Add(new RowItems(){a=2, b=2, c=2, d=2...});

How do I access the second group item b?

3.Could I use just a dictionary with 130 items

4.Should I use a list of dictionaries (again I don't know how to access a particular item)

5.Should I pass in a class of 130 properties

Just for your interest the properties are css parameters used for a composite control. The control displays 13 elements in each row and there are 10 rows and each row is customisable.

+2  A: 

Why not just pass an instance of the class to the other class? It's not clear to me why the properties need to be in another data structure. If you want to limit the properties the other class can access, you can define an interface and implement it on class A then type the argument in the method that receives the class B as the interface type.

Jamie Ide
Thanks for the quick reply...I'm making a composite control and within the class is a nested class. So rather than making a new object of the class I thought it would be better to pass in just the properties. But maybe its better to just pass in the class. Can I pass the class into a nested class? This is confusing me
insanepaul
@insanepaul: You can. you even can pass reference to factory to nested class.
vittore
Does it affect performance? What do you mean by pass to factory to nested class?
insanepaul
I sincerely doubt it would measurably affect performance, but if it did it would improve it because all you're passing is a reference to the class. It depends if the nested class is public or not. If it is, you can pass a class directly; if not, you'll have to pass it to the containing class which can then hand it off to the nested class.
Jamie Ide
A: 

How do I access the second group item b?

By an integer index.

RowItems rowItems = list[index];

Whatever you do, do not expand the class or struct to have more properties unless those properties are relevant to the specific item. If the item has 13 properties to describe it, so should the class/struct. If you need to describe multiple items, use multiple objects such as in your List collection.

Anthony Pegram
OK, thanks yes I must have had a bad day of course to get the index...but which is the best way of passing the properties
insanepaul
I agree with Jamie Ide. If you already have a class to represent your object, what is stopping you from passing it instead of a struct intermediary?
Anthony Pegram
@Anthony I thought it would affect performance so was just passing in the parameters but now i need to get a load more parameters. Also how do I pass the class into a nested class?
insanepaul
A nested class can refer to an instance of its parent, and that includes methods that accept parameters of the parent's type. Just define the method like you would any other.
Anthony Pegram
@Anthony I think I'm nearly there. I just tested but got an error object not referenced. How do I reference the parent class fields? In the nested class i used private ParentClass parentClass as a field then refenced it by string x = parentClass.parentClassParameter
insanepaul
A: 

To access the second item's b property would just be:

listRowItems[1].b

Honestly though this seems pretty complex. It's not clear exactly what represents the actual CSS in what you're doing, but I would just use a dictionary indexed by row on the main property. You shouldn't be creating new classes or structs to simply represent properties of another class unless they fall into a logical subclass (same guidelines as using nested classes).

womp
OK, I've read all the answers which help my understanding but I'm thinking about performance too. When using the composite control in a web page the user needs to set the properties of the control by passing in the css class names and a couple of true/false properties. All the work is done in a nested Template class which doesn't have access to the properties unless I pass them into the constructor (maybe there is another way). So someone suggested passing the whole class into the nested class or I think you suggested having 1 dictionary. Performance is important. how aboutlist of dictionary?
insanepaul
A: 

If you want to access the 10 rows by index number, pass a generic List of your structs:

public void DoIt(System.Collections.Generic.List<MyPropertyStruct> list) {
    var thirdItem = list[2];
    }

If you need to access the 10 rows by some sort of key, use a generic Dictionary:

public void DoIt(System.Collections.Generic.Dictionary<string, MyPropertyStruct> d) {
    var sallyItem = d["sally"];
    }
richardtallent
OK, I now understand that thanks, but now it looks like I can just pass in the class(nobody mentioned performance yet) but I need to access those properties from a nested class
insanepaul
A: 

Just for your interest the properties are css parameters used for a composite control. The control displays 13 elements in each row and there are 10 rows and each row is customisable.

I would use something like this:

    public class ControlElement{}
    public class ControlRow
    {
        public Dictionary<string, ControlElement> Elements
        {
            get;
            set;
        }
    }

    public class Control
    {
        public List<ControlRow> Rows
        {
            get;
            set;
        }
    }
Jake
Thats something to think about but it looks like I can just pass in the class from some of the answers rather than just the properties.
insanepaul
A: 

Something like that ?

class StyleDefinition
    {
        public string Property { get; set; }
        public string Value { get; set; }

        public StyleDefinition(string p, string v)
        {
            Property = p;
            Value = v;
        }
    }

    class Cell {
        public List<StyleDefinition> Styles { get; set; }
        public string Value { get; set; }
    }

    class CellBuilder
    {
        public Cell CreateCell(List<StyleDefinition> styles, string value)
        {
            return new Cell { Styles = styles, Value = value };
        }

        public List<Cell> CreateRow(Dictionary<int, List<StyleDefinition>> styles, IEnumerable<string> values)
        {
            var res = new List<Cell>();
            var it = values.GetEnumerator();
            foreach( var kp in styles) {
                // logic for cell with key = kp.Key
                res.Add(CreateCell(kp.Value, it.Current));
                it.MoveNext();
            }

            return res;
        }

        public List<List<Cell>> CreateTable(Dictionary<int, Dictionary<int, List<StyleDefinition>>> styles, IEnumerable<IEnumerable<string>> values)
        {
            var res = new List<List<Cell>>();
            var it = values.GetEnumerator();
            foreach (var kp in styles)
            {                            
                res.Add(CreateRow(kp.Value, it.Current));
                it.MoveNext();
            }

            return res;
        }

    }
vittore