Reputation: 744
I have a class named Foo
with properties like this:
Public Class Foo(Of T)
public Property Value as T
public Property Bar as Boolean
End Class
I have a type conversion function overload :
Public Shared Narrowing Operator CType(ins As [Foo](Of T)) As T
Return ins.Value
End Operator
Public Shared Widening Operator CType(prop As T) As [Foo](Of T)
Return New Foo(Of T) With {.Value = prop}
End Operator
I using my class like this:
private Sub someSub()
Dim f as new Foo(of String)
f.Bar = True
f = "This is The Text"
// when doing this I lose the `Bar` beacuase of `Return New Foo(Of T) With {.Value = rightSide}` on `Widening` overload
End Sub
is there any way to keep other properties of the class?
Upvotes: 1
Views: 694
Reputation: 54417
How could there be? When you assign to f
you are creating a new Foo(Of T)
object that knows nothing about the Bar
property of the object already assigned to that variable. You'd have to set the Bar
property of the new object inside the operator but it knows nothing about the variable you're assigning the result to so it can't get that value either. To keep that property value you would have to first get it and you have no way to get it other than the obvious manual retrieval beforehand and manual resetting afterwards.
Upvotes: 1