Please check code - need to make generic

Discussion in 'ASP .Net' started by =?Utf-8?B?U2FuZHk=?=, Apr 23, 2005.

  1. 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
     
    =?Utf-8?B?U2FuZHk=?=, Apr 23, 2005
    #1
    1. Advertising

  2. 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
     
    =?Utf-8?B?RWx0b24gVw==?=, Apr 23, 2005
    #2
    1. Advertising

  3. =?Utf-8?B?U2FuZHk=?=

    DoesDotNet Guest

    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
     
    DoesDotNet, Apr 23, 2005
    #3
  4. 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
     
    =?Utf-8?B?U2FuZHk=?=, Apr 24, 2005
    #4
  5. 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

    >
    >
     
    =?Utf-8?B?U2FuZHk=?=, Apr 24, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Murat Tasan
    Replies:
    1
    Views:
    8,057
    Chaitanya
    Feb 3, 2009
  2. KK
    Replies:
    2
    Views:
    625
    Big Brian
    Oct 14, 2003
  3. Replies:
    2
    Views:
    441
  4. minlearn
    Replies:
    2
    Views:
    461
    red floyd
    Mar 13, 2009
  5. tabonni
    Replies:
    15
    Views:
    216
    Harag
    Aug 8, 2004
Loading...

Share This Page