Home All Groups Group Topic Archive Search About

Loop not working in VB.NET

Author
14 Nov 2006 9:31 PM
jimmy
I am working on a project which tracks 'bad' words in IE and im using a
For loop to check for an array of words in he address bar. I have
included the broken code below. Any pointers on why it isnt working
would be very useful.

Private Sub BeginNavigate(ByVal pDisp As Object, ByRef URL As Object,
ByRef Flags As Object, ByRef TargetFrameName As Object, ByRef PostData
As Object, ByRef Headers As Object, ByRef Cancel As Boolean)
        Dim i As Integer
        For i = 0 To BadWords.Length - 1
            If InStr(URL.ToString(), BadWords(i)) Then
                IE.Quit()
            End If
        Next
    End Sub

Thanks in advance

Author
14 Nov 2006 9:33 PM
jimmy
Just thought id add. only the first word in the array is detected.. all
the others are ignored ifthat helps you solve the problem
Author
14 Nov 2006 10:36 PM
Tim Patrick
InStr is (if I remember correctly) case sensitive, so make sure everything
matches in upper/lower case. Without seeing the code used to define and populate
BadWords(), it's hard to make guesses about what it contains.

-----
Tim Patrick
Start-to-Finish Visual Basic 2005

Show quoteHide quote
> I am working on a project which tracks 'bad' words in IE and im using
> a For loop to check for an array of words in he address bar. I have
> included the broken code below. Any pointers on why it isnt working
> would be very useful.
>
> Private Sub BeginNavigate(ByVal pDisp As Object, ByRef URL As Object,
> ByRef Flags As Object, ByRef TargetFrameName As Object, ByRef PostData
> As Object, ByRef Headers As Object, ByRef Cancel As Boolean)
> Dim i As Integer
> For i = 0 To BadWords.Length - 1
> If InStr(URL.ToString(), BadWords(i)) Then
> IE.Quit()
> End If
> Next
> End Sub
> Thanks in advance
>
Author
14 Nov 2006 11:39 PM
rowe_newsgroups
> InStr is (if I remember correctly) case sensitive, so make sure everything
> matches in upper/lower case.

Easiest way would be to convert all the "badwords" and the URL to
uppercase characters. I believe the command is .ToUpper (I don't have
vb on this machine)

Thanks,

Seth Rowe


Tim Patrick wrote:
Show quoteHide quote
> InStr is (if I remember correctly) case sensitive, so make sure everything
> matches in upper/lower case. Without seeing the code used to define and populate
> BadWords(), it's hard to make guesses about what it contains.
>
> -----
> Tim Patrick
> Start-to-Finish Visual Basic 2005
>
> > I am working on a project which tracks 'bad' words in IE and im using
> > a For loop to check for an array of words in he address bar. I have
> > included the broken code below. Any pointers on why it isnt working
> > would be very useful.
> >
> > Private Sub BeginNavigate(ByVal pDisp As Object, ByRef URL As Object,
> > ByRef Flags As Object, ByRef TargetFrameName As Object, ByRef PostData
> > As Object, ByRef Headers As Object, ByRef Cancel As Boolean)
> > Dim i As Integer
> > For i = 0 To BadWords.Length - 1
> > If InStr(URL.ToString(), BadWords(i)) Then
> > IE.Quit()
> > End If
> > Next
> > End Sub
> > Thanks in advance
> >
Author
15 Nov 2006 12:38 AM
Dennis
You might try:

Dim str as string = URL.ToString()
For Each s as string in BadWords
    if InStr(str,s)>=0 then IE.Quit
next

--
Dennis in Houston


Show quoteHide quote
"jimmy" wrote:

> I am working on a project which tracks 'bad' words in IE and im using a
> For loop to check for an array of words in he address bar. I have
> included the broken code below. Any pointers on why it isnt working
> would be very useful.
>
> Private Sub BeginNavigate(ByVal pDisp As Object, ByRef URL As Object,
> ByRef Flags As Object, ByRef TargetFrameName As Object, ByRef PostData
> As Object, ByRef Headers As Object, ByRef Cancel As Boolean)
>         Dim i As Integer
>         For i = 0 To BadWords.Length - 1
>             If InStr(URL.ToString(), BadWords(i)) Then
>                 IE.Quit()
>             End If
>         Next
>     End Sub
>
> Thanks in advance
>
>
Author
15 Nov 2006 4:09 AM
Tom Leylan
I'm confused :-)  Is the intent to check a list of "words" against a
complete URL e.g. www.essexhotel.com so the sequence "sex" (given it's
presence in the list) would mean that one couldn't view this site?  I'm not
sure that will work so well as a strategy but given that is anybody checking
the docs on the InStr() method?

To the original poster... InStr() doesn't return a boolean right? So there
isn't much surprise there but let me suggest that you test your hypotheses
(in the future) rather than just write code.  If is isn't working you might
try typing the following into the immediate window.  I get an 11 as a return
value.... the boolean test is therefore out.

? microsoft.VisualBasic.InStr( "this is a test", "test")

And you will see that there is a CompareMethod parameter which if you don't
supply it defaults to the Option Compare setting.  Would that setting be the
one you want?

And Dennis... meant > 0 rather than >= 0 since 0 is returned in a number of
cases to indicate the string was not found.

Hope this helps...




Show quoteHide quote
"Dennis" <Den***@discussions.microsoft.com> wrote in message
news:A66C81C0-8015-4EB4-ABE7-DC5AC807DAAE@microsoft.com...
> You might try:
>
> Dim str as string = URL.ToString()
> For Each s as string in BadWords
>    if InStr(str,s)>=0 then IE.Quit
> next
>
> --
> Dennis in Houston
>
>
> "jimmy" wrote:
>
>> I am working on a project which tracks 'bad' words in IE and im using a
>> For loop to check for an array of words in he address bar. I have
>> included the broken code below. Any pointers on why it isnt working
>> would be very useful.
>>
>> Private Sub BeginNavigate(ByVal pDisp As Object, ByRef URL As Object,
>> ByRef Flags As Object, ByRef TargetFrameName As Object, ByRef PostData
>> As Object, ByRef Headers As Object, ByRef Cancel As Boolean)
>>         Dim i As Integer
>>         For i = 0 To BadWords.Length - 1
>>             If InStr(URL.ToString(), BadWords(i)) Then
>>                 IE.Quit()
>>             End If
>>         Next
>>     End Sub
>>
>> Thanks in advance
>>
>>
Author
15 Nov 2006 2:33 PM
Chris Dunaway
Tom Leylan wrote:

> To the original poster... InStr() doesn't return a boolean right? So there
> isn't much surprise there but let me suggest that you test your hypotheses
> (in the future) rather than just write code.  If is isn't working you might
> try typing the following into the immediate window.  I get an 11 as a return
> value.... the boolean test is therefore out.

You are correct, so the OP must be running without Option Strict On,
otherwise the compiler would have complained!
Author
16 Nov 2006 12:35 AM
Dennis
You are correct..I was thinking of IndexOf method.  Also, the
String.Compare(a,b,True) = 0 would be a better solution to avoid case
problems.
--
Dennis in Houston


Show quoteHide quote
"Tom Leylan" wrote:

> I'm confused :-)  Is the intent to check a list of "words" against a
> complete URL e.g. www.essexhotel.com so the sequence "sex" (given it's
> presence in the list) would mean that one couldn't view this site?  I'm not
> sure that will work so well as a strategy but given that is anybody checking
> the docs on the InStr() method?
>
> To the original poster... InStr() doesn't return a boolean right? So there
> isn't much surprise there but let me suggest that you test your hypotheses
> (in the future) rather than just write code.  If is isn't working you might
> try typing the following into the immediate window.  I get an 11 as a return
> value.... the boolean test is therefore out.
>
> ? microsoft.VisualBasic.InStr( "this is a test", "test")
>
> And you will see that there is a CompareMethod parameter which if you don't
> supply it defaults to the Option Compare setting.  Would that setting be the
> one you want?
>
> And Dennis... meant > 0 rather than >= 0 since 0 is returned in a number of
> cases to indicate the string was not found.
>
> Hope this helps...
>
>
>
>
> "Dennis" <Den***@discussions.microsoft.com> wrote in message
> news:A66C81C0-8015-4EB4-ABE7-DC5AC807DAAE@microsoft.com...
> > You might try:
> >
> > Dim str as string = URL.ToString()
> > For Each s as string in BadWords
> >    if InStr(str,s)>=0 then IE.Quit
> > next
> >
> > --
> > Dennis in Houston
> >
> >
> > "jimmy" wrote:
> >
> >> I am working on a project which tracks 'bad' words in IE and im using a
> >> For loop to check for an array of words in he address bar. I have
> >> included the broken code below. Any pointers on why it isnt working
> >> would be very useful.
> >>
> >> Private Sub BeginNavigate(ByVal pDisp As Object, ByRef URL As Object,
> >> ByRef Flags As Object, ByRef TargetFrameName As Object, ByRef PostData
> >> As Object, ByRef Headers As Object, ByRef Cancel As Boolean)
> >>         Dim i As Integer
> >>         For i = 0 To BadWords.Length - 1
> >>             If InStr(URL.ToString(), BadWords(i)) Then
> >>                 IE.Quit()
> >>             End If
> >>         Next
> >>     End Sub
> >>
> >> Thanks in advance
> >>
> >>
>
>
>
Author
16 Nov 2006 6:25 AM
Michael D. Ober
When using the native VB string functions, you can use "Option Compare Text"
to avoid case issues as well.

Mike Ober.

Show quoteHide quote
"Dennis" <Den***@discussions.microsoft.com> wrote in message
news:CED55C80-A2AF-4607-8FA0-70BECDE3953E@microsoft.com...
> You are correct..I was thinking of IndexOf method.  Also, the
> String.Compare(a,b,True) = 0 would be a better solution to avoid case
> problems.
> --
> Dennis in Houston
>
>
> "Tom Leylan" wrote:
>
> > I'm confused :-)  Is the intent to check a list of "words" against a
> > complete URL e.g. www.essexhotel.com so the sequence "sex" (given it's
> > presence in the list) would mean that one couldn't view this site?  I'm
not
> > sure that will work so well as a strategy but given that is anybody
checking
> > the docs on the InStr() method?
> >
> > To the original poster... InStr() doesn't return a boolean right? So
there
> > isn't much surprise there but let me suggest that you test your
hypotheses
> > (in the future) rather than just write code.  If is isn't working you
might
> > try typing the following into the immediate window.  I get an 11 as a
return
> > value.... the boolean test is therefore out.
> >
> > ? microsoft.VisualBasic.InStr( "this is a test", "test")
> >
> > And you will see that there is a CompareMethod parameter which if you
don't
> > supply it defaults to the Option Compare setting.  Would that setting be
the
> > one you want?
> >
> > And Dennis... meant > 0 rather than >= 0 since 0 is returned in a number
of
> > cases to indicate the string was not found.
> >
> > Hope this helps...
> >
> >
> >
> >
> > "Dennis" <Den***@discussions.microsoft.com> wrote in message
> > news:A66C81C0-8015-4EB4-ABE7-DC5AC807DAAE@microsoft.com...
> > > You might try:
> > >
> > > Dim str as string = URL.ToString()
> > > For Each s as string in BadWords
> > >    if InStr(str,s)>=0 then IE.Quit
> > > next
> > >
> > > --
> > > Dennis in Houston
> > >
> > >
> > > "jimmy" wrote:
> > >
> > >> I am working on a project which tracks 'bad' words in IE and im using
a
> > >> For loop to check for an array of words in he address bar. I have
> > >> included the broken code below. Any pointers on why it isnt working
> > >> would be very useful.
> > >>
> > >> Private Sub BeginNavigate(ByVal pDisp As Object, ByRef URL As Object,
> > >> ByRef Flags As Object, ByRef TargetFrameName As Object, ByRef
PostData
> > >> As Object, ByRef Headers As Object, ByRef Cancel As Boolean)
> > >>         Dim i As Integer
> > >>         For i = 0 To BadWords.Length - 1
> > >>             If InStr(URL.ToString(), BadWords(i)) Then
> > >>                 IE.Quit()
> > >>             End If
> > >>         Next
> > >>     End Sub
> > >>
> > >> Thanks in advance
> > >>
> > >>
> >
> >
> >
>