InStr test

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

hey all,
i'm getting a result that i don't understand

i have a string "test1, test2"

If InStr("test1") and InStr("test2") Then
'Inside
EndIf

The inside is not running for some reason. Any ideas?

thanks,
rodchar
 
As =?Utf-8?B?cm9kY2hhcg==?= once said in
microsoft.public.dotnet.framework.aspnet
hey all,
i'm getting a result that i don't understand

i have a string "test1, test2"

If InStr("test1") and InStr("test2") Then
'Inside
EndIf

The inside is not running for some reason. Any ideas?

Yes. You are not using the wrong syntax for Instr.

I surprised this even compiles.
 
hey all,
i'm getting a result that i don't understand

i have a string "test1, test2"

If InStr("test1") and InStr("test2") Then
'Inside
EndIf

The inside is not running for some reason. Any ideas?

Er, well it's been a while since I've used any flavour of Basic, but doesn't
InStr require more than one argument...?

Switch Option Strict on...
 
Try this:

If InStr("test1")>=0 and InStr("test2")>=0 Then

Also, to add efficiency you should use AndAlso instead of And.
 
rodchar said:
hey all,
i'm getting a result that i don't understand

i have a string "test1, test2"

If InStr("test1") and InStr("test2") Then
'Inside
EndIf

The inside is not running for some reason. Any ideas?

thanks,
rodchar

The first reason is that the code will not compile at all. The InStr
function requires two parameters.

Furthermore, the InStr function returns an integer, not a boolean. If
you use the and operator between two integers, it will perform a binary
and between the operators. With your example, the first InStr returns
the value 1 and the second returns the value 8. The binary and operation
between 1 and 8 gives the value 0. That is then converted to the boolean
value False when it's used in the If statement.
 
Hi,
hey all,
i'm getting a result that i don't understand

i have a string "test1, test2"

If InStr("test1") and InStr("test2") Then
'Inside
EndIf

The inside is not running for some reason. Any ideas?

what exactly are you wanting to do?
Using InStr, you can look for the position of the first occurance of a
string within another, starting at a certain position within the string.

dim strLookFor as string="test"
If (InStr(1, "test1", strLookFor)+InStr(1, "test2", strLookFor))>0 Then
'Inside
EndIf

BTW - instead of InStr, you may as well use the IndexOf-function which is a
member of system.string. IOW, if you are checking a string-variable, use
strYourString.IndexOf(...).

Guess it'd be better if you explained what exactly you're looking for.

Cheers,
Olaf
 
My apologies to everyone i should have state that i was not strict on syntax
and i was focused more on the concept of this:
I thought, 0 meant false and >0 meant true without having to be explicit.

If I'm explicit like you, it works:
If InStr("test1")>=0 and InStr("test2")>=0 Then
....
 
Hi,
My apologies to everyone i should have state that i was not strict on syntax
and i was focused more on the concept of this:
I thought, 0 meant false and >0 meant true without having to be explicit.

If I'm explicit like you, it works:
...

if you're after concepts, what exactly is your question then?

Cheers,
Olaf
 
I already answered his conceptual question to his satisfaction.
There was confusion about the return value of the Instr function. All
cleared up now.
 
Back
Top