Creating a derived object from a base object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • benliu

    Creating a derived object from a base object

    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.ToUppe r() 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

  • Marc Gravell

    #2
    Re: Creating a derived object from a base object

    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( );
    }
    }


    Comment

    • Peter Bradley

      #3
      Re: Creating a derived object from a base object

      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(mystri ng.ToUpper());

      HTH


      Peter


      "benliu" <benliu@iasport smedia.comwrote in message
      news:1165312955 .690968.322930@ j44g2000cwa.goo glegroups.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.ToUppe r() 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
      >

      Comment

      • benliu

        #4
        Re: Creating a derived object from a base object

        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(mystri ng.ToUpper());
        >
        HTH
        >
        >
        Peter
        >
        >
        "benliu" <benliu@iasport smedia.comwrote in message
        news:1165312955 .690968.322930@ j44g2000cwa.goo glegroups.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.ToUppe r() 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

        Comment

        • benliu

          #5
          Re: Creating a derived object from a base object

          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.ToUppe r 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( );
          }
          }

          Comment

          • Mark Wilden

            #6
            Re: Creating a derived object from a base object

            "benliu" <benliu@iasport smedia.comwrote in message
            news:1165348714 .304784.231430@ n67g2000cwd.goo glegroups.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.ToUppe r 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?


            Comment

            • benliu

              #7
              Re: Creating a derived object from a base object

              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" <benliu@iasport smedia.comwrote in message
              news:1165348714 .304784.231430@ n67g2000cwd.goo glegroups.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.ToUppe r 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?

              Comment

              • Jon Skeet [C# MVP]

                #8
                Re: Creating a derived object from a base object

                benliu <benliu@iasport smedia.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 - <skeet@pobox.co m>
                http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
                If replying to the group, please do not mail me too

                Comment

                • Peter Bradley

                  #9
                  Re: Creating a derived object from a base object

                  By passing the string parameter to the parent constructor.


                  Peter

                  "benliu" <benliu@iasport smedia.comwrote in message
                  news:1165348288 .155729.84120@7 9g2000cws.googl egroups.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(mystri ng.ToUpper());
                  >>
                  >HTH
                  >>
                  >>
                  >Peter
                  >>
                  >>
                  >"benliu" <benliu@iasport smedia.comwrote in message
                  >news:116531295 5.690968.322930 @j44g2000cwa.go oglegroups.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.ToUppe r() 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
                  >
                  >

                  Comment

                  • Peter Bradley

                    #10
                    Re: Creating a derived object from a base object

                    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" <benliu@iasport smedia.comwrote in message
                    news:1165348288 .155729.84120@7 9g2000cws.googl egroups.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(mystri ng.ToUpper());
                    >>
                    >HTH
                    >>
                    >>
                    >Peter
                    >>
                    >>
                    >"benliu" <benliu@iasport smedia.comwrote in message
                    >news:116531295 5.690968.322930 @j44g2000cwa.go oglegroups.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.ToUppe r() 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
                    >
                    >

                    Comment

                    • Peter Bradley

                      #11
                      Re: Creating a derived object from a base object

                      Try this:

                      Create a class:

                      using System;
                      using System.Collecti ons.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.ToChar Array());
                      }

                      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.Collecti ons.Generic;
                      using System.Text;

                      namespace NewsGroupTest
                      {
                      class MyStringTest
                      {
                      static void Main(string[] args)
                      {
                      MyString.MyStri ng ms = new MyString.MyStri ng("a lower case
                      string");
                      ms = ms.UpperCase();
                      Console.WriteLi ne(ms.s);
                      Console.ReadLin e();
                      }
                      }
                      }

                      That should do it.


                      Peter


                      "Peter Bradley" <pbradley@uwic. ac.ukwrote in message
                      news:OxPrqRSGHH A.3780@TK2MSFTN GP02.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" <benliu@iasport smedia.comwrote in message
                      news:1165348288 .155729.84120@7 9g2000cws.googl egroups.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(mystri ng.ToUpper());
                      >>>
                      >>HTH
                      >>>
                      >>>
                      >>Peter
                      >>>
                      >>>
                      >>"benliu" <benliu@iasport smedia.comwrote in message
                      >>news:11653129 55.690968.32293 0@j44g2000cwa.g ooglegroups.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.ToUpp er() 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
                      >>
                      >>
                      >
                      >

                      Comment

                      Working...