如何提取关键对象的字符串表示
我将项目存储在强类型 IDictionary
中,以便值也代表键:
public class MyObject
{
public string Name { get; private set; }
public SectionId Section { get; private set; }
public MyObject(SectionId section, string name)
{
Section = section;
Name = name;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != typeof(MyObject)) return false;
return Equals((MyObject)obj);
}
public override int GetHashCode()
{
unchecked
{
return (Name.ToLower().GetHashCode() * 397) ^ Section.GetHashCode();
}
}
}
在我的表示层中,我需要迭代此字典,将每个项目添加到 ListBox控制。我很难弄清楚如何将 MyObject (也充当键)转换为 ListBox 控件可以用作值的字符串。我应该像这样显式调用 MyObject.GetHashCode() 吗:
MyListBox.Add(new ListItem(myObject.Name, myObject.GetHashCode())
I'm storing items in a strongly typed IDictionary<TKey, TValue>
such that the value also represents the key:
public class MyObject
{
public string Name { get; private set; }
public SectionId Section { get; private set; }
public MyObject(SectionId section, string name)
{
Section = section;
Name = name;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != typeof(MyObject)) return false;
return Equals((MyObject)obj);
}
public override int GetHashCode()
{
unchecked
{
return (Name.ToLower().GetHashCode() * 397) ^ Section.GetHashCode();
}
}
}
In my presentation tier, I need to iterate through this Dictionary, adding each item to a ListBox control. I'm having a difficult time figuring out how to transform MyObject (which also acts as a key) into a string that the ListBox control can use as a value. Should I just make an explicit call to MyObject.GetHashCode() like this:
MyListBox.Add(new ListItem(myObject.Name, myObject.GetHashCode())
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会想到覆盖 toString 方法 在这里,您基本上将编写代码,生成一个有意义的字符串并在用户界面中显示
希望我正确理解您的问题。
I would think of overriding the toString method and in here you will basically write code that will generate a meaningful string to be displayed in the ui
Hope I understood your question correctly.
不,
GetHashCode()
是:相反,每个
MyObject
都应该有某种唯一的标识符键。这可以是一个enum
值或由数据库中的IDENTITY列生成的数字,或者只是一个唯一标识每个特定MyObject
的字符串
,您可以从中检索MyObject
从您用作存储库的任何集合或数据库。如果只能有一个具有给定
Name
和Section
的MyObject
,您可以将两者组合起来:SectionId + " :" + 名称
。这样您就可以在事后解析这两个值。No,
GetHashCode()
is:MyObject
from.Instead, each of your
MyObject
s should have some kind of unique identifier key. This can be anenum
value or a number generated by an IDENTITY column in your database, or just astring
that uniquely identifies each particularMyObject
, and from which you can retrieve theMyObject
from whatever collection or database you're using as a repository.If there can only ever be a single
MyObject
with a givenName
andSection
, you could just combine the two:SectionId + ":" + Name
. That way you can parse those two values out after the fact.