S
Shannon Richards
Hello: I have a problem using ByRef arguments with Option Strict ON. I have
built a generic sub procedure "ChangeValue()" to change the value of an
argument if the new value is not the same as the original value...To
accommodate all variable types I made the arguments in ChangeValue() of type
object...I then check the typecode and do the correct comparison etc...
With Option Strict ON I have to cast the arguments to the generic object
type in the call to ChangeValue()
The problem is that the value of a ByRef argument does not change even
though ChangeValue modifies the argument??? Does CObj(lsValue) actually
return a new object reference?
What's happening here???
Any insight will be greatly appreciated!!!
- Shannon
'*********************
'* Example Code:
'*********************
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim ls_Value As String = "1"
Dim lo_Object As Object = "1"
Console.WriteLine(ls_Value)
ChangeValue(CObj(ls_Value), "2")
Console.WriteLine(ls_Value)
Console.WriteLine("***")
Console.WriteLine(lo_Object)
ChangeValue(lo_Object, "2")
Console.WriteLine(lo_Object)
End Sub
Private Sub ChangeValue(ByRef ao_Value As Object, ByVal ao_NewValue As
Object)
Dim le_TypeCode As TypeCode = Type.GetTypeCode(ao_Value.GetType)
Select Case le_TypeCode
Case TypeCode.String
If (CStr(ao_Value) <> CStr(ao_NewValue)) Then
ao_Value = ao_NewValue
End If
End Select
End Sub
built a generic sub procedure "ChangeValue()" to change the value of an
argument if the new value is not the same as the original value...To
accommodate all variable types I made the arguments in ChangeValue() of type
object...I then check the typecode and do the correct comparison etc...
With Option Strict ON I have to cast the arguments to the generic object
type in the call to ChangeValue()
The problem is that the value of a ByRef argument does not change even
though ChangeValue modifies the argument??? Does CObj(lsValue) actually
return a new object reference?
What's happening here???
Any insight will be greatly appreciated!!!
- Shannon
'*********************
'* Example Code:
'*********************
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim ls_Value As String = "1"
Dim lo_Object As Object = "1"
Console.WriteLine(ls_Value)
ChangeValue(CObj(ls_Value), "2")
Console.WriteLine(ls_Value)
Console.WriteLine("***")
Console.WriteLine(lo_Object)
ChangeValue(lo_Object, "2")
Console.WriteLine(lo_Object)
End Sub
Private Sub ChangeValue(ByRef ao_Value As Object, ByVal ao_NewValue As
Object)
Dim le_TypeCode As TypeCode = Type.GetTypeCode(ao_Value.GetType)
Select Case le_TypeCode
Case TypeCode.String
If (CStr(ao_Value) <> CStr(ao_NewValue)) Then
ao_Value = ao_NewValue
End If
End Select
End Sub