G
Guest
I'm running into a situation there I think an operator overload would solve
the issue, but I'm unable to make it work for some reason. If anyone can
help here I would appreciate it.
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.
If each ClassA and ClassB have a constructor that takes a string and each
class also had an operator CType that would convert the class instance to a
string, shouldn't the assignment become valid?
public class ClassA inherits Base
public sub New (param as sting)
....
end sub
public shared overloads operator CType(byval obj as ClassA() as string
return obj.ToString()
end sub
end class
If this is not valid, could someone please share what is wrong about this?
the operator CType will compile successfully, but the results are not desired
and the compile still flags it as an error.
unforunately the derived class (ClassA and ClassB) do no know about each
other so we can't put any more specific types into the class definitions.
thanks
bill
the issue, but I'm unable to make it work for some reason. If anyone can
help here I would appreciate it.
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.
If each ClassA and ClassB have a constructor that takes a string and each
class also had an operator CType that would convert the class instance to a
string, shouldn't the assignment become valid?
public class ClassA inherits Base
public sub New (param as sting)
....
end sub
public shared overloads operator CType(byval obj as ClassA() as string
return obj.ToString()
end sub
end class
If this is not valid, could someone please share what is wrong about this?
the operator CType will compile successfully, but the results are not desired
and the compile still flags it as an error.
unforunately the derived class (ClassA and ClassB) do no know about each
other so we can't put any more specific types into the class definitions.
thanks
bill