tipudelacablu Posted November 20, 2005 Posted November 20, 2005 Is there a way to add a new function (called URL for instance) to string class? I just need an easy way to process a string. Thanks. Quote
IngisKahn Posted November 21, 2005 Posted November 21, 2005 Sure, are you using C# 3.0? ;) Quote "Who is John Galt?"
bri189a Posted November 21, 2005 Posted November 21, 2005 You can't add directly to the .NET string class and it's a sealed class so you can't extend it by creating a derived class. Generally there is no need to extend native types such as this. For instance, why would a windows application care about a URL? Or console app? Or a class library? Just create a helper class with a static method that does this URL thing you want to do. Quote
Leaders snarfblam Posted November 21, 2005 Leaders Posted November 21, 2005 If you have a member of a class that is a string that is always used for a url, rather than extending System.String, you could always create a url class that contains a string. Just to demonstrate: Public Class URL Dim _URL As String Public Sub New(Address As String) Me.Address = Address End Sub Public Property Address As String Get Return _URL End Get Set(Value As Integer) 'Validate _URL = Value End Set End Property Public Sub Launch() System.Diagnostics.Process.Start(_URL) End Sub Public Function Domain() As String Dim Start As Integer = _URL.IndexOf("//") + 2 Dim Len As Integer = _URL.SubString(Start).IndexOf("/") Return _URL.SubString(Start, Len) End Function End Class Quote [sIGPIC]e[/sIGPIC]
Administrators PlausiblyDamp Posted November 22, 2005 Administrators Posted November 22, 2005 Could you not just use the System.Uri class for your URL processing? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Leaders snarfblam Posted November 22, 2005 Leaders Posted November 22, 2005 I don't know about the original "for instance" but my example was hypothetical. Quote [sIGPIC]e[/sIGPIC]
mskeel Posted November 22, 2005 Posted November 22, 2005 I just found this: C# 3.0 has something that reminds me of mixins: extender methods. You create a class, and add a static method that is attached to an existing class. Dumb example: I want to add a new method HasEvenLength to the string class: public class MyExtenders { public static bool HasEvenLength(this string s) { return s.Length % 2 == 0; } } Which can then be used like this: string myString = "some string"; bool even = myString.HasEvenLength();And it looked applicable. I got it from this Forum. Quote
mskeel Posted November 22, 2005 Posted November 22, 2005 And glancing back up at the other posts in this thread I know understand what IngisKahn meant. Quote
Leaders snarfblam Posted November 22, 2005 Leaders Posted November 22, 2005 // class in which i use string processing code public class myClass { // ... public static bool StringHasEvenLength(string s) { return s.Length % 2 == 0; } public static void example() { string myString = "some string"; bool even = StringHasEvenLength(myString); } } This has always worked for me. I hate extenders already. Quote [sIGPIC]e[/sIGPIC]
bri189a Posted November 23, 2005 Posted November 23, 2005 From the OOP world if you can't derive from it (thus extending it) there's probably a reason for it. I think this 'extension' thing that exists in 3.0 is a potential disaster waiting to happen and doesn't do much good for problems that need to be fixed today. Quote
IngisKahn Posted November 23, 2005 Posted November 23, 2005 In most cases classes are sealed because the designer didn't think it was worth considering which functions to make virtual. Extension methods should be used vary sparingly, in fact the C# 3.0 documentation stresses this. With that said, I find it very useful for utility functions, especially those dealing with structures since they can be derived from anyway. I�ve already made good use of them with DirectX�s Vector3 and Matrix structures. Quote "Who is John Galt?"
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.