Re: Creating a derived object from a base object

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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@xxxxxxxxxxxxxxxxx> wrote in message
news:1165348288.155729.84120@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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" <benliu@xxxxxxxxxxxxxxxxx> wrote in message
news:1165312955.690968.322930@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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




.



Relevant Pages

  • Re: how do i import a text file with line wraps into a table
    ... Dim Directory As String ... Dim MyString As String ... Set dbs = CurrentDb ...
    (microsoft.public.access.externaldata)
  • Re: how do i import a text file with line wraps into a table
    ... Dim dbs As Database, rst As Recordset ... Dim Directory As String ... Dim MyString As String ... Set dbs = CurrentDb ...
    (microsoft.public.access.externaldata)
  • Re: how do i import a text file with line wraps into a table
    ... Dim dbs As Database, rst As Recordset ... Dim Directory As String ... Dim MyString As String ... Set dbs = CurrentDb ...
    (microsoft.public.access.externaldata)
  • Re: Email Merge in Word
    ... WHERE mystring is not null AND trim'' ... If the address looks blank but len) is> 0 then the field probably contains invisible non-space characters. ... Different software packages can treat "null", "a string set to ''", and "a string containing white space differently, and may also treat variable-length and fixed-length data differently in this respect. ...
    (microsoft.public.word.mailmerge.fields)
  • Re: Creating a derived object from a base object
    ... namespace MyString ... public class MyString ... private String _s; ... can't cast the result to a MyString since I can't cast a base type to ...
    (microsoft.public.dotnet.languages.csharp)