如何分配可由多个属性使用的方法 [英] How do I assign a method that can be used by multiple properties
问题描述
我想创建一个类/方法,允许我检查值是否在限制范围内。我无法弄清楚如何为此定义类/方法。以下是一些伪代码:
I would like to create a class/method that allows me to check if the value is within limits. I cannot figure out how to define the class/method for this. Here is some pseudo code:
Class T
Public a as integer
Public b as integer
Public c as integer
Function check as Boolean
return value>0
End Function
End Class
使用上面的内容:
Use above like:
dim myT as new T
myT.a=9
debug.writeline (myT.a.check)
myT.b=-1
debug.writeline (myT.b.check)
myT.c=1
debug.writeline (myT.c.check)
显示应该是:
true
false
true
请帮助我。谢谢
Display should be:
true
false
true
Please help me. Thank you
推荐答案
您好,这里有两个例子可以为您提供答案。第二个类T2使用类型Integer的扩展方法,如本文前面所述。第一个类示例使用自定义类来保存整数值并提供检查值的功能。
Hi there, here are two examples that may provide you an answer. The second Class T2 uses an extension method for type Integer as mentioned before this post. The first Class example uses a custom class to hold the integer value and supplies the functionality to check the value.
Imports System.Runtime.CompilerServices
Module Module1
'This is my custom class to hold and check integer value
Friend Class T
Property A As New myInteger
Property b As New myInteger
Property c As New myInteger
Friend Class myInteger
Private _value As Integer
Public Property Value() As Integer
Get
Return _value
End Get
Set(ByVal value As Integer)
_value = value
End Set
End Property
Friend Function Check() As Boolean
Return CBool(Value > 0)
End Function
End Class
End Class
'This is your class from pseudo code.
Friend Class T2
Property A As Integer
Property B As Integer
Property C As Integer
End Class
'Run Code here
Sub Main()
Debug.WriteLine("Testing Class T")
Dim myT As New T
myT.A.Value = 9
Debug.WriteLine(myT.A.Check)
myT.b.Value = -1
Debug.WriteLine(myT.b.Check)
myT.c.Value = 1
Debug.WriteLine(myT.c.Check)
Debug.WriteLine("Testing Class T2")
Dim myT2 As New T2
myT2.A = 9
Debug.WriteLine(myT2.A.ExtCheck)
myT2.B = -1
Debug.WriteLine(myT2.B.ExtCheck)
myT2.C = 1
Debug.WriteLine(myT2.C.ExtCheck)
End Sub
'Needed Extension method
<Extension()>
Public Function ExtCheck(ByVal value As Integer) As Boolean
Return CBool(value > 0)
End Function
End Module
我希望有帮助。
I hope that helps.
你不需要在你的课程中有一个功能。您可以在课堂外使用此功能,例如:
You don't need to have a function inside your class. You can have a function outside your class, like this:
Function Check(valueToCheck As Integer) As Boolean ' put this method outside your class
Return valueToCheck > 0
End Function
使用上面这样的方法:
Use the above method like this:
dim myT as new T
myT.a=9
debug.writeline (Check(myT.a))
myT.a=-1
debug.writeline (Check(myT.a))
myT.a=0
debug.writeline (Check(myT.a))
你可以定义检查
方法在你的班级(然后你必须像 myT.Check(myT.a)
),但是没有理由这样做(除非你真的需要它是出于某种目的,但我不会立刻想到这一点。)
You can define the Check
method in your class (and then you have to all it like myT.Check(myT.a)
), but there is not really a reason for doing that (unless you really need it for a purpose, but there doesn't come one to my mind immediately).
这篇关于如何分配可由多个属性使用的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!