VB.NET:运算符 '='未定义...相同类型的变量和对象?

发布于 2024-11-06 00:36:53 字数 1201 浏览 8 评论 0原文

好吧,我在这里完全困惑了。我有一堂课...说MyClass。它具有我的类型的另一个类的多个属性,例如 MyHelperClass (以及其他属性)。

我正在执行以下操作:

Dim inst As MyClass = New MyClass() With {
    .p1 = sv1, 
    .p2 = sv2, 
    .h1 = getHelperClass(a1), 
    .p3 = sv3, 
    .p4 = sv4, 
    .h2 = getHelperClass(a2), 
    .p5 = sv5, 
    ...
    .pN = svN
}

*其中 .p# 是某个属性,.sv# 是某个有效值。 .h# 是 MyHelperClass 类型的属性, getHelperClass(a#) 返回所述类的实例。

现在,我遇到了奇怪的事情,h1 的赋值语句完美地工作。没问题。然而,h2 的赋值语句给了我以下蓝色波形错误:

Operator '=' is not Defined for types myLib.MyHelperClass and myLib.MyHelperClass.

我只是在以下位置没有收到此错误全部!我什至不知道从哪里开始解决这个问题。帮助!

201105.06 0305: h1 类型的签名是 List(Of myLib.Address),其中 Address 是一个非常基本的类,具有典型的地址字段(名称、地址、城市、州、邮政编码、 ETC。)。 getHelperClass 的返回类型也是List(Of myLib.Address)

正如 SSS 在他的回答中暗示的那样,如果我使用它对没有运算符的类进行相等测试,我希望 = 不会以“自然”方式工作,但是我将它用作分配运算符,而不是相等,我看不出有任何问题。我期望将 getHelperClass 的结果分配给 h2。但它告诉我 = 没有为该类型定义。是否有可能由于某种原因,编译器将其解释为 =(EQUALS) 而不是 =(ASSIGN)

至于注释掉该行以及它发生在第一行上,我需要等到明天回到办公室才能检查。会回来报告。

Okay, I am TOTALLY confused here. I have a class... say MyClass. It has several properties of another class of my type, say MyHelperClass (along with other properties).

I am doing the following:

Dim inst As MyClass = New MyClass() With {
    .p1 = sv1, 
    .p2 = sv2, 
    .h1 = getHelperClass(a1), 
    .p3 = sv3, 
    .p4 = sv4, 
    .h2 = getHelperClass(a2), 
    .p5 = sv5, 
    ...
    .pN = svN
}

*where .p# is some property, .sv# is some valid value. .h# is a property of type MyHelperClass and getHelperClass(a#) returns an instance of said class.

Now, I have the odd thing here, where the assignment statement for h1 works perfect. No problems. The assignment statement for h2 however, it is giving me the following blue-squiggle error:

Operator '=' is not defined for types myLib.MyHelperClass and myLib.MyHelperClass.

I just do not get this error at all! I don't even know where to start to figure this out. HELP!

201105.06 0305:
The signature for h1's type is List(Of myLib.Address), where Address is a very basic class with typical address fields (name, address, city, state, zip, etc.). The return type of getHelperClass is also List(Of myLib.Address).

As SSS hinted at in his answer, I would expect = to not work in the "natural" way if I was using it for equality testing on a class without operators, however I am using it as an assignment operator, not equality, which I can't see any problem with. I am expecting the result of getHelperClass to be assigned to h2. But instead it's telling me = is not defined for the type. Is it possible that for some reason, the compiler is interpreting it as =(EQUALS) instead of =(ASSIGN)?

As for commenting out that line and it happening on the first one, I'll need to wait till I'm back in the office tomorrow to check that. Will report back.

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

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

发布评论

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

评论(2

说谎友 2024-11-13 00:36:53

啊,是的,抱歉没有正确阅读你的OP。该作业一定被误解为比较。也许你漏掉了一个逗号?例如,在语句“a = b = c”中,第一个等号是赋值,第二个等号是比较。

Ah, yeah, sorry didn't read your OP properly. The assignment must be being misinterpreted as an comparison. Maybe you are missing a comma? For example in the statement "a = b = c" the first equals sign is an assignment, the second is a comparison.

慕巷 2024-11-13 00:36:53

您需要将 Operator 方法添加到 MyHelperClass,

例如

Public Shared Operator =(byval a as MyHelperClass, byval b as MyHelperClass) As Boolean
...
End Operator

Public Shared Operator <>(byval a as MyHelperClass, byval b as MyHelperClass) As Boolean
...
End Operator

您还应该了解引用类型和值类型之间的区别。

You need to add the Operator methods to MyHelperClass

e.g.

Public Shared Operator =(byval a as MyHelperClass, byval b as MyHelperClass) As Boolean
...
End Operator

Public Shared Operator <>(byval a as MyHelperClass, byval b as MyHelperClass) As Boolean
...
End Operator

You should also read up on the difference between Reference and Value types.

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