New Methods for existing types?
-
I was wondering something... Is it possible do do something like:
string s1 = "12345";
string s2 = s1.RemoveNum("5");
MessageBox.Show(s2);Where further down you add the RemoveNum() method ONTO the string type? something like...
public static string add RemoveNum(string num)
{
return string.Replace(num, "");
}? Just wondering... :)
-= Reelix =-
-
I was wondering something... Is it possible do do something like:
string s1 = "12345";
string s2 = s1.RemoveNum("5");
MessageBox.Show(s2);Where further down you add the RemoveNum() method ONTO the string type? something like...
public static string add RemoveNum(string num)
{
return string.Replace(num, "");
}? Just wondering... :)
-= Reelix =-
-
public static string add RemoveNum(string num) { return string.Replace(num, ""); } obviously doesnt work (Seeing as I made it up on the spot :p) Was googling around, and couldn't find anything particularly useful... You can't override, since it doesnt exist... I'm just wondering if there's a way TO do it :)
-= Reelix =-
-
I was wondering something... Is it possible do do something like:
string s1 = "12345";
string s2 = s1.RemoveNum("5");
MessageBox.Show(s2);Where further down you add the RemoveNum() method ONTO the string type? something like...
public static string add RemoveNum(string num)
{
return string.Replace(num, "");
}? Just wondering... :)
-= Reelix =-
OOP way to augment functionality is inheritance, but no luck this time:
String
class is sealed. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
OOP way to augment functionality is inheritance, but no luck this time:
String
class is sealed. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
Of course there are ways, evil, but you have to ask the devil. :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
Of course there are ways, evil, but you have to ask the devil. :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
I was wondering something... Is it possible do do something like:
string s1 = "12345";
string s2 = s1.RemoveNum("5");
MessageBox.Show(s2);Where further down you add the RemoveNum() method ONTO the string type? something like...
public static string add RemoveNum(string num)
{
return string.Replace(num, "");
}? Just wondering... :)
-= Reelix =-
check out extension methods
betonglasermur.FeedDwarf(pur_is, 17);
ProcessStartupInfo.AintNotCreateNoWindow = (false && !true) != (true || false) ? false == true ? true : false : (true != false && false);Morgonen är tröttmans mecka
-
check out extension methods
betonglasermur.FeedDwarf(pur_is, 17);
ProcessStartupInfo.AintNotCreateNoWindow = (false && !true) != (true || false) ? false == true ? true : false : (true != false && false);Morgonen är tröttmans mecka
http://www.codeproject.com/KB/cs/Using_Extension_Methods.aspx[^]
betonglasermur.FeedDwarf(pur_is, 17);
ProcessStartupInfo.AintNotCreateNoWindow = (false && !true) != (true || false) ? false == true ? true : false : (true != false && false);Morgonen är tröttmans mecka
-
I was wondering something... Is it possible do do something like:
string s1 = "12345";
string s2 = s1.RemoveNum("5");
MessageBox.Show(s2);Where further down you add the RemoveNum() method ONTO the string type? something like...
public static string add RemoveNum(string num)
{
return string.Replace(num, "");
}? Just wondering... :)
-= Reelix =-
there is a way, using extension methods. but this requires c# 3.0 .. Extension methods are static methods that can be invoked using instance method syntax. In effect, extension methods make it possible to extend existing types and constructed types with additional methods.
-
check out extension methods
betonglasermur.FeedDwarf(pur_is, 17);
ProcessStartupInfo.AintNotCreateNoWindow = (false && !true) != (true || false) ? false == true ? true : false : (true != false && false);Morgonen är tröttmans mecka
VICTORY!!!
using System;
class String { public string RemoveFirst(string num) { //num = num.Remove(num); string toReturn = num.Remove(0, 1); return toReturn; //Console.WriteLine("Hackz0red!"); } } class ExtMethodDemo { static void Main(string\[\] args) { String r1 = new String(); string r2 = r1.RemoveFirst("12345"); Console.WriteLine(r2); Console.ReadLine(); } }
Thanks laserbaronen!!! :D
-= Reelix =-
-
VICTORY!!!
using System;
class String { public string RemoveFirst(string num) { //num = num.Remove(num); string toReturn = num.Remove(0, 1); return toReturn; //Console.WriteLine("Hackz0red!"); } } class ExtMethodDemo { static void Main(string\[\] args) { String r1 = new String(); string r2 = r1.RemoveFirst("12345"); Console.WriteLine(r2); Console.ReadLine(); } }
Thanks laserbaronen!!! :D
-= Reelix =-
hmm o_o
public static class ExtMethods
{
public static string RemoveFirst(this string str)
{
return str.Remove(0,1);
}
}
class ExtMethodDemo
{
static void Main(string[] args)
{
string r1 = "12345".RemoveFirst();
Console.WriteLine(r1);
Console.ReadLine();
}
}was thinking something like this
betonglasermur.FeedDwarf(pur_is, 17);
ProcessStartupInfo.AintNotCreateNoWindow = (false && !true) != (true || false) ? false == true ? true : false : (true != false && false);Morgonen är tröttmans mecka
modified on Tuesday, May 20, 2008 10:05 AM
-
there is a way, using extension methods. but this requires c# 3.0 .. Extension methods are static methods that can be invoked using instance method syntax. In effect, extension methods make it possible to extend existing types and constructed types with additional methods.
buchstaben wrote:
In effect, extension methods make it possible
make it appear possible, when in fact it isn't. :-D
-
buchstaben wrote:
In effect, extension methods make it possible
make it appear possible, when in fact it isn't. :-D
PIEBALDconsult wrote:
buchstaben wrote: In effect, extension methods make it possible make it appear possible, when in fact it isn't.
that's just what c# 3.0 specification says.