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;
}
}
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
SlotGroupNtypes must have reference toClientInfo, it would be worthwhile to have base classSlotGroupBasewhich will contain reference toClientInfo. Also you should add toSlotDatatype reference toSlotGroupBase. In this case you will access your skews likeAnother good idea is to restrict yourself and other developers from creation
SlotGroupNclass instances without reference toClientInfo,SlotDataclass item without reference toSlotGroup. To achieve this you should make default constructors private and add constructor with parameterClientInfo