Solved

Add a method to an object

Posted on 2007-11-26
10
247 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

919 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

16 Experts available now in Live!

Get 1:1 Help Now