获取 vb.net 中对象引用的字符串表示形式

发布于 2024-11-13 05:35:03 字数 332 浏览 16 评论 0原文

我想创建一个对象引用的哈希表,并且希望将不同对象的引用作为键。我怎样才能在vb.net中做到这一点?

在 java 中(假设我使用默认的 toString 方法,并且 add() 将字符串作为键,将对象引用作为值),这将类似于:

hashtable.add(obj1.toString(), obj2)

我不想使用 vb.net gethashcode() 函数因为我希望对象的深层克隆具有不同的标识符。

一个相关的问题是 vb.net 中默认的 toString 是什么?

总结一下:如何获得表示 vb.net 中对象引用的字符串?

I want to make a hashtable of object references and I want a different object's reference to be the key. How can I do this in vb.net?

In java (assuming I am using the default toString method and that add() takes a string as a key and an object ref as the value) this would be something like:

hashtable.add(obj1.toString(), obj2)

I do not want to use a vb.net gethashcode() function because I want deep clones of objects to have different identifiers.

A related question is what is the default toString in vb.net?

In summery: How can I get a string that represents an object reference in vb.net?

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

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

发布评论

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

评论(3

不必了 2024-11-20 05:35:03

如果您希望对象引用成为哈希表的键,那么只需使用对象本身即可。

hashtable.Add(obj1, obj2)

要回答第二个问题,VB.Net 中 ToString 的默认实现是调用 Object.ToString。在 .Net 中,这将打印出底层实例的类型名称

If you want the object reference to be the key of the hash table then just use the object itself.

hashtable.Add(obj1, obj2)

To answer your second question the default implementation of ToString in VB.Net is to call into Object.ToString. In .Net this will print out the type name of the underlying instance

以为你会在 2024-11-20 05:35:03

一种可能性(尽管出于多种原因我不能说我会推荐这种做法)是使用 .net 中相当于 VB6 ObjPtr 函数的函数。本质上,您将对象固定在内存中,然后您可以检索该对象的内存地址(保证是唯一的)。

请参阅此帖子

http://forums.devx.com/showthread.php?t=122407< /a>

或者,如果我确实需要一个对象的唯一标识符,我可能会扩展该对象并添加一个“ObjectID”只读属性,该属性始终返回该对象的特定实例的生成 GUID。

如果这些 ID 没有保留在任何地方,您还可以创建一个单例“IDGenerator”对象,该对象仅分发递增的整数,并使用这些整数作为对象的 ID。但同样,您不想保留这些 int id,因为它们在您的应用程序运行中不会是唯一的。另外,根据实例化的对象数量,您可能需要将其设置为 long int 而不仅仅是 int。

One possibility (though I can't say I'd recommend this for a host of reasons) is to use the .net equivalent of the VB6 ObjPtr function. Essentially, you pin the object in memory and then you can retrieve a memory address for that object (which is guaranteed to be unique).

see this posting

http://forums.devx.com/showthread.php?t=122407

Alternately, if I really needed a unique identifier for an object, I'd probably extend the object and add an "ObjectID" readonly property that always returns a generated GUID for that particular instance of the object.

If those ID's weren't persisted anywhere, you could also create a singleton "IDGenerator" object that just handed out incrementing ints, and use those int's as ID's for your objects. But again, you wouldn't want to persist those int id's because they wouldn't be unique across runs of your app. Also, depending on how many objects you're instantiating, you may need to make it a long int and not just an int.

相守太难 2024-11-20 05:35:03

使用强类型字典会更好:

Dim myDict As New Dictionary(Of myObject1Type,myObject2Type)
myDict(obj1) = obj2

You'd be far better off using a strongly typed dictionary:

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