473,385 Members | 1,610 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,385 software developers and data experts.

How to add my own Method to existing class?

In C#, the String class has a lot of Methods, such as "mystring".Trim();

I want to add my own Method so that the whole web application can use it.
What I want is:

"mystring".GetIt();

How do I do it? How do I override the String class so that the whole
application knows about the change? I don't think just creating a new class
which inherits from String class would work, right?

Thanks..


Nov 18 '05 #1
2 1498
Matt Doggie <Ma********@nosuchmail.com> wrote:
In C#, the String class has a lot of Methods, such as "mystring".Trim();

I want to add my own Method so that the whole web application can use it.
What I want is:

"mystring".GetIt();

How do I do it? How do I override the String class so that the whole
application knows about the change? I don't think just creating a new class
which inherits from String class would work, right?


Indeed it wouldn't, and you can't do that anyway as String is sealed.

You can't add methods to existing classes in .NET.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 18 '05 #2
You cannot do it by subclassing the String class (it is sealed and even if
it was not, strings would be instantiated in System.String, not in your
class).
All you can do is create a helper class with static methods:

class MyHelper {
public static string GetIt(string str) { /** your stuff here **/ }
}

you call it as MyHelper.GetIt("mystring");

Bruno.

"Matt Doggie" <Ma********@nosuchmail.com> a écrit dans le message de
news:3s*****************@twister01.bloor.is.net.ca ble.rogers.com...
In C#, the String class has a lot of Methods, such as "mystring".Trim();

I want to add my own Method so that the whole web application can use it.
What I want is:

"mystring".GetIt();

How do I do it? How do I override the String class so that the whole
application knows about the change? I don't think just creating a new class which inherits from String class would work, right?

Thanks..

Nov 18 '05 #3

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

Similar topics

31
by: Chris S. | last post by:
Is there a purpose for using trailing and leading double underscores for built-in method names? My impression was that underscores are supposed to imply some sort of pseudo-privatization, but would...
4
by: Edvard Majakari | last post by:
Hi, I just found py.test and converted a large unit test module to py.test format (which is actually almost-no-format-at-all, but I won't get there now). Having 348 test cases in the module and...
0
by: Shekhar | last post by:
My ASP always calls a DLL that was constructed using VFP Code. If needed, this DLL can call a VFP External Class Method that is outside the DLL for some process. The DLL is bound with COM always. ...
6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
7
by: noro | last post by:
Is it possible to do the following: for a certain class: ---------------------------- class C: def func1(self): pass def func2(self):
19
by: zzw8206262001 | last post by:
Hi,I find a way to make javescript more like c++ or pyhon There is the sample code: function Father(self) //every contructor may have "self" argument { self=self?self:this; ...
5
sumittyagi
by: sumittyagi | last post by:
Hi all, I was reading factory method pattern from wikipedia link. There are some limitations mentioned three limitations mentioned there. These are all fine. But there is a conclusion...
3
by: =?Utf-8?B?TmF2YW5lZXRoLksuTg==?= | last post by:
Hi All, I have to design a "Product" class. This class will contain a product details. I have a "ProductFactory" which contain some static methods which returns "Product" instance. I have a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.