Creating a derived object from a base object

  • Thread starter Thread starter benliu
  • Start date Start date
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
 
You can never change the type of an object once created. You can however
provide implicit or explicit cast operators that convert between the two
types, and you can provide "new" implementations of methods to change the
return types, but this should be used with caution as any callers using a
subclass object thru a baseclass reference will use the *old* methods.

The following shows a way to use "virtual" and "new" methods to get around
this; called on a baseclass instance always returns a baseclass; called on a
subclass instance always returns a subclass - however note that it is typed
by the reference - so if the caller is referencing it as "BaseClass" it will
see a baseclass, and it if is referencing it as a "SubClass" it will see a
subclass. Either way the instance is still a SubClass. The cast in the "new"
method allows further subclasses to continue to override InnerSomething.

Marc

public class BaseClass {
public BaseClass Something() {
return InnerSomething();
}

protected virtual BaseClass InnerSomething() {
return new BaseClass();
}
}

public class SubClass : BaseClass {
protected override BaseClass InnerSomething() {
return new SubClass();
}

public new SubClass Something() {
return (SubClass) InnerSomething();
}
}
 
One solution that occurs is to do this, assuming you have a constructor that
takes a string, which seems reasonable:

MyString mystring = new MyString("a lower case string");
mystring = new MyString(mystring.ToUpper());

HTH


Peter
 
Ok, so your suggested solution is the real issue i'm getting at.

How WOULD you write a MyString constructor that takes a string? You
don't have access to the underlying implementation of String (and even
if you can think of a hacky way, this is just an example, and let's
assume that the String class is a black box). So therefore how would
the constructor create itself from an instance of a String?

So is this now different than in a case that you're trying to create an
object from a completely unrelated object? Seems that since MyString
does not add any new properties to String, there should be a quick easy
way to construct a MyString object from a String object. Maybe not?
 
Let me further clarify - suppose I want to convert my MyString to
uppercase. String class is a blackbox - i cannot modify it whatsoever.
I don't want to write a separate MyString ToUpper method when String
already implements it. However, String.ToUpper returns a String, not a
MyString. And similarly, even if i was to write my own
MyString.ToUpper implementation, how would i do it? I don't have
access to the implementations details of String (using String-MyString
as an example - in my real world case, i don't have access to the
implementatino of the base class, so hacking around this using some
special String method will not help)

Thanks!
 
benliu said:
Let me further clarify - suppose I want to convert my MyString to
uppercase. String class is a blackbox - i cannot modify it whatsoever.
I don't want to write a separate MyString ToUpper method when String
already implements it. However, String.ToUpper returns a String, not a
MyString. And similarly, even if i was to write my own
MyString.ToUpper implementation, how would i do it? I don't have
access to the implementations details of String (using String-MyString
as an example - in my real world case, i don't have access to the
implementatino of the base class, so hacking around this using some
special String method will not help)

Would a class that wrapped String instead of deriving from it help?
 
Yes, i was thinking about that.

It seems then that i am not crazy - there is no simple way to create a
derived object out of a base object - whether through casting or
creation of new object... Is that the consensus?
 
benliu said:
Ok, so your suggested solution is the real issue i'm getting at.

How WOULD you write a MyString constructor that takes a string? You
don't have access to the underlying implementation of String (and even
if you can think of a hacky way, this is just an example, and let's
assume that the String class is a black box). So therefore how would
the constructor create itself from an instance of a String?

So is this now different than in a case that you're trying to create an
object from a completely unrelated object? Seems that since MyString
does not add any new properties to String, there should be a quick easy
way to construct a MyString object from a String object. Maybe not?

As you're not adding any data to string, you should just write a class
with static methods which can be called with instances of strings. In
C# 3.0 this will be made more elegant (arguably) with extension
methods.
 
Ah! I think I get what you're on about.

I thought you were ignoring, for the purposes of this discussion, the fact
that you couldn't inherit from the string class - but now I see that you're
not.

In the case where you can't inherit from the parent, then you'll just have
to provide a wrapper and delegate to the wrapped uninheritable class.
Someone's already suggested this, I think.

Cheers


Peter
 
Try this:

Create a class:

using System;
using System.Collections.Generic;
using System.Text;

namespace MyString
{
public class MyString
{
private String _s;

public String s
{
get
{
return _s;
}
set
{
_s = value;
}
}

public MyString(String s)
{
_s = new String(s.ToCharArray());
}

public MyString UpperCase()
{
return new MyString(this.s.ToUpper());
}
}
}

Create a console app (with a reference to the new class) to exercise the it:

using System;
using System.Collections.Generic;
using System.Text;

namespace NewsGroupTest
{
class MyStringTest
{
static void Main(string[] args)
{
MyString.MyString ms = new MyString.MyString("a lower case
string");
ms = ms.UpperCase();
Console.WriteLine(ms.s);
Console.ReadLine();
}
}
}

That should do it.


Peter
 
Back
Top