Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

  • Leaders
Posted

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

[sIGPIC]e[/sIGPIC]
Posted

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.
  • Leaders
Posted

// 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.

[sIGPIC]e[/sIGPIC]
Posted
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.
Posted

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.

"Who is John Galt?"

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...