|
-
Feb 6th, 2004, 04:24 PM
#1
if statement
Is there a way to make sure something is not two things within one if statement?
I don't want to write out two different if statements, but will if needed. I want to cut down code, though.
I want something like:
VB Code:
If var <> ("Text1" Or "Text2") Then
I'm just not sure if this is possible or not.
-
Feb 6th, 2004, 04:26 PM
#2
Frenzied Member
Are you asking if "or" is valid?
If so, yes.. and so is "and"
-
Feb 6th, 2004, 04:28 PM
#3
uhh.. no.
I'm not really a beginner..
I don't know how I could make it clearer, I'm asking if I can put two conditions into the same if statement, but I don't want to do have to do the norm, IE:
VB Code:
If var <> "Text1" Or var <> "Text2" Then
I don't want to enter the variable twice.
-
Feb 6th, 2004, 04:37 PM
#4
Addicted Member
if(var="Text1" or var="Text2") then
-
Feb 6th, 2004, 04:43 PM
#5
ok, look.. I already know that can be done, I showed that I knew that in my second post. I don't want to enter the variable name twice, I just want to check it's contents within one if statement, like I showed in my first post with the code I made up..
Anyone even understand what I want, or no?
-
Feb 6th, 2004, 04:48 PM
#6
Frenzied Member
Why not just create a new project and try it?
Then come back and read my first answer.
-
Feb 6th, 2004, 05:00 PM
#7
I already have tried the code I made.. why would I come ask for help if I already had the code?
I just made a new project and tried it anyway..
VB Code:
Dim var1 As String, var2 As String, var3 As String
Private Sub Form_Load()
var1 = "BLAH"
var2 = "BLAH2"
End Sub
Private Sub Text1_Change()
var3 = Text1.Text
If var3 = (var1 Or var2) Then
MsgBox "!!"
End If
End Sub
Try it yourself.
-
Feb 6th, 2004, 05:43 PM
#8
I've always wanted to do that, too, but in most cases it doesn't really make logical sense. We talk that way, but it is just a sort of shorthand representation in speech.
When you say "A is greater than B or C", you mean "A is greater than B, and A is greater than C". We understand what you mean, but the statement is a form of logical concatenation. Of course, in some situations, you could make a program do that, but it takes a contortion that can be expensive.
-
Feb 6th, 2004, 07:17 PM
#9
from these guys' replies, they didn't get what I meant at all..
-
Feb 6th, 2004, 07:54 PM
#10
kows
You can do that, but it will not have the same meaning logically.
What you want:
Example values for A = 1, B = 1, C = 2
if A <> B and A <> C gives you
Is 1 <> 1 and 1 <> 2
What the orther statement actually give you
Is A <> (B or C)
if A <> (1 or 2)
In which the value in parenthisis is actually equated to 2. so you get (representing a logical OR).
if A <> (2)
This is why you cannot do what you want.
-
Feb 6th, 2004, 07:57 PM
#11
PowerPoster
Hi,
Hey guys, don't get heated! Communication is always one of the big problems in Forums.
Kows, I think that one of the reasons for the misunderstanding is that you are asking for something that is just not available in VB Computors are not as intelligent as (most) humans. They can only understand very simple steps. Also when you say "I don't want to write out two different if statements, " the following code which was quoted to you by john24 IS only one statement.
"if(var="Text1" or var="Text2") then"
and it works with or without the brackets. You HAVE to clearly state the conditions you are applying. You cannot miss out words (in this case the name of the object) just because you, as a human, know what you mean - the computor doesn't. On the other hand, the computor, being unable to think, carries out the statement far quicker that you can, so it is not necessary to take short cuts in the language. If you are not happy with this, then you will just have to invent your own computor language!! That will take you about 10 million man-hours!
Last edited by taxes; Feb 7th, 2004 at 04:35 AM.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Feb 6th, 2004, 08:05 PM
#12
PowerPoster
Hi Randem,
"You can do that, but it will not have the same meaning logically."
He CAN'T do that!!!!!!!
As for the rest of your post, does anyone else have the slightest idea of what you mean? I'm afraid I don't.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Feb 6th, 2004, 08:13 PM
#13
PowerPoster
Hi ShaggyHiker,
"When you say "A is greater than B or C", you mean "A is greater than B, and A is greater than C". "
NO he doesn't!
He is stating a condition that A must be greater than B or C.
A does not have to be greater than both B and C.
e.g. if a =3, b=2, and c=5
then A > B or C is true ( A is greater than B)
but a> B and C is false ( A is greater than B but smaller than C).
Oh boy. Some of you guys need to sit down with an icepack on your foreheads
Don't take offence anyone, but you do have to read the questions carefully and not jump in with the first thing that comes into your head.
Last edited by taxes; Feb 7th, 2004 at 04:33 PM.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Feb 6th, 2004, 09:19 PM
#14
taxes
If you don't have the slightest idea... why reply? And he can do that. It is not impossible, he will just get the wrong meaning most of the time.
Read whole statements. You acuse others of what you do.
And simple math would help also.
-
Feb 6th, 2004, 10:31 PM
#15
Addicted Member
Kows, the short answer to your question is no.
But if all you want to do is shorten down your code then you could write a function to do that. Then you can just call the function like:
VB Code:
If twoIFs(var, "Text1", "Text2") then
Of course that will slow down your project if you're calling it thousands of times a second.
-
Feb 7th, 2004, 01:31 AM
#16
never actually thought about a function imbue, think I'll try that and see how it works out for now.
oh, also, taxes.. I'm not programming in VB.NET, so it wouldn't matter if it was available in it or not. I know what a programming language is and I also know how it works, so there's no need for you to 'explain' it. It would be nice to have something like that built into a future version of VB, though.
Last edited by kows; Feb 7th, 2004 at 01:35 AM.
-
Feb 7th, 2004, 04:44 AM
#17
PowerPoster
Hi Kows,
"oh, also, taxes.. I'm not programming in VB.NET, so it wouldn't matter if it was available in it "
My typing error, I should have said "not available in VB".
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Feb 7th, 2004, 08:30 AM
#18
Fanatic Member
Curse VB's excessive operator overloading. It overloads ( ), =, and the or/and operators a tad too much IMO. Having separate operators for bitwise and for logical or'ing would be much clearer.
-
Feb 7th, 2004, 05:58 PM
#19
Addicted Member
sorry that i read only the first few replies (in case
my answer seems irrelavent)
what you want is not possible......... !!exactly!!,
but i have a way of doing what you want in
one logical test, but the result will not
reflect what you want 100% but maybe about 99%.
instead of
VB Code:
If var<>"text1" Or var<>"text2" Then
use
VB Code:
If Not "text1text2" Like "*" & var & "*" Then
im sure i once heard of a IsIn function, so that
"me" IsIn "kemer" ... returns true, im not sure
which language supports this test,
check out if its in vb, this might be used instead of like statement.
-
Feb 7th, 2004, 09:27 PM
#20
PowerPoster
Hi Randem,
I'm sorry, but I HAVE read your post carefully - several times but I just cannot make any sense out of your comments:
"What the orther statement actually give you
Is A <> (B or C)
if A <> (1 or 2)"
(I understand those bits but not the following)
"In which the value in parenthisis is actually equated to 2. so you get (representing a logical OR).
if A <> (2)"
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Feb 7th, 2004, 09:35 PM
#21
PowerPoster
Hi ZaidGS
"im sure i once heard of a IsIn function, so that
"me" IsIn "kemer" ... returns true, im not sure
which language supports this test, "
Are you thinking of the InStr function of VB6 and VB.NET?
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Feb 7th, 2004, 10:44 PM
#22
taxes,
I made a slight mistake,
A <> (B or C) - Represented by A=1, B=1, C=2 then would be
1 <> (1 or 2) - When you logically or a 1 and a 2 you get 3 so this equates to:
1 <> (3) - So 1 <> 3 is true, but it's not exactly what is wanted.
Logically
1 or 2 = 3
1 or 3 = 3
1 or 4 = 5
1 or 5 = 5
2 or 3 = 3
2 or 4 = 6
so on an so on.
-
Feb 8th, 2004, 01:20 AM
#23
Addicted Member
Yo!
I don't know if this has been resolved yet, but using the code that kows posted the third time, with a little change, it should work the way he wants it too:
VB Code:
Option Explicit
Dim var1 As String, var2 As String, var3 As String
Private Sub Form_Load()
var1 = "BLAH"
var2 = "BLAH2"
End Sub
Private Sub Text1_Change()
var3 = Text1.Text
If var3 = var1 Or var3 = var2 Then
MsgBox ("!!")
End If
End Sub
Anyway that worked for me!!!!!
I think you'll have to use the var3 twice in the if statement. If i used:
VB Code:
If var3 = (var1 or var2) then
I got an error: Type Mismatch
but if i used:
VB Code:
If var3 = var1 or var3 = var2 then
It worked fine. So i'm pretty sure you'll have to used the var3 twice, anyway, you're only using like 3 more words in the code
Last edited by SuperChris9; Feb 8th, 2004 at 01:25 AM.
-
Feb 10th, 2004, 01:01 PM
#24
Addicted Member
Originally posted by taxes
Hi ZaidGS
"im sure i once heard of a IsIn function, so that
"me" IsIn "kemer" ... returns true, im not sure
which language supports this test, "
Are you thinking of the InStr function of VB6 and VB.NET?
how does that work ?!?!
-
Feb 10th, 2004, 05:44 PM
#25
PowerPoster
Hi ZaidGS,
From MSDN Help Index Instr function.
InStr Function
Returns a Variant (Long) specifying the position of the first occurrence of one string within another.
Syntax
InStr([start, ]string1, string2[, compare])
The InStr function syntax has thesearguments:
Part Description
start Optional.Numeric expression that sets the starting position for each search. If omitted, search begins at the first character position. If start containsNull, an error occurs. The start argument is required if compare is specified.
string1 Required.String expression being searched.
string2 Required. String expression sought.
compare Optional. Specifies the type ofstring comparison. If compare is Null, an error occurs. If compare is omitted, the Option Compare setting determines the type of comparison.
Settings
The compare argument settings are:
Constant Value Description
vbUseCompareOption -1 Performs a comparison using the setting of the Option Compare statement.
vbBinaryCompare 0 Performs a binary comparison.
vbTextCompare 1 Performs a textual comparison.
vbDatabaseCompare 2 Microsoft Access only. Performs a comparison based on information in your database.
Return Values
If InStr returns
string1 is zero-length 0
string1 is Null Null
string2 is zero-length start
string2 is Null Null
string2 is not found 0
string2 is found within string1 Position at which match is found
start > string2 0
Remarks
The InStrB function is used with byte data contained in a string. Instead of returning the character position of the first occurrence of one string within another, InStrB returns the byte position.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Feb 10th, 2004, 10:11 PM
#26
Addicted Member
For the record...
Dim ImChubbyToo as Boolean, MyChubbyUncle as Boolean, MyChubbyAunt as Booean
MyChubbyAunt = True
MyChubbyUncle = True
This latter will out perform the following...
'Comment: must evaluate both clauses
If (MyChubbyAunt and MyChubbyUncle) then
ImChubbyToo = True
End If
'Comment: must evaluate first clause only... if true, then evaluate second clause = Faster Code
If MyChubbyAunt then
If MyChubbyUncle then
ImChubbyToo = True
End If
End If
So.. to sum up... what you're doing is bad practice. Evaluate variables only as required.
Last edited by EZapps; Feb 10th, 2004 at 10:15 PM.
-
Feb 11th, 2004, 04:50 AM
#27
PowerPoster
Hi Ezaps,
Yes, this works with Select Case also BUT:
"If (MyChubbyAunt and MyChubbyUncle) then"
really is
"If (MyChubbyAunt = True and MyChubbyUncle=True) then"
so that it does not achieve what kows wants.
However, I agree with your final statement.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|