【问题标题】:Class structure for the proposed data and its containers? [closed]建议数据及其容器的类结构? [关闭]
【发布时间】:2010-12-31 23:45:08
【问题描述】:

首先,我想祝所有可能读到这篇文章的人新年快乐 :)

我在如何为要导入应用程序的一些数据创建容器时遇到问题,我不确定如何很好地解释这一点,而且我的英语不是那么好,所以我希望你能容忍我的错误并帮助我提供一些指导。

目前,我正在使用 foreach 从收到的数据中导入以下字段: guid, itemid, name, owner(可选, 可以为null), zoneid, titleid, subid, heading, x, y, z, typeA, typeB, typeC

从上述字段中,我需要存储给定项目已移动到的所有坐标的航点列表,但是对于每个 guid,我都有一个新的航点列表。

从航路点列表中,第一个条目也是我的初始物品起始位置,这将是我的物品初始位置(如果您注意到我有一个单独的列表,我不确定它会不会更好)并非所有物品有一个航点列表,但所有项目都有第一个位置。

所以我存储这些数据的第一个想法是一个列表,其中包含一个包含 2 个内部类及其列表的类:

public List<ItemList> myList = new List<ItemList>();
public class ItemList
{
    public int GuID { get; set; }
    public int ItemID { get; set; }
    public string Name { get; set; }
    public int TitleID { get; set; }
    public itemType Status { get; set; }

    public class Waypoint
    {
        public int Zone { get; set; }
        public int SubID { get; set; }
        public int Heading { get; set; }
        public float PosX { get; set; }
        public float PosY { get; set; }
        public float PosZ { get; set; }
    }
    public List<Waypoint> Routes = new List<Waypoint>();
}

下面是我如何将新航点添加到列表中存在的 GUID 的示例

                    ItemList myItem = myList.Find(item => item.GuID == GUID);
                    if (myItem != null)
                    {
                        int lastDistance = 3;
                        ItemList.Waypoint nextWaypoint;

                            // Add new Coordinates to the given GUID
                            ItemList.Waypoint lastWaypoint = myItem.Routes.LastOrDefault();
                            if (lastWaypoint != null)
                            {
                                lastDistance = getDistance(posX, posY, posZ, lastWaypoint.PosX, lastWaypoint.PosY, lastWaypoint.PosZ);
                                if (lastDistance > 2)
                                {
                                    nextWaypoint = new ItemList.Waypoint();
                                    nextWaypoint.SubID = subID;
                                    nextWaypoint.Zone = zone;
                                    nextWaypoint.Heading = convertHeading(heading);
                                    nextWaypoint.PosX = posX;
                                    nextWaypoint.PosY = posY;
                                    nextWaypoint.PosZ = posZ;
                                    nextWaypoint.Rest = rest;
                                    myItem.waypoint.Add(nextWaypoint);
                                }
                            }
                    }

然后要注册一个新项目,我会利用上面的 itemExist,所以我不会再次注册相同的 GUID:

                    ItemList newItem = new ItemList();
                    newItem.GuID = GUID;
                    newItem.ItemID = itemID;
                    newItem.Name = name;
                    newItem.Status = status;
                    newItem.TitleID = titleID;

                    // Item location
                    ItemList.Location itemLocation = new ItemList.Location();
                    itemLocation.SubID = subID;
                    itemLocation.Zone= zone;
                    itemLocation.Heading = convertHeading(heading);
                    itemLocation.PosX = posX;
                    itemLocation.PosY = posY;
                    itemLocation.PosZ = posZ;
                    newItem.position.Add(itemLocation);
                    myList.Add(newItem);

您能帮我就我的班级结构和列表的外观提供建议吗?

是否有更好的方法与列表交互以获取 GUID 的 lastWaypoint 或验证项目是否存在?

一般来说,您还有什么建议?

PS:如果您有任何问题或我错过了发布的内容,请告诉我,我会更新。

更新:已更改以上内容以反映我目前拥有的内容以及如果其他人仍有我想听的建议。

【问题讨论】:

    标签: c# containers class-design


    【解决方案1】:

    一些笔记(在两杯香槟之后……):

    • 您没有说明对数据结构及其使用的任何要求。你的设计看起来很合理。
    • 您可以使用FirstOrDefault() 方法检查项目是否存在(即返回值!= null),并通过一次操作从列表中检索它。
    • 您的代码有一个明显的错误:在添加新的航路点之前没有nextWaypoint != null 检查并且它不需要存在。同样,也不需要检查lastWaypoint != null,因为列表不会包含任何空值。
    • 我建议向您的类引入构造函数,以接受强制属性的值。
    • 没有明确的一点要同时使用 GUID 和整数 ID 来标识 ItemList — 搜索条件被过度指定了。
    • 常用的编码标准规定属性名称应以大写字母开头(posXPosX 等)。
    • waypointposition 列表也应该是属性;仅具有 getter 并以 readonly 字段为后盾。

    附:进入你的时区,新年快乐,2011 年有很多好的代码;-)

    【讨论】:

    • You can you the FirstOrDefault() method to check for existence (i.e. the return value != null) of an item and retrieve it from the list in one operation. 但这对我来说不可用,因为在航点上我不需要第一个而是最后一个,并且在我必须搜索该项目的方式之前,但它是上帝知道. apparent bug 实际上我正在重写它以将其发布在这里,我可能已经跳过了过程中的重要部分,我的错。 I'd recommend introducing constructors 我确实有,但我试图仅使用我所怀疑的真正需要的信息来缩小帖子的大小。
    • 几乎是呵呵 ;) 谢谢,是的,属性也是一种类型 + 一些饮料......呵呵,我确实从我的代码中复制并粘贴了其中的一部分,但在发布过程中重写了一些东西在这里,可能最终会忘记它。
    • 关于FirstOrDefault,这是一个很棒的提示,因为它引导我访问LastOrDefault
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    • 2016-05-21
    • 1970-01-01
    • 2017-06-24
    • 2014-09-26
    相关资源
    最近更新 更多