我创建这个函数是为了从自定义类的属性值中删除vbCrLf:(vTransaction)
Public Function ValidateTransaction(ByRef vTransaction)
Dim property1 As String
Dim value1 As String
For Each p As System.Reflection.PropertyInfo In vTransaction.GetType().GetProperties()
If p.CanRead Then
property1 = p.Name '// FOR TESTING to identify Property Name
value1 = p.GetValue(vTransaction, Nothing)
If (TypeOf value1 Is String) Then
If value1 <> " " And value1 <> "" Then
'MsgBox("Before .Replace:" & vbNewLine & value1.ToString) '// FOR TESTING.
value1 = value1.ToString.Replace(vbCrLf, " ")
'MsgBox("After .Replace:" & vbNewLine & value1.ToString) '// FOR TESTING.
End If
End If
End If
Next
Return vTransaction
End Function
在整个测试过程中,我可以验证vbCrLf是否如预期的那样被一个空格所取代。当我重新检查vTransaction时,更改没有被保留,vbCrlf仍然存在。
我需要做些什么来保留对vTransaction中的值所做的更改。
您不会再次设置属性值。您只是在创建一个字符串,之后就不用了。你最里面的If块应该是这样的:
If value1 <> " " And value1 <> "" Then
'MsgBox("Before .Replace:" & vbNewLine & value1.ToString) '// FOR TESTING.
p.SetValue(vTransaction, value1.ToString.Replace(vbCrLf, " "), Nothing)
'MsgBox("After .Replace:" & vbNewLine & value1.ToString) '// FOR TESTING.
End If