提问人:Ufuk Can İşbildi 提问时间:12/31/2021 更新时间:1/1/2022 访问量:43
如何控制多元组的相等性?
How to control the equality of mutiple tuples?
问:
Imports System
Class RGB
Public Shared ReadOnly Dim Red As New RGB(255, 0, 0)
Public Shared ReadOnly Dim Green As New RGB(0, 255, 0)
Public Shared ReadOnly Dim Blue As New RGB(0, 0, 255)
Private R, G, B As Byte
Sub New(ByVal R As Byte, G As Byte, B As Byte)
Me.R = R
Me.G = G
Me.B = B
End Sub
Function IsSameColorWith(ByVal OtherRGB As RGB) As Boolean
Return (Me.R, Me.G, Me.B) = (OtherRGB.R, OtherRGB.B, OtherRGB.G)
End Function
End Class
Module MainModule
Sub Main()
Dim Color As RGB = New RGB(255, 0, 0)
Console.WriteLine(RGB.Red.IsSameColorWith(Color))
End Sub
End Module
我无法控制 中两个元组的相等性。
我该如何纠正这个问题?如何控制多个元组的相等性?IsSameColorWith(ByVal OtherRGB As RGB)
答:
1赞
Calaf
1/1/2022
#1
我认为你不需要使用元组。只需检查 R、G 和 B 的相等性:
Return Me.R = OtherRGB.R AndAlso Me.G = OtherRGB.G AndAlso Me.B = OtherRGB.B
无论如何,在这种情况下,您不能使用运算符:您必须使用 .=
.Equals(...)
您的代码变为:
Return (Me.R, Me.G, Me.B).Equals((OtherRGB.R, OtherRGB.B, OtherRGB.G))
评论
(RGB) = (RBG)