By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,474 Members | 966 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,474 IT Pros & Developers. It's quick & easy.

Creating a derived object from a base object

P: n/a
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

Dec 5 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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();
}
}
Dec 5 '06 #2

P: n/a
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
"benliu" <be****@iasportsmedia.comwrote in message
news:11**********************@j44g2000cwa.googlegr oups.com...
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

Dec 5 '06 #3

P: n/a
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?
Peter Bradley wrote:
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
"benliu" <be****@iasportsmedia.comwrote in message
news:11**********************@j44g2000cwa.googlegr oups.com...
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
Dec 5 '06 #4

P: n/a
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!

Marc Gravell wrote:
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();
}
}
Dec 5 '06 #5

P: n/a
"benliu" <be****@iasportsmedia.comwrote in message
news:11**********************@n67g2000cwd.googlegr oups.com...
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?
Dec 5 '06 #6

P: n/a
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?

Mark Wilden wrote:
"benliu" <be****@iasportsmedia.comwrote in message
news:11**********************@n67g2000cwd.googlegr oups.com...
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?
Dec 5 '06 #7

P: n/a
benliu <be****@iasportsmedia.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 5 '06 #8

P: n/a
By passing the string parameter to the parent constructor.
Peter

"benliu" <be****@iasportsmedia.comwrote in message
news:11*********************@79g2000cws.googlegrou ps.com...
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?
Peter Bradley wrote:
>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
"benliu" <be****@iasportsmedia.comwrote in message
news:11**********************@j44g2000cwa.googleg roups.com...
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

Dec 6 '06 #9

P: n/a
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
"benliu" <be****@iasportsmedia.comwrote in message
news:11*********************@79g2000cws.googlegrou ps.com...
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?
Peter Bradley wrote:
>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
"benliu" <be****@iasportsmedia.comwrote in message
news:11**********************@j44g2000cwa.googleg roups.com...
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

Dec 6 '06 #10

P: n/a
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
"Peter Bradley" <pb******@uwic.ac.ukwrote in message
news:Ox**************@TK2MSFTNGP02.phx.gbl...
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
"benliu" <be****@iasportsmedia.comwrote in message
news:11*********************@79g2000cws.googlegrou ps.com...
>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?
Peter Bradley wrote:
>>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
"benliu" <be****@iasportsmedia.comwrote in message
news:11**********************@j44g2000cwa.google groups.com...
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


Dec 6 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.