473,396 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Basic OOP Object Inheritance

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


*** Sent via Developersdex http://www.developersdex.com ***
Jul 6 '07 #1
5 1307
On Fri, 06 Jul 2007 14:16:42 -0700, Michael Thompson
<su*****@totalstream.comwrote:
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
Jul 6 '07 #2
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.

"Michael Thompson" <su*****@totalstream.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
>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);
}


*** Sent via Developersdex http://www.developersdex.com ***
Jul 6 '07 #3
On Fri, 06 Jul 2007 16:31:26 -0700, Mike Flynn <su******@btinternet.com>
wrote:
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. :)
Jul 7 '07 #4
On Fri, 06 Jul 2007 14:16:42 -0700, Michael Thompson
<su*****@totalstream.comwrote:
>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);
}


*** Sent via Developersdex http://www.developersdex.com ***
C# 3 has extension methods that will allow you to do that sort of
thing ...

--
http://bytes.thinkersroom.com
Jul 7 '07 #5
Wow, thank you all for your helpful comments! I've never posted here
before, and it was an incredible response in one day.

*** Sent via Developersdex http://www.developersdex.com ***
Jul 7 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Squeamz | last post by:
Hello, Say I create a class ("Child") that inherits from another class ("Parent"). Parent's destructor is not virtual. Is there a way I can prevent Parent's destructor from being called when a...
4
by: jm | last post by:
Consider: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconwhenshouldiimplementinterfacesinmycomponent.asp // Code for the IAccount interface module. public...
3
by: Robert Abi Saab | last post by:
Hi everyone. I just finished a course on PostgreSQL and I found out that PostgreSQL doesn't provide any object relational features (as claimed in the official documentation), except table...
4
by: MikeB | last post by:
I've been all over the net with this question, I hope I've finally found a group where I can ask about Visual Basic 2005. I'm at uni and we're working with Visual Basic 2005. I have some books, ...
7
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears...
6
by: burningodzilla | last post by:
Hi all - I'm preparing to dive in to more complex application development using javascript, and among other things, I'm having a hard time wrapping my head around an issues regarding "inheritance"...
3
by: jacobstr | last post by:
I've noticed Object.extend used in a few different ways and I'm having trouble distinguishing why certain usages apply to a given situation. On line 804 Ajax.Base is defined as follows: ...
28
by: Randy Reimers | last post by:
(Hope I'm posting this correctly, otherwise - sorry!, don't know what else to do) I wrote a set of programs "many" years ago, running in a type of basic, called "Thoroughbred Basic", a type of...
14
by: MartinRinehart | last post by:
Working on parser for my language, I see that all classes (Token, Production, Statement, ...) have one thing in common. They all maintain start and stop positions in the source text. So it seems...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.