Solved

Add a method to an object

Posted on 2007-11-26
10
273 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 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
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 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to set focus on a dynamic control 18 50
SQL Server Serialization error 8 44
history of pages 7 51
Whice version VisualStudio for new Dev Laptop? 10 41
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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