Adding a function to the String class?

  • Thread starter Thread starter Rob Meade
  • Start date Start date
R

Rob Meade

Hi all,

I have written a small ProperCase function which I would like to make
available to our team at work through our common class library.

A colleague mentioned that I could write a new class which derived from the
String class, add the function there and then we would all use the new class
which contained my function and all of the others etc....sounded relatively
straight forward...

My problem is that I figured immediately after creating a new class I'd need
a line that Inherits...however, having check the documentation for the
String class, when I try adding:

Inherits System.String

I just a wiggly line in Visual Studio....If I add it with the Imports
statement it works fine...

Can anyone suggest what I might be doing wrong please...

My aim is to be able to do something like this:

Dim myString as String

myString = "HELLO WORLD"

myString = myString.ToProperCase()

Response.Write myString


The end result will appear as

Hello World


Please note I've done the function, just need to tie it into something...any
help appreciated..

Regards

Rob
 
Hi,

What i understand is that you dont want to use import statement and still
wish that ur string class should work. (May be i am wrong)

With respect to what i Understand,I think you should create a seperate class
library containing your string class and try adding your assembly in GAC and
then give it a try.

Hope that helps
 
Rob,

The very idea doesn't sound right to me. String is probably the most common
class in dotnet. It is used in all sorts of scenarios all places around. To
replace is with another class just for the sake of one little method??? How
are you going to enforce using your new class by everyone in all cases?
Also, you will have to care about type casting. May be you should consider
having a simple utility class with static method ProperCase at apply it as
needed?

Eliyahu
 
...
With respect to what i Understand,I think you should create a seperate
class
library containing your string class and try adding your assembly in GAC
and
then give it a try.

We have a seperate class library already, which already has various classes
and common functionality in it - we all incorporate this into each of our
applications. It is also already in the GAC, my question is simply how I
get a new class which includes all of the existing functionality from the
String class, and adds my own ProperCase method.

Regards

Rob
 
...
The very idea doesn't sound right to me.
ok

String is probably the most common class in dotnet. It is used in all
sorts of scenarios all places around. To
replace is with another class just for the sake of one little method???

I will admit I hadn't given any thought to the size of the existing class.
How are you going to enforce using your new class by everyone in all
cases?

We have a common class library which we all use (only 7 of us here in the
team), we use this on each project, so by adding my new string class to it
we'd all have access to it.
Also, you will have to care about type casting.

I dont know what that means?
May be you should consider having a simple utility class with static
method ProperCase at apply it as
needed?

Based on what you've suggested above about the string class that might well
be the way we have to go...I was thinking mainly from a costmetic
perspective when I asked the question, I was hoping that my colleagues would
be able to use it like:

myString = myString.ToProperCase

Instead of

myString = ProperCase(myString)

etc

Regards

Rob
 
Use inheritance.

Rob Meade said:
...


We have a seperate class library already, which already has various classes
and common functionality in it - we all incorporate this into each of our
applications. It is also already in the GAC, my question is simply how I
get a new class which includes all of the existing functionality from the
String class, and adds my own ProperCase method.

Regards

Rob
 
Did you know that there already is a function in the framework to convert to
title case? It's ToTitleCase(), I'll leave it up to you to find the
namespace that it's in.
 
...
Class String is sealed. This means you can't derive from it.

Hi Eliyahu,

Yeah, tis a shame :o/ I guess there's a good reason why - be nice to know
what that was - lol - oh well...

Cheers

Rob
 
because the string class is sealed (no inheritance allowed - for
performance), the best you can do is a class of static (public shared in vb)
functions (like vb does with left, right, mid) of your new routines:

-- bruce (sqlwork.com)


| Hi all,
|
| I have written a small ProperCase function which I would like to make
| available to our team at work through our common class library.
|
| A colleague mentioned that I could write a new class which derived from
the
| String class, add the function there and then we would all use the new
class
| which contained my function and all of the others etc....sounded
relatively
| straight forward...
|
| My problem is that I figured immediately after creating a new class I'd
need
| a line that Inherits...however, having check the documentation for the
| String class, when I try adding:
|
| Inherits System.String
|
| I just a wiggly line in Visual Studio....If I add it with the Imports
| statement it works fine...
|
| Can anyone suggest what I might be doing wrong please...
|
| My aim is to be able to do something like this:
|
| Dim myString as String
|
| myString = "HELLO WORLD"
|
| myString = myString.ToProperCase()
|
| Response.Write myString
|
|
| The end result will appear as
|
| Hello World
|
|
| Please note I've done the function, just need to tie it into
something...any
| help appreciated..
|
| Regards
|
| Rob
|
|
 
Hi,
I am sorry, I forgot that String class is not inheritable.
In such a case you may try the following.

1. Create a new class with static methods of your requirement
2. Take time and effort to create your own version of string class with same
function names and use the built in functions inside it. (Its tedious , I
say) plus your own functions like ProperCase.

Hope that helps
 
Rob,

In some cases classes are sealed for performance reasons. Since String is a
very common class, it makes sense to optimize it's performance. Not sure if
it is the real reason in the case though.

Eliyahu
 
...
Did you know that there already is a function in the framework to convert
to
title case? It's ToTitleCase(), I'll leave it up to you to find the
namespace that it's in.

OMG! Hello Scott,

Thanks for the info - I've just tried this, seems a bit overly complex to
use, but the results are quite good...

The thing with mine was that it contains a set of ignore words, because this
is going to be used in a medical profession I have set a series of common
words/abbreviations that should always appear a certain way - ToTitleCase
came a little unstuck on some of these, although NHS was ok...NPfIT for
example was not..

Thanks for the info though.

Regards

Rob
 

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

Back
Top