문제

I've been teaching myself C#, and I'm just learning how to use custom data types. The program I'm writing produces a bunch of pairs of coordinate pairs. I thought it'd be a neat idea to create a data type that holds each set (x1, x2, y1, y2), along with a few other variables pertaining to that set. However, the program will produce more than one array of coordinates sets (different categories), so keeping track of things was still difficult. I then broke it down further into categories, and placed each category under a third type that acts as a third level, which is then put into a list.

Each "tier" of items has some properties specific to that tier, but prior to this roadblock I didn't have any need to swap data among the hierarchy. The problem arose when I realized that I needed to modify the coordinate pair sets using an offset, and each offset is specific to the parent data type. I can modify the get{} code to return the data plus the offset (I called it "skew"), but not if the offset is from outside the data type's class itself. I tried setting a value in the parent data type (even a public static one), but the child couldn't read it for some reason.

The only way I know how to make this work is by setting the property in each coordinate set, but there could be thousands of them. The value is unique to the parent, but all the children need to use it, so that seems wasteful, given that there will be a lot of other calculations going on. My other thought was to maintain an offset array, and add it to the places where the values are retrieved. But, that isn't as clean as containing it within the data type itself, and so it will add to the confusion. Is there another method of accomplishing this?

Here is how some of the code looks:

public class SlotData
    {
        private double _x1, _x2, _y1, _y2;

        public double X1
        {
            get { return _x1; }
            set { _x1 = value; }
        }
        public double X2
        {
            get { return _x2; }
            set { _x2 = value; }
        }
        public double Y1
        {
            get { return _y1; }
            set { _y1 = value; }
        }
        public double Y2
        {
            get { return _y2; }
            set { _y2 = value; }
        }
    }
public class ClientInfo
    {
        public static double _skewX, _skewY;

        public SlotGroup1 Group1
        {
            get;
            set;
        }
        public SlotGroup2 Group2
        {
            get;
            set;
        }
        public SlotGroup3 Group3
        {
            get;
            set;
        }
    }

public class SlotGroup1
    {
        public SlotData Slot1
        {
            get;
            set;
        }
        public SlotData Slot2
        {
            get;
            set;
        }
    }
도움이 되었습니까?

해결책

    public class SlotData
    {
        private SlotData() { }
        public SlotData(SlotGroupBase group) 
        {
            this._group = group;
        }

        private SlotGroupBase _group;

        public double X1 { get; set; }
        public double X2 {get; set;}
        public double Y1 {get; set;}
        public double Y2 {get; set;}

        public double NewX1 
        {
            get
            {
                return _group.ClientInfo._skewX + X1;
            }
        }
    }

    public class ClientInfo
    {
        public double _skewX, _skewY;

        public SlotGroup1 Group1 { get; set; }
    }

    public abstract class SlotGroupBase
    {
        private SlotGroupBase() { }
        public SlotGroupBase(ClientInfo ci) 
        {
            this._ci = ci;
        }

        private ClientInfo _ci;

        public ClientInfo ClientInfo
        {
            get
            {
                return _ci;
            }
        }            
    }

    public class SlotGroup1 : SlotGroupBase
    {
        public SlotGroup1(ClientInfo ci):base (ci) {}
        public SlotData Slot1 { get; set; }
        public SlotData Slot2 { get; set; }
    }

    static void Main(string[] args)
    {
        ClientInfo ci = new ClientInfo();
        SlotGroup1 sg1 = new SlotGroup1(ci);
        sg1.Slot1 = new SlotData(sg1);
        sg1.Slot2 = new SlotData(sg1);
        Console.ReadLine();
    }

In your code you don't have either parent or descendant data types. So, members of some type couldn't be accessible to other types in any way other than you will have reference to an instance of object of some type.

But object-oriented programming could help you. In case if each from SlotGroupN types must have reference to ClientInfo, it would be worthwhile to have base class SlotGroupBase which will contain reference to ClientInfo. Also you should add to SlotData type reference to SlotGroupBase. In this case you will access your skews like

return _group.ClientInfo._skewX + X1;

Another good idea is to restrict yourself and other developers from creation SlotGroupN class instances without reference to ClientInfo, SlotData class item without reference to SlotGroup. To achieve this you should make default constructors private and add constructor with parameter ClientInfo

public SlotGroupBase(ClientInfo ci) 

다른 팁

Extending you design ...

using System.Drawing;

public class SlotData
{
    private PointF _one;
    private PointF _two;

    internal SizeF Skew {get; set;}

    public PointF One 
    {
        get
        {
            return PointF.Add(_one, Skew);
        }
        set {_one = value; }
    }

    public PointF Two 
    {
        get
        {
            return PointF.Add(_two, Skew);
        }
        set {_two = value; }
    }
}

public class SlotGroup : List<SlotData>
{
    internal SizeF Skew
    {
        set
        {
            foreach(var slotData in this)
            {
                slotData.Skew = value;
            }
        }
    }
}

public class ClientData : List<SlotGroup>
{
    private SizeF _skew;

    public SizeF Skew
    {
        get { return _skew; }

        set
        {
            _skew = value;
            foreach (var slotGroup in this)
            {
                slotGroup.Skew = value;
            }
        }
    }
}

I could not think of anything more elegant that would work. Encapsulation dictates that contained classes cannot access the data of thier container and the code to override the the child accesors on the container classes would have been more cumbersome.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top