功能参数列表visual basic

时间:2014-07-24 16:38:09

标签: vb.net

我试图将我在函数中收到的参数列表保存到变量中。例如:

Function fTest(xVal1 as Integer, xVal2 as Integer) as String

   wListParams = "xVal1:" & xVal 1 & "#" & "xVal2:" & xVal2

End Function

如果发生错误并发送邮件,我想使用此列表。

我在寻找它是一种构建此String的方法,而无需在每个函数中编写每个案例(超过1000个)。

请帮忙!

感谢!!!

1 个答案:

答案 0 :(得分:0)

您想将所有参数连接成一个字符串吗?如果是这样,试试这个。

Imports System.Text

Public Function BuildParametersString(ByVal ParamArray parameters() As Integer) As String
    Dim sb As New StringBuilder()
    For i As Integer = 0 To parameters.Count() - 1
        sb.Append(String.Format("xVal{0}:{1}#", i + 1, parameters(i)))
    Next
    Return sb.ToString()
End Function

Private Sub test()
    Dim param1 As Integer = 1, param2 As Integer = 2
    ' passing individual parameters
    Dim s1 As String = BuildParametersString(param1, param2)
    ' passing paramaters in an array
    Dim s2 As String = BuildParametersString({param1, param2})
End Sub