交叉引用多个对象

发布于 2024-07-12 04:50:34 字数 749 浏览 5 评论 0原文

好吧,所以我很难想出一个“好”的方法来执行以下操作:

将 4 个对象分组,这样如果我对象 AI 可以轻松地且无需经过对象 B 和对象 B,那么我可以 轻松地创建对象。 C 查明对象 D 是否与对象 A 相关。以及任何其他引用组合。

我目前正在使用 SortedLists 将 A 链接到 B,将 B 链接到 C,将 C 链接到 D,并且交叉引用它们的工作非常草率。 这可行,但它真的非常难看。 我不喜欢丑陋的代码,除非没有其他方法可以做到。

我正在考虑使用一个类来存储对象。 但引用它们的最佳方式是什么?

提前致谢


更多信息:

对象是表单上的控件。 4 个不同的控件都彼此相关,因为它们对应于用于配置的相同设置。 表单上有多个设置需要修改,每个设置使用 4 个控件。

我需要一种方法来轻松引用它们,而不必调用Setting4ControlB.Checked 或Setting2ControlD.text,即链接列表。 我对某些控件使用通用事件,以便重用代码,它们都执行相同的操作,只是它们引用不同的对象。

难看的部分是,首先,设置链接,其次,进行从 A 到 D 或 C 到 A 的引用,等等。我希望能够做这样的事情:

GetSettingControls(ReferencingControl).ControlA.Checked

ReferencingControl 将是事件中的发送者,ControlA 是与 ReferencingControl 相关的控件。

Ok, so I'm having a hard time coming up with a 'good' way to do the following:

Group 4 objects so that if I Object A I can easily and without having to go through Objects B & C to find out if Object D relates to Object A. As well as any other combination of referencing.

I am currently using SortedLists to link A to B and B to C and C to D and doing a really sloppy job of cross referencing them. This works, but its really, really ugly. I don't like ugly code unless there is no other way to do it.

I'm thinking of using a class to store the objects. But what would be the best way to reference them?

Thanks in advance


More info:

The objects are controls on a form. The 4 different controls are all related to each other in that they correspond to the same setting that they are used to configure. There are multiple settings that get modified on the form, each using 4 controls.

I need a way to easily reference them without having to call Setting4ControlB.Checked or Setting2ControlD.text, thus the linked lists. I'm using generic events for certain controls in order to reuse code, they all do the same thing, except they reference different objects.

The ugly portion is first, setting up the links, and second, doing the referencing from A to D or C to A, etc. I wanted to be able to do something like this:

GetSettingControls(ReferencingControl).ControlA.Checked

ReferencingControl would be the sender in the event, ControlA is a control that is related to ReferencingControl.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

野稚 2024-07-19 04:50:34

首先,如果你有一个类似 A->B->C->D 的关系,查看 A 是否与 D 相关的唯一方法就是从 A 一直走到 D。尽管如此,你仍然可以应用一些 “

这将涉及手动跟踪程序中存在的每个间接引用,也就是说,您将负责动态添加 A->D 引用,这对于某些逻辑可能很有用,例如能够 在游戏中携带”物体。

您需要使用弱引用字典,这样您就不会通过引用使对象保持活动状态...

public class MyObject
{
  public List<WeakReference> References = new List<WeakReference>();

  // My Class Properties and Methods
}

MyObject obj = new MyObject();
obj.refence.Add(new WeakReference(gun));

这样,如果从对象池中清除了枪(例如 Level.Objects 集合中的 userObjects 集合),则弱引用将失效。

您可以通过执行以下操作来检查对象:

foreach ref in obj.references
  if (ref.IsAlive)
    object tool = ref.Target;

这只是一个示例,您知道,实现良好的访问器、属性等...

First of all, if you have a relationship like A->B->C->D the only way to see if A relates to D is to go all the way from A to D. You can nontheless apply a few tricks

This will involve manually keeping track of each indirect reference present in the program, that is, you'll be in charge of adding the A->D reference dinamically, that may be useful with certain logics like for example being able to "carry" objects in a game.

You need to use a Dictionary of WeakReferences, so that you don't keep objects alive through references...

public class MyObject
{
  public List<WeakReference> References = new List<WeakReference>();

  // My Class Properties and Methods
}

MyObject obj = new MyObject();
obj.refence.Add(new WeakReference(gun));

that way if the gun is cleared from the object pool (for example the userObjects collection from your Level.Objects collection) the weakreference will become invalid.

You can check for the object by doing

foreach ref in obj.references
  if (ref.IsAlive)
    object tool = ref.Target;

This is just an example, you know, implement good accessors, properties and so on...

挥剑断情 2024-07-19 04:50:34

您可以使用通用 Dictionary 类来指定对象之间的键/值对。

这将允许您根据另一个对象查找一个对象。

System.Collections.Generic.Dictionary<object, object> myDictionary = new Dictionary<object, object>();
myDictionary.Add(objectA, objectD);
myDictionary.Add(objectD, objectA);

You could use the generic Dictionary class to specify key/value pairs between the objects.

This would allow you to look up an object based on another object.

System.Collections.Generic.Dictionary<object, object> myDictionary = new Dictionary<object, object>();
myDictionary.Add(objectA, objectD);
myDictionary.Add(objectD, objectA);
我做我的改变 2024-07-19 04:50:34

我认为不遍历对象 B 和 C 不是一个好主意。您的对象应该只知道它的直接连接,然后从那里,您可以基于整个对象图处理关系。

您应该能够轻松找到基本图形处理的算法。

我不明白的是你为什么使用 SortedList。 连接是否存储在对象外部?

无论如何,您应该能够通过存储包含从一个对象到另一个对象的直接连接的 IEnumerabe(数组或其他)来轻松完成此操作。 您只需递归地处理对象(您不必按字面意思实现它,但实际上,这就是您要做的)对象,直到找到从一个对象到另一个对象的路径。 您只需存储已经采取的路径,这样您就不会发现自己在处理循环引用。

I don't think it's a good idea to not go through objects B and C. Your objects should only know it's direct connections and then from there, you can process relations based on the entire object graph.

You should be able to find algorithms for basic graph processing easily.

What I don't get is why you are using the SortedList. Are the connections stored externally to the object?

Regardless, you should be able to do this easily by storing an IEnumerabe (array or whatever) that contains the immediate connections from one object to another. You just have to recursively process (you don't have to literally implement it this way, but in spirit, that is what you would do) the objects until you found the path from one to another. You just have to store the paths you have taken already so you don't find yourself processing circular references.

爱你是孤单的心事 2024-07-19 04:50:34

根据您试图表示的关系类型,可能会有非常好的实现/算法。 例如,如果关系确定等效类,您可以使用类似 并集查找算法判断两个元素是否属于同一类。

Depending on the kind of relation you are trying to represent there could be really good implementation/algorithm. For example if the relation determine equivalent classes you could use something like the Union Find Algorithm to determine if two elements belongs to the same class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文