LEN function (1 Viewer)

smig

Registered User.
Local time
Today, 18:10
Joined
Nov 25, 2009
Messages
2,209
This code scrap would cause some head scratching to a beginner:

Code:
a = 3
If a = 6 Or 3 Then

;)
I'm scratching my head now :D
6 or 3 will result as 7 (Or am I wrong ?) so why checking if it's 3 ???

I really like to learn new thing, but you are only trying to confuse me more :D
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 01:10
Joined
Jan 20, 2009
Messages
12,854
Incorrectly testing a variable in an If using that style when they intend the following is not an uncommon beginner's mistake.

If a = 3 Or a = 6 Then

Ironically it is a valid statement but the False result would be quite unexpected.
 

smig

Registered User.
Local time
Today, 18:10
Joined
Nov 25, 2009
Messages
2,209
If a = 6 Or 3 Then

After trying the above style once or twice I found it give me a wrong answer so I never use it again.
I never knew it is a valid statement :D
I can't even see what is it good for, but now that I know it is possible I'll probably find it a good use at some point :)

A note to Markk:
I do like using binary
for example: In one of my applications I have a groups of users. To be able to set different permissions to the groups I number them using binary numbering - 1, 2, 4, 8, 16....
This way I can give permission to groups 1 and 4 only using 5.
 

MarkK

bit cruncher
Local time
Today, 08:10
Joined
Mar 17, 2004
Messages
8,186
There's no EBCDIC on PCs.

smig, yeah, you can essentially pile up multiple yes/no attributes in one number. That's what Dave is talking about. Bitwise math.
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 16:10
Joined
Sep 12, 2006
Messages
15,677
If a = 6 Or 3 Then

After trying the above style once or twice I found it give me a wrong answer so I never use it again.
I never knew it is a valid statement :D
I can't even see what is it good for, but now that I know it is possible I'll probably find it a good use at some point :)

A note to Markk:
I do like using binary
for example: In one of my applications I have a groups of users. To be able to set different permissions to the groups I number them using binary numbering - 1, 2, 4, 8, 16....
This way I can give permission to groups 1 and 4 only using 5.

No, it's not so useful. I think what galaxiom was saying is that someone could easily write

If a = b or c thinking it meant

If a=b or a=c when it actually means
If a=b or'ed with 3. A legitimate (therefore not an error) but incorrect statement .
Or it might mean if a=(b or c), again unlike to be what was intended.
 

smig

Registered User.
Local time
Today, 18:10
Joined
Nov 25, 2009
Messages
2,209
I understood this is what Galaxiom meant.

I said that as my crazy mind love playing around with stupid things I'll might find this a good use at some point :)

I already have an idea how to use it in the exemple I gave before:
Code:
If permitedGroups = myGroup or permitedGroups

If permitedGroups is 6 and im in group 2 or 4 it will be Ok, but if im in group 1 or 8 it wont :)
 
Last edited:

smig

Registered User.
Local time
Today, 18:10
Joined
Nov 25, 2009
Messages
2,209
I'm trying to use this OR thing, but something is not working for me. I get True at all cases :eek:
intAppsID Or MyAppID give me 6, as expected.

Code:
Function TestAppsID()

MsgBox fnCheckReportForThisApp(2, 4)

End Function


Public Function fnCheckReportForThisApp(intAppsID As Integer, MyAppID As Integer) As Boolean
' --- This test is based on binary test
' --- Each number (intAppsID, GetAppID()) is striped to it's Binary value
' --- The test compare the binary value Byte by Byte
' --- Example: 6 or 1 = 110 or 001 will result as 111 (Not 6) = False
' --- Example: 7 or 2 = 111 or 010 will result as 111 (7) = True

fnCheckReportForThisApp = False

If intAppsID = intAppsID Or MyAppID Then
    fnCheckReportForThisApp = True
End If

End Function

but if I change into this it will work:
Code:
Public Function fnCheckReportForThisApp(intAppsID As Integer, MyAppID As Integer) As Boolean
[COLOR="Red"]Dim intORresult As Integer

intORresult = intAppsID Or MyAppID 
[/COLOR]
fnCheckReportForThisApp = False

If intAppsID = intORresult Then
    fnCheckReportForThisApp = True
End If
 

MarkK

bit cruncher
Local time
Today, 08:10
Joined
Mar 17, 2004
Messages
8,186
Don't you want to use And, not Or? If you use And, then you can determine if an attribute is set, so if you have an enum which is a bunch of squares of two, like this . . .
Code:
Public Enum enSchoolSupplies
    enUnArmed = 0
    enUzi = 1
    enColt = 2
    enSmithWesson = 4
    enWinchester = 8
    enGlock = 16
    enGrenade = 32
End Enum
. . . then when little Billy packs his back-pack in the morning, he does it like this . . .
Code:
dim BillysSupplies as long
[COLOR="Green"]'billy is bringing an Uzi, a Colt and a Glock to school today[/COLOR]
BillysSupplies = enUzi OR enColt OR enGlock
. . . so OR has the effect of adding things together. Then, when little Billy arrives at school, his teacher might ask, "Little Billy, did you bring your daddy's Uzi to school today?" All little Billy has to do is a little bitwise math like . . .
Code:
if BillysSupplies And enUzi = enUzi Then
    Msgbox "Billy brought his daddy's Uzi to school"
end if
. . . so we use And to test a number for the presence of an attribute. If we use AND and we get the same number we're testing for, then the number is present. This way we can pile a bunch of attributes into a single number, and determine which attributes are set.
 

smig

Registered User.
Local time
Today, 18:10
Joined
Nov 25, 2009
Messages
2,209
In my case Billy pack one gun only.
i want to check if he brought one of the guns allowed, not if he brought a specific one :D


But back to my question:
why do you put -
BillysSupplies = enUzi OR enColt OR enGlock

and only after that do the check:
if BillysSupplies And enUzi = enUzi Then


why can't you ask (These ones give me a wrong result):
if (enUzi OR enColt OR enGlock) And enUzi = enUzi Then...
OR
if enUzi = (enUzi OR enColt OR enGlock) And enUzi Then...
 

MarkK

bit cruncher
Local time
Today, 08:10
Joined
Mar 17, 2004
Messages
8,186
In my case Billy pack one gun only.
i want to check if he brought one of the guns allowed, not if he brought a specific one
Code:
If BillysSupplies > enUnArmed Then
    Debug.Print "Billy is armed."
End If
If BillysSupplies is non-zero, then billy has a gun. You don't know which gun, or how many.
And for this . . .
Code:
If (enUzi OR enColt OR enGlock) And enUzi = enUzi Then
Change to this . . .
Code:
If ((enUzi OR enColt OR enGlock) And enUzi) = enUzi Then
. . . and you should get the result you expect.
 

MarkK

bit cruncher
Local time
Today, 08:10
Joined
Mar 17, 2004
Messages
8,186
But back to my question:
why do you put -
BillysSupplies = enUzi OR enColt OR enGlock

and only after that do the check:
if BillysSupplies And enUzi = enUzi Then
Just to show the difference between OR and AND in a practical implementation of bitwise math.
 

smig

Registered User.
Local time
Today, 18:10
Joined
Nov 25, 2009
Messages
2,209
I know he's having one gun only, in my case :) I want to check if this gun is in the allowed ones list.
The code worked OK for me.
Problem was I had to use two line of codes (I took your code here):
BillysSupplies = enUzi OR enColt OR enGlock
if BillysSupplies And enUzi = enUzi Then

Using the brackets as you suggested I can now have one line of code (again your code) :)
if ((enUzi OR enColt OR enGlock) And enUzi) = enUzi Then


I'm using OR for my tests and it's OK.
I was only missing the brackets now.
 

Users who are viewing this thread

Top Bottom