Basic OOP Object Inheritance

  • Thread starter Michael Thompson
  • Start date
M

Michael Thompson

I am new to .Net and OOP techniques; I am not even certain that I using
the correct terminology here.

I believe that I want to know how to do inheritance.

I want to create a custom "string" class, "TSTString".
TSTString should behave exactly like a string except that I want to add
a couple methods like "public string ProperCase()", but still have the
TSTString object behave like string, and be able to pass them through as
string-type parameters.

Here is how I would want the class to behave:

static void Main()
{
string someText0;
string someText1;
TSTString someText2;

someText0 = someText2.ProperCase();
someText1 = someText2.SubString(1, 2);
MessageBox.Show(someText2);
}
 
P

Peter Duniho

I am new to .Net and OOP techniques; I am not even certain that I using
the correct terminology here.

I believe that I want to know how to do inheritance.

You may find these pages helpful:

http://msdn2.microsoft.com/en-us/library/ms173109.aspx
http://msdn2.microsoft.com/en-us/library/ms173149.aspx

You can easily do what you describe using inheritance. Though, you may
find that if that's the only method you intend to add, it's overkill to
create a whole new class, especially since to use it would mean either
replacing every use of "string" with "TSTString", or creating a whole new
"TSTString" instance any time you wanted to call that method. Neither
seem like particularly inviting scenarios to me, just to do one thing.

Note also that to make the inherited class most useful, you would want to
create new constructors with all of the same semantics as those that exist
for the String. There are a bunch of them, and without creating them, you
may well run into situations where you are forced to instantiate the base
String class just for the purpose of instantiating the derived class
moments later.

If you have a whole bunch of new functionality you want to add to the
String class, then the above may all be worthwhile and making a new class
that inherits String might make sense. But otherwise...

If all you want to do is be able to create a method called "ProperCase()"
that does something with a string and returns a new one, you may be better
off just creating that as a static method in some other class you've
already got (or create a new class where you put this sort of utility
method). You'll avoid all the inefficiencies that inheritance would cause
in this case.

Note that you should still learn about inheritance. It is often exactly
the right thing to do. I'm just saying that in this case, the example
seems sort of contrived and may not lead to better code by inheriting the
String class.

Pete
 
M

Mike Flynn

The problem with the string class is that it is a sealed class and therefore
cannot be inherited, so as is already stated you would have to create your
own method ( a static method in a seperate class) to do this, a quick search
on google would give plenty of pre-generated code from the army of late
night coders out there.
 
P

Peter Duniho

The problem with the string class is that it is a sealed class and
therefore cannot be inherited

lol...

Actually, as it may be evident from my reply, I forgot String was sealed.
Doh. You're right, inheritance won't work in that case.

Oh well...my other suggestion was reasonable anyway. :)
 
R

Rad [Visual C# MVP]

I am new to .Net and OOP techniques; I am not even certain that I using
the correct terminology here.

I believe that I want to know how to do inheritance.

I want to create a custom "string" class, "TSTString".
TSTString should behave exactly like a string except that I want to add
a couple methods like "public string ProperCase()", but still have the
TSTString object behave like string, and be able to pass them through as
string-type parameters.

Here is how I would want the class to behave:

static void Main()
{
string someText0;
string someText1;
TSTString someText2;

someText0 = someText2.ProperCase();
someText1 = someText2.SubString(1, 2);
MessageBox.Show(someText2);
}

C# 3 has extension methods that will allow you to do that sort of
thing ...
 
M

Michael Thompson

Wow, thank you all for your helpful comments! I've never posted here
before, and it was an incredible response in one day.
 
M

Mark Rae [MVP]

Michael,
Wow, thank you all for your helpful comments! I've never posted here
before, and it was an incredible response in one day.

You got good responses because:

a) your post was very well written, and specific

b) you mentioned your current level of knowledge, so the responses were
phrased accordingly (everybody was a newbie once - no shame at all in that!)

c) your post clearly showed that you have thought about the issue, as
opposed to "will somebody please write my code for me because I can't be
bothered..."

With that sort of approach and attitude, you will certainly not be a newbie
for long... :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top