B
benliu
Is there an easy/special way to turn a base object into a derived
object? So for example, given the following:
class MyString : String
{
....
}
(ok, so String is sealed, but just play along)
MyString myString = new MyString();
say i want to convert myString to upper case - sure would be nice to
use the ToUpper method of the base String object - but doesn't seem
like i can:
myString.ToUpper() returns a String, not a MyString
So how can i use ToUpper() if i need the result to be a MyString? I
can't cast the result to a MyString since I can't cast a base type to a
derived type. Are all the String methods that return a new String
instance useless now?
I guess, even more simply, my question is what is the easiest way to
convert a base type to a derived type? How do i convert a String to a
MyString? Given that MyString is a String type, is there any special
way to convert String into MyString? Or is it literally 2 completely
different objects now, just like a String and a Foo Object. And i need
to write a explicit cast method to do the conversion?
Thanks
object? So for example, given the following:
class MyString : String
{
....
}
(ok, so String is sealed, but just play along)
MyString myString = new MyString();
say i want to convert myString to upper case - sure would be nice to
use the ToUpper method of the base String object - but doesn't seem
like i can:
myString.ToUpper() returns a String, not a MyString
So how can i use ToUpper() if i need the result to be a MyString? I
can't cast the result to a MyString since I can't cast a base type to a
derived type. Are all the String methods that return a new String
instance useless now?
I guess, even more simply, my question is what is the easiest way to
convert a base type to a derived type? How do i convert a String to a
MyString? Given that MyString is a String type, is there any special
way to convert String into MyString? Or is it literally 2 completely
different objects now, just like a String and a Foo Object. And i need
to write a explicit cast method to do the conversion?
Thanks