Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

I understand most of the overloading concept, and was wondering how far I could take it.

 

Could I overload the String.Replace method?

 

Example:

 



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim s As String = "this that this that this that"
Dim ht As New Hashtable
ht.Add("this", "was")
ht.Add("that", "there")

'Pass a hashtable into my own overloaded replace.
s = s.Replace(ht)

End Sub


'How would I declare this?
Public Overloads Function Replace(ByVal ht As Hashtable) As String
Dim strReturn As String
'strReturn = ?
'Loop through replacing keys w/values
Dim o As DictionaryEntry
For Each o In ht
strReturn = strReturn.Replace(o.Key, o.Value)
Next
Return strReturn
End Function

[/Code]

 

I wouldn't really have a use for this, its just an example I came up with to show the type of overloading I am talking about.

 

Is this possible?

...

Edited by JumpsInLava
Posted
Just check the documentation for the member you want to overload and you'll see if you can overload it or not, also it'll tell you if you can override it
Fat kids are harder to kidnap
Posted



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim s As String = "this that this that this that"
Dim ht As New Hashtable
ht.Add("this", "was")
ht.Add("that", "there")

'Pass a hashtable into my own overloaded replace.
s = s.Replace(ht)

End Sub
[/Code]

 

Keep in mind, in this case you will be calling the base string Replace() method.

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted
Keep in mind' date=' in this case you will be calling the base string Replace() method.[/quote']

 

Yes, I am trying to overload the string.replace method. I think the code I posted earlier was out in left field.

 

After doing some more reading about the wonderful world of inheritance and polymorphism.... It looks like to do what I want, I have to create a new class that inherits the string class, then overload the Replace function.

 

But when I create a new class, and try "Inherit String" it comes up as "NotInheritable".

 

:(

  • *Experts*
Posted

You can't inherit from the String class; as you noted, it is declared NonInheritable

(sealed in C#). To overload or override a method, you must first inherit the class that

contains it. Because you can't do this, you cannot overload the method.

 

If you are really serious about this, you could create your own class that

exposes all the properties and methods of a string variable, but that means

writing wrappers for every member of the String class. If you are looking to

greatly extend the functionality of the String class, then by all means go for it.

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Posted
You can't inherit from the String class; as you noted, it is declared NonInheritable

(sealed in C#). To overload or override a method, you must first inherit the class that

contains it. Because you can't do this, you cannot overload the method.

 

If you are really serious about this, you could create your own class that

exposes all the properties and methods of a string variable, but that means

writing wrappers for every member of the String class. If you are looking to

greatly extend the functionality of the String class, then by all means go for it.

 

I guess that wouldn't be worth it for just a few additions. Oh well, back to regular old functions and subs in a library module.

 

Thanks all.

  • *Experts*
Posted

Also, you would have to declare all of your variables as type "MyString" (or whatever name you picked). That means any functions that accept a String would have to accept MyString. Anytime you call a built-in function that accepts a string, you would have to use something like:

MyString s = "hello";

// Function Test takes a String as an argument

Test(s.ToString());

Luckily, not calling ToString will result in a compile error so you can catch these quickly.

 

Not so luckily, if you want to assign the MyString to a DataSet, you'll have to remember to use ToString. If you don't, you won't get a compile error but you'll get a runtime error:

// assume ds is a DataSet

MyString s = "hello";

ds.Tables[0].Rows[0][0] = s;

 

This will compile just fine as the value being assigned takes an object, which s is. Since MyString isn't a normal DataSet datatype, this will throw an exception at runtime.

 

I tried this with an integer (I made NullInt). You have to overload a ton of things, and you're still left with a number of issues that you must be very diligent about fixing. It might be worth it, if you're careful.

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

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