本文介绍了不保留自定义类中属性值的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我创建了此函数,以从自定义类的属性值中删除vbCrLf:(vTransaction)
I created this function to remove vbCrLf from the values of properties in a Custom Class:(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中的值所做的更改.
Throughout testing I can verify that vbCrLf is being replaced with a space as expected. When I re-examine vTransaction the changes have not been retained and the vbCrlf''s are still there.
What do I need to do to retain the changes being made to values in vTransaction.
推荐答案
Public Function ValidateTransaction(ByRef vTransaction)
Dim property1 As Object
Dim value1 As Object
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.
p.SetValue(sTransaction, value1 = value1.ToString.Replace(vbCrLf, " "), Nothing)
'MsgBox("After .Replace:" & vbNewLine & value1.ToString) '// FOR TESTING.
End If
End If
End If
Next
Return sTransaction
End Function
这篇关于不保留自定义类中属性值的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!