Loop not working in VB.NET

J

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
 
J

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
 
T

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.
 
R

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
 
G

Guest

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
 
T

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...
 
C

Chris Dunaway

Tom said:
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!
 
G

Guest

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.
 
M

Michael D. Ober

When using the native VB string functions, you can use "Option Compare Text"
to avoid case issues as well.

Mike Ober.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top