开发者

passing groups of properties from 1 class to another

I have a group of 13 properties in a class. I created a struct for thes开发者_运维百科e 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.


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.


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.


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


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"];
    }


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;
        }
    }


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;
        }

    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜