Re: Can anyone help with proper use of operator CType in VB.net?
- From: Bill foust <Billfoust@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sat, 22 Jul 2006 12:34:01 -0700
Thanks for the quick replies! Unfortunately neither suggestion will work for
me.
I have to use an operator = to do this assignment and cannot use other
methods such as you Assign method. That is unless I'm misunderstanding what
you are saying and Assign() exists on all objects and is used by the compiler
to implement operator=. The help docs didn't seem to suggest that however. :)
You see, Base contains a string of data and the derived classes simply
implement a variety of properties to access specific substring portions of
the string in the class in order to give them a "pretty name". Also, because
this code is the result of an automatic code generation process, it doesnt
really have any knowledge about the types of these derived classes. For these
reasons, I'm kind of stuck using operator = to accomplish both a deep copy
(not a reference copy) and a type cast in one operation.
I was hoping the usage of operator Ctype for a widening to string and
another ctype for widening to the specific class (example below)...
public ClassA : inherits Base
....
public shared overrides widening operator CType(src as ClassA) as string
return src.toString()
end operator
public shared overrides widening operator Ctype(srtc as string) as ClassA
return new ClassA(src)
end operator
would produce an effect similiar to the following...
m_ClassA = new ClassA(m_ClassB.ToString())
Bill
"Branco Medeiros" wrote:
Bill foust wrote:.
<snip>
I have a base class that is common to many other classes.<snip>
public class Base
...
end class
I have 2 seperate classes that inherit from base
public ClassA : inherits Base
...
End Class
public ClassB : inherits Base
...
End Class
Now, I have an instance of ClassA and ClassB and I'm trying to assign the
ClassB instance to ClassA.
Dim m_ClassA as new ClassA
dim m_ClassB as new ClassB
m_ClassA = m_ClassB
Obviously, this won't work as is because ClassA is not the same as ClassB.
So we turn to operator overloading. I really want to overload the assignment
operator, but I read that VB.Net doesn't support this. So we have to overload
operator CType.
One possible solution would be to have a virtual (Overridable) Assign
method in Base:
Public Overridable Sub Assign(Value As Base)
'...
End Sub
And have each class get from the value whatever they want:
'On ClassB
Public Overrides Sub Assign(Value As Base)
'get items from a Base Class
End Sub
Public Overridable Overloads Sub Assign(Value As ClassB)
Assign(DirectCast(Value, Base))
'get items that are specific for ClassB types
'...
End Sub
'On ClassA
Public Overrides Sub Assign(Value As Base)
'get items from a Base Class
End Sub
Public Overridable Overloads Sub Assign(Value As ClassA)
Assign(DirectCast(Value, Base))
'get items that are specific for ClassA types
'...
End Sub
Dim m_ClassA as new ClassAm_ClassA.Assign(m_ClassB)
dim m_ClassB as new ClassB
HTH,
Regards
Branco
- Follow-Ups:
- Re: Can anyone help with proper use of operator CType in VB.net?
- From: Branco Medeiros
- Re: Can anyone help with proper use of operator CType in VB.net?
- From: Jay B. Harlow [MVP - Outlook]
- Re: Can anyone help with proper use of operator CType in VB.net?
- References:
- Re: Can anyone help with proper use of operator CType in VB.net?
- From: Branco Medeiros
- Re: Can anyone help with proper use of operator CType in VB.net?
- Prev by Date: Re: Can anyone help with proper use of operator CType in VB.net?
- Next by Date: Help on DataSets
- Previous by thread: Re: Can anyone help with proper use of operator CType in VB.net?
- Next by thread: Re: Can anyone help with proper use of operator CType in VB.net?
- Index(es):
Relevant Pages
|