如何自动将参数传递给函数?
How to pass arguments to a function automatically?
我想通过此函数传递 txt1 和 txt2 来验证两个用户输入框,但是我一次只能传递一个参数,这意味着我必须创建 2 个函数?有没有一种方法可以在输入 txt1 的所有地方 txt1 之后自动传递 txt2。
Private Function isNumericOnly(ByVal txt1 As String, ByVal txt2 As String) As Boolean
Dim iLen As Integer
Dim iCtr As Integer
Dim sChar As String
iLen = Len(txt1)
If iLen > 0 Then
For iCtr = 1 To iLen
sChar = Mid(txt1, iCtr, 1)
If Not sChar Like "[0-9]" Then Return False
Next
Return True
End If
End Function
您可以使用一种验证方法:
Public Function DigitsOnly(text As String) As Boolean
Return text IsNot Nothing AndAlso text.All(AddressOf Char.IsDigit)
End Function
然后你把TextBoxes
的文本传给它:
Dim isValid As Boolean = DigitsOnly(txt1.Text) AndAlso DigitsOnly(txt2.Text)
注意方法returns True
如果你传递一个空字符串。如果您不想使用:
Return Not String.IsNullOrEmpty(text) AndAlso text.All(AddressOf Char.IsDigit)
如果您有许多要验证的控件(或字符串),您可以使用集合:
Dim allTextBoxes As TextBox() = {txt1, txt2, txt3}
Dim isValid = allTextBoxes.All(Function(txt) DigitsOnly(txt.Text))
我想通过此函数传递 txt1 和 txt2 来验证两个用户输入框,但是我一次只能传递一个参数,这意味着我必须创建 2 个函数?有没有一种方法可以在输入 txt1 的所有地方 txt1 之后自动传递 txt2。
Private Function isNumericOnly(ByVal txt1 As String, ByVal txt2 As String) As Boolean
Dim iLen As Integer
Dim iCtr As Integer
Dim sChar As String
iLen = Len(txt1)
If iLen > 0 Then
For iCtr = 1 To iLen
sChar = Mid(txt1, iCtr, 1)
If Not sChar Like "[0-9]" Then Return False
Next
Return True
End If
End Function
您可以使用一种验证方法:
Public Function DigitsOnly(text As String) As Boolean
Return text IsNot Nothing AndAlso text.All(AddressOf Char.IsDigit)
End Function
然后你把TextBoxes
的文本传给它:
Dim isValid As Boolean = DigitsOnly(txt1.Text) AndAlso DigitsOnly(txt2.Text)
注意方法returns True
如果你传递一个空字符串。如果您不想使用:
Return Not String.IsNullOrEmpty(text) AndAlso text.All(AddressOf Char.IsDigit)
如果您有许多要验证的控件(或字符串),您可以使用集合:
Dim allTextBoxes As TextBox() = {txt1, txt2, txt3}
Dim isValid = allTextBoxes.All(Function(txt) DigitsOnly(txt.Text))