?
Solved

Add a method to an object

Posted on 2007-11-26
10
Medium Priority
?
275 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 47

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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 47

Expert Comment

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

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 47

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 47

Expert Comment

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

777 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