重载“And”的示例是什么? VB.NET 中的运算符?

发布于 2024-10-03 18:32:29 字数 241 浏览 2 评论 0原文

因此,对于查找“And”或“Or”等重载运算符的示例,Google 并不是一个好的选择,因为它会尝试将它们解析为搜索查询本身的运算符。 MSDN 也没有提供如何实现重载 And 运算符的示例,因此我不确定如何为我的项目正确重载它。

有人至少有一个“和”的例子吗? “或”或“异或”(或任何其他)将是一个奖励。我不确定是否需要在我的对象中重载这些运算符,因为我仍在构建它们并且还没有计划。但是,拥有可能被谷歌索引的例子可能会帮助很多人保持理智......

So Google's not a good choice for looking up examples for overloaded operators like "And" or "Or", because it tries to parse them as operators to the search query itself. MSDN also provides no examples of how to implement an overloaded And operator, so I'm not certain how to properly overload it for my project.

Does anyone have an example of "And" at minimum? "Or" or "Xor" (or any others) would be a bonus. I'm not certain if I need to overload these operators in my objects just yet, as I'm still building them out and haven't planned beyond just yet. But having examples around that might get indexed by Google will probably help save the sanity of a lot of people...

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

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

发布评论

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

评论(2

烟凡古楼 2024-10-10 18:32:29

以与重载一元或算术运算符相同的方式重载逻辑运算符。

我实际上无法想到目前这会很有用的情况,但为了举例,下面是一个覆盖 AndOr 运算符的示例结构:

Public Structure Foo

    Public Shared Operator And(ByVal val1 as Foo, ByVal val2 as Foo) As Foo
        ''#(calculate the logical And of the two specified values here)
        Return New Foo(val1.Bar And val2.Bar, val1.Baz And val2.Baz)
    End Operator

    Public Shared Operator Or(ByVal val1 as Foo, ByVal val2 as Foo) As Foo
        ''#(calculate the logical Or of the two specified values here)
        Return New Foo(val1.Bar Or val2.Bar, val1.Baz Or val2.Baz)
    End Operator

End Structure

MSDN 页面 给出了一些其他示例和进一步的解释。

You overload the logical operators in the same way that you overload the unary or arithmetic operators.

I can't actually think of a case where this would be useful at the moment, but for the sake of example, here's a sample structure that overrides the And and Or operators:

Public Structure Foo

    Public Shared Operator And(ByVal val1 as Foo, ByVal val2 as Foo) As Foo
        ''#(calculate the logical And of the two specified values here)
        Return New Foo(val1.Bar And val2.Bar, val1.Baz And val2.Baz)
    End Operator

    Public Shared Operator Or(ByVal val1 as Foo, ByVal val2 as Foo) As Foo
        ''#(calculate the logical Or of the two specified values here)
        Return New Foo(val1.Bar Or val2.Bar, val1.Baz Or val2.Baz)
    End Operator

End Structure

This MSDN page gives some other examples and further explanation.

醉酒的小男人 2024-10-10 18:32:29

这里是 MSDN 上的一篇文章,解释运算符重载在 VB.NET 2005 中,这应该仍然与 2010 相关。

文章中的 Add 示例(使用文章中定义的 ComplexNumber 类):

Public Shared Operator +(cn1 As ComplexNumber, _
            cn2 As ComplexNumber) As ComplexNumber

   Dim Result As New ComplexNumber( _
                        cn1.Real() + cn2.Real(), _
                       cn1.Imaginary() + cn2.Imaginary())

   Return Result

End Operator

Here is an article on MSDN explaining operator overloading in VB.NET 2005, this should still be relevant to 2010.

The example for Add, from the article (using the ComplexNumber class defined in the article):

Public Shared Operator +(cn1 As ComplexNumber, _
            cn2 As ComplexNumber) As ComplexNumber

   Dim Result As New ComplexNumber( _
                        cn1.Real() + cn2.Real(), _
                       cn1.Imaginary() + cn2.Imaginary())

   Return Result

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