Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Add a method to an object

Posted on 2007-11-26
10
Medium Priority
?
277 Views
Last Modified: 2010-04-23
Hi,

I want to add a custom method to an object, I have a function called Sanitize, this takes a string and sanitises it for SQL, currently I would call it like so:

dim str as string
str = "a 'little' form"
str = Sanitize(str)

What I would lke ot be able to do is add the function Sanitize to the string object, so to clean a string I would just type:

str.Sanitize()


How do I do this, and can yo upllease provide the correct terms for this process too?


For info, this is my Sanitize function

    Public Shared Function Sanitize(ByVal str as String) As String
        str = Replace(str, "'", "''")
        str = Replace(str, Chr(34), Chr(34) & Chr(34))
        str = HttpUtility.HtmlEncode(str)
        str = str.Replace(Environment.NewLine, "<br />")
        Sanitize = str
    End Function
0
Comment
Question by:bhermer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 48

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 1600 total points
ID: 20348781
Hi bhermer,

In order to do that, you would probably want to inherit from String, which, unfortunately, cannot be done (it is sealed).

My only suggestion is to send the string to your function by reference, like this....

    Public Shared Function Sanitize(ByRef str as String) As String
        str = Replace(str, "'", "''")
        str = Replace(str, Chr(34), Chr(34) & Chr(34))
        str = HttpUtility.HtmlEncode(str)
        str = str.Replace(Environment.NewLine, "<br />")
        Sanitize = str
    End Function

....which would enable you to sanitize the string like so....

    Sanitize(str)

...instead of like before....

    str = Sanitize(str)

Regards,

Wayne
0
 
LVL 4

Author Comment

by:bhermer
ID: 20348814
Hi Wayne,

Thanks for that, the ByRef makes sense. Is this true for all in bult objects? i.e you can''t inherit?
0
 
LVL 5

Assisted Solution

by:gocemi
gocemi earned 400 total points
ID: 20348825
Create class MyString that will inherit from String

public class MyString:String
{

 Public Shared Function Sanitize(ByVal str as String) As String
        str = Replace(str, "'", "''")
        str = Replace(str, Chr(34), Chr(34) & Chr(34))
        str = HttpUtility.HtmlEncode(str)
        str = str.Replace(Environment.NewLine, "<br />")
        Sanitize = str
    End Function

}


so INSTEAD OF creating stirng s = "sss";
you create strings ....
myString m = new MyString();

Since you inherit from string every string can be casted in to MyString Class

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 20348827
Not all - String is the only one I know of for certain.
0
 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 20348833
gocemi - you can not inherit from String.
0
 
LVL 4

Author Comment

by:bhermer
ID: 20348840
gocemi, thanks for that, it would work without a doubt, but would not save me any real time as I would have to declare my strings differently, Probably easier to use ByRef instead, then I can use it with any string without having to plan ahead.

I am awarding the bulk of the poitns to Wayne, as he has provided the solution I am using, but I am also awarding some to gocemi as 'technically' this did answer my question.

Thanks people.
0
 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 20348860
No disrespect, but gocemi's suggestion is plainly wrong. It is not possible to inherit from String.....

http://www.experts-exchange.com/Programming/Languages/.NET/Q_22848364.html
0
 
LVL 5

Expert Comment

by:gocemi
ID: 20348905
my Appologies for this,

I've never neded to do this,

Sorry man for my mistake
0
 
LVL 4

Author Comment

by:bhermer
ID: 20348929
ok, sorry too, I didnt realize that that was inheriting in the direct sense, dont understand this subject particularly well.
0
 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 20349207
No problems. I just wanted to point it out to avoid any confusion.
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question