InStr test

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
 
K

kpg

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

Mark Rae

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

Steve C. Orr [MVP, MCSD]

Try this:

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

Also, to add efficiency you should use AndAlso instead of And.
 
G

Guest

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

Olaf Rabbachin

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
 
K

kpg

As Mark Rae once said in microsoft.public.dotnet.framework.aspnet
I think the OP *is* using the wrong syntax for InStr...

! um, yes, that's what I meant...
 
G

Guest

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

Olaf Rabbachin

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
 
S

Steve C. Orr [MVP, MCSD]

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

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top