不保留自定义类中属性值的更改 [英] Not retaining Changes to values of properties in a Custom Class

查看:73
本文介绍了不保留自定义类中属性值的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了此函数,以从自定义类的属性值中删除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.

推荐答案

您得到了,您对其进行了更改.现在放回去,即SetValue.

艾伦.
You got it, you changed it. Now put it back, i.e. SetValue.

Alan.


已解决的问题:

That fixed it:

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


您的代码没有"真的没有任何意义.但是,您要做的是获得一个字符串值的副本,从副本中删除所有vbCrLf,然后将其删除并且不对其进行任何操作.您只返回了传递给方法的值,所以是的,没有任何改变.
Your code doesn''t really make any sense. But, what you did was get a copy of a string value, remove all the vbCrLf''s from the copy, then drop it and not do anything with it. You returned only the value that was passed into the method, so yeah, nothing changed.


这篇关于不保留自定义类中属性值的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆