Bill,
| 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.
Why? A square peg (copy/cast) doesn't always fit in a round hole (whatever
you are *actually* attempting to do).
As you show you would need a widening to String, then a narrowing(or
widening) to your specific type.
Unfortunately (or is it fortunately!) VB doesn't apply 2 (user defined)
conversions in a row; Very few languages will apply multiple conversion
operators in a row.
You will need to apply one of the "conversion s" manually:
m_ClassA = m_ClassB.ToStri ng()
or
m_ClassA = CStr(m_ClassB)
However!! conversions here really don't feel appropriate!
| 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".
I would consider making ClassA & ClassB strategies that Base uses. ClassA &
ClassB would not have properties as much as a method (possibly a single
method) that given a name returned the value from the string (the substring
portion)...
| 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.
Is it your code generator or someone elses? If its your's it sounds like it
needs to be smarter...
Overall I would step back, look at what the process is really trying to do,
and pick a solution (algorithm) that solves what you are really trying to
do.
--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley -
http://www.tsbradley.net
"Bill foust" <Bi*******@disc ussions.microso ft.comwrote in message
news:EA******** *************** ***********@mic rosoft.com...
| 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 misunderstandin g
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.
| 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.
| <snip>
| >
| 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(DirectCa st(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(DirectCa st(Value, Base))
| 'get items that are specific for ClassA types
| '...
| End Sub
| >
| Dim m_ClassA as new ClassA
| dim m_ClassB as new ClassB
|
| m_ClassA.Assign (m_ClassB)
| >
| HTH,
| >
| Regards
| >
| Branco
| >
| >