Solved

Add a method to an object

Posted on 2007-11-26
10
237 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
  • 5
  • 3
  • 2
10 Comments
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 400 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 100 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now