Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Please check code - need to make generic

Reply
Thread Tools

Please check code - need to make generic

 
 
=?Utf-8?B?U2FuZHk=?=
Guest
Posts: n/a
 
      04-23-2005
Hello -

This code was snagged by me from the Internet and altered. Its purpose is
to check for swear words. It works the way it currently is, but I need it to
be more generic -- i.e., I don't want it to refer to TextBox1 or Label1
directly; I want to be able to plug in the name of any textbox into
CheckString(TextBox1.Text), instead of just a specific textbox. [I have many
textboxes on one page that all need to be checked.]

Public Sub CheckString(inputstring as string)
Dim alWordList as New ArrayList

Dim origtext as String
origtext = TextBox1.Text

dim xmlDocPath as string = server.mappath("bad_words2.xml")
dim xmlReader as XmlTextreader = New xmlTextReader(xmlDocPath)
While (xmlReader.Read())
if xmlReader.Nodetype=xmlNodeType.Text then
alWordList.Add(xmlReader.Value)
End If
End While
xmlReader.Close()

Dim r as Regex
dim element as string
dim output as string
For Each element in alWordList
r = New Regex("\b" & element)
InputString = r.Replace(InputString, "****")
Next

TextBox1.Text = InputString
If origtext <> TextBox1.Text Then
Label1.Text = "Funky words, please replace."
Else
Label1.Text = "Okay." 'This is just included for testing
End If
End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
CheckString(TextBox1.Text)
End Sub

Any suggestions will be greatly appreciated!
--
Sandy
 
Reply With Quote
 
 
 
 
=?Utf-8?B?RWx0b24gVw==?=
Guest
Posts: n/a
 
      04-23-2005
Hi Sandy,

You can try

Public Sub CheckString(txtBox As TextBox, lbl As Label)
Dim alWordList as New ArrayList
Dim inputstring as string = txtBox.Text
Dim origtext as String = inputstring

dim xmlDocPath as string = server.mappath("bad_words2.xml")
dim xmlReader as XmlTextreader = New xmlTextReader(xmlDocPath)
While (xmlReader.Read())
if xmlReader.Nodetype=xmlNodeType.Text then
alWordList.Add(xmlReader.Value)
End If
End While
xmlReader.Close()

Dim r as Regex
dim element as string
dim output as string
For Each element in alWordList
r = New Regex("\b" & element)
inputString = r.Replace(inputString, "****")
Next

txtBox.Text = inputString
If Not origtext.Equals(inputString) Then
lbl.Text = "Funky words, please replace."
Else
lbl.Text = "Okay." 'This is just included for testing
End If
End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
CheckString(TextBox1, Label1)
End Sub



"Sandy" wrote:

> Hello -
>
> This code was snagged by me from the Internet and altered. Its purpose is
> to check for swear words. It works the way it currently is, but I need it to
> be more generic -- i.e., I don't want it to refer to TextBox1 or Label1
> directly; I want to be able to plug in the name of any textbox into
> CheckString(TextBox1.Text), instead of just a specific textbox. [I have many
> textboxes on one page that all need to be checked.]
>
> Public Sub CheckString(inputstring as string)
> Dim alWordList as New ArrayList
>
> Dim origtext as String
> origtext = TextBox1.Text
>
> dim xmlDocPath as string = server.mappath("bad_words2.xml")
> dim xmlReader as XmlTextreader = New xmlTextReader(xmlDocPath)
> While (xmlReader.Read())
> if xmlReader.Nodetype=xmlNodeType.Text then
> alWordList.Add(xmlReader.Value)
> End If
> End While
> xmlReader.Close()
>
> Dim r as Regex
> dim element as string
> dim output as string
> For Each element in alWordList
> r = New Regex("\b" & element)
> InputString = r.Replace(InputString, "****")
> Next
>
> TextBox1.Text = InputString
> If origtext <> TextBox1.Text Then
> Label1.Text = "Funky words, please replace."
> Else
> Label1.Text = "Okay." 'This is just included for testing
> End If
> End Sub
>
> Private Sub Button1_Click(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles Button1.Click
> CheckString(TextBox1.Text)
> End Sub
>
> Any suggestions will be greatly appreciated!
> --
> Sandy

 
Reply With Quote
 
 
 
 
DoesDotNet
Guest
Posts: n/a
 
      04-23-2005
You could achieve this by e.g. creating a new class with a static
method called

public static bool CheckString( string input, string censoredString )

that returns a bool indicating whether it had to censor the string.
The censored string is written into the second string reference you
pass.
Since I am not familiar with VB, here is the C# for that.

public class SwearWordChecker{
public static string CheckString( string input, string censoredString
){
censoredString = input;
// do your thing here
...
censoredString = r.Replace(InputString, "****");
...
//if they are the same, return true
return (input == censoredString);
}
}

and then use that class in your Button code:

Private Sub Button1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
string censored = null;
if( !CheckString(TextBox1.Text, censored) ){
TextBox1.Text = censored;
lblError.Text = "You cursing sombitch."
}
End Sub

Hope this helps.
Manuel



Sandy wrote:
> Hello -
>
> This code was snagged by me from the Internet and altered. Its

purpose is
> to check for swear words. It works the way it currently is, but I

need it to
> be more generic -- i.e., I don't want it to refer to TextBox1 or

Label1
> directly; I want to be able to plug in the name of any textbox into
> CheckString(TextBox1.Text), instead of just a specific textbox. [I

have many
> textboxes on one page that all need to be checked.]
>
> Public Sub CheckString(inputstring as string)
> Dim alWordList as New ArrayList
>
> Dim origtext as String
> origtext = TextBox1.Text
>
> dim xmlDocPath as string = server.mappath("bad_words2.xml")
> dim xmlReader as XmlTextreader = New xmlTextReader(xmlDocPath)
> While (xmlReader.Read())
> if xmlReader.Nodetype=xmlNodeType.Text then
> alWordList.Add(xmlReader.Value)
> End If
> End While
> xmlReader.Close()
>
> Dim r as Regex
> dim element as string
> dim output as string
> For Each element in alWordList
> r = New Regex("\b" & element)
> InputString = r.Replace(InputString, "****")
> Next
>
> TextBox1.Text = InputString
> If origtext <> TextBox1.Text Then
> Label1.Text = "Funky words, please replace."
> Else
> Label1.Text = "Okay." 'This is just included for testing
> End If
> End Sub
>
> Private Sub Button1_Click(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles Button1.Click
> CheckString(TextBox1.Text)
> End Sub
>
> Any suggestions will be greatly appreciated!
> --
> Sandy


 
Reply With Quote
 
=?Utf-8?B?U2FuZHk=?=
Guest
Posts: n/a
 
      04-24-2005
Thanks so much, Elton. It works beautifully!

Sandy

"Elton W" wrote:

> Hi Sandy,
>
> You can try
>
> Public Sub CheckString(txtBox As TextBox, lbl As Label)
> Dim alWordList as New ArrayList
> Dim inputstring as string = txtBox.Text
> Dim origtext as String = inputstring
>
> dim xmlDocPath as string = server.mappath("bad_words2.xml")
> dim xmlReader as XmlTextreader = New xmlTextReader(xmlDocPath)
> While (xmlReader.Read())
> if xmlReader.Nodetype=xmlNodeType.Text then
> alWordList.Add(xmlReader.Value)
> End If
> End While
> xmlReader.Close()
>
> Dim r as Regex
> dim element as string
> dim output as string
> For Each element in alWordList
> r = New Regex("\b" & element)
> inputString = r.Replace(inputString, "****")
> Next
>
> txtBox.Text = inputString
> If Not origtext.Equals(inputString) Then
> lbl.Text = "Funky words, please replace."
> Else
> lbl.Text = "Okay." 'This is just included for testing
> End If
> End Sub
>
> Private Sub Button1_Click(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles Button1.Click
> CheckString(TextBox1, Label1)
> End Sub
>
>
>
> "Sandy" wrote:
>
> > Hello -
> >
> > This code was snagged by me from the Internet and altered. Its purpose is
> > to check for swear words. It works the way it currently is, but I need it to
> > be more generic -- i.e., I don't want it to refer to TextBox1 or Label1
> > directly; I want to be able to plug in the name of any textbox into
> > CheckString(TextBox1.Text), instead of just a specific textbox. [I have many
> > textboxes on one page that all need to be checked.]
> >
> > Public Sub CheckString(inputstring as string)
> > Dim alWordList as New ArrayList
> >
> > Dim origtext as String
> > origtext = TextBox1.Text
> >
> > dim xmlDocPath as string = server.mappath("bad_words2.xml")
> > dim xmlReader as XmlTextreader = New xmlTextReader(xmlDocPath)
> > While (xmlReader.Read())
> > if xmlReader.Nodetype=xmlNodeType.Text then
> > alWordList.Add(xmlReader.Value)
> > End If
> > End While
> > xmlReader.Close()
> >
> > Dim r as Regex
> > dim element as string
> > dim output as string
> > For Each element in alWordList
> > r = New Regex("\b" & element)
> > InputString = r.Replace(InputString, "****")
> > Next
> >
> > TextBox1.Text = InputString
> > If origtext <> TextBox1.Text Then
> > Label1.Text = "Funky words, please replace."
> > Else
> > Label1.Text = "Okay." 'This is just included for testing
> > End If
> > End Sub
> >
> > Private Sub Button1_Click(ByVal sender As Object, ByVal e As
> > System.EventArgs) Handles Button1.Click
> > CheckString(TextBox1.Text)
> > End Sub
> >
> > Any suggestions will be greatly appreciated!
> > --
> > Sandy

 
Reply With Quote
 
=?Utf-8?B?U2FuZHk=?=
Guest
Posts: n/a
 
      04-24-2005
Thanks for your response!

Sandy

"DoesDotNet" wrote:

> You could achieve this by e.g. creating a new class with a static
> method called
>
> public static bool CheckString( string input, string censoredString )
>
> that returns a bool indicating whether it had to censor the string.
> The censored string is written into the second string reference you
> pass.
> Since I am not familiar with VB, here is the C# for that.
>
> public class SwearWordChecker{
> public static string CheckString( string input, string censoredString
> ){
> censoredString = input;
> // do your thing here
> ...
> censoredString = r.Replace(InputString, "****");
> ...
> //if they are the same, return true
> return (input == censoredString);
> }
> }
>
> and then use that class in your Button code:
>
> Private Sub Button1_Click(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles Button1.Click
> string censored = null;
> if( !CheckString(TextBox1.Text, censored) ){
> TextBox1.Text = censored;
> lblError.Text = "You cursing sombitch."
> }
> End Sub
>
> Hope this helps.
> Manuel
>
>
>
> Sandy wrote:
> > Hello -
> >
> > This code was snagged by me from the Internet and altered. Its

> purpose is
> > to check for swear words. It works the way it currently is, but I

> need it to
> > be more generic -- i.e., I don't want it to refer to TextBox1 or

> Label1
> > directly; I want to be able to plug in the name of any textbox into
> > CheckString(TextBox1.Text), instead of just a specific textbox. [I

> have many
> > textboxes on one page that all need to be checked.]
> >
> > Public Sub CheckString(inputstring as string)
> > Dim alWordList as New ArrayList
> >
> > Dim origtext as String
> > origtext = TextBox1.Text
> >
> > dim xmlDocPath as string = server.mappath("bad_words2.xml")
> > dim xmlReader as XmlTextreader = New xmlTextReader(xmlDocPath)
> > While (xmlReader.Read())
> > if xmlReader.Nodetype=xmlNodeType.Text then
> > alWordList.Add(xmlReader.Value)
> > End If
> > End While
> > xmlReader.Close()
> >
> > Dim r as Regex
> > dim element as string
> > dim output as string
> > For Each element in alWordList
> > r = New Regex("\b" & element)
> > InputString = r.Replace(InputString, "****")
> > Next
> >
> > TextBox1.Text = InputString
> > If origtext <> TextBox1.Text Then
> > Label1.Text = "Funky words, please replace."
> > Else
> > Label1.Text = "Okay." 'This is just included for testing
> > End If
> > End Sub
> >
> > Private Sub Button1_Click(ByVal sender As Object, ByVal e As
> > System.EventArgs) Handles Button1.Click
> > CheckString(TextBox1.Text)
> > End Sub
> >
> > Any suggestions will be greatly appreciated!
> > --
> > Sandy

>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make this code generic? Thoma VHDL 6 11-21-2009 06:43 PM
not just generic type programming,but also parallism generic syntaxprogramming?? minlearn C++ 2 03-13-2009 05:17 PM
generic interfaces with generic methods Murat Tasan Java 1 02-03-2009 12:17 PM
Generic class in a non generic class nramnath@gmail.com Java 2 07-04-2006 07:24 AM
Please tell me the generic way to check different radio button groups? tabonni Javascript 15 08-08-2004 09:53 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57