954,119 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

If functions

HI, sorry if this is really thick, but I'm abit stuck...I have written this piece of code as part of a bigger function:

If Z = 1 Then
SIDE = W - (V / 3600#)
End If
If Z = 2 Then
SIDE = W + (V / 3600#)
End If
If Z = 3 Then
SIDE = W - (V / 3600#)
End If
If Z = 4 Then
SIDE = W + (V / 3600#)
End If
If Z = 5 Then
SIDE = W - (V / 3600#)
End If
If Z = 6 Then
SIDE = W + (V / 3600#)
End If

I know it's a stupid way of doing it, but I tried:
If Z = 1 or 3 or 5 Then
SIDE = W - (V / 3600#)
else
SIDE=W + (V / 3600#)
End If

Could someone please help me to find a neater way of telling it to perform different formula for odd and even numbers?

Any help would be much appreciated

grandfilth
Newbie Poster
16 posts since Sep 2005
Reputation Points: 10
Solved Threads: 0
 

If Z=1 or Z=2 or Z=3 or Z=4 or Z=5 or Z=6 then
SIDE = W - (V / 3600#)
End If

purplegerbil
Junior Poster in Training
78 posts since Apr 2005
Reputation Points: 24
Solved Threads: 6
 

cheers, that's very helpful...
have a good one

grandfilth
Newbie Poster
16 posts since Sep 2005
Reputation Points: 10
Solved Threads: 0
 

Sorry purplegerbil, but that makes all 6 values equal the same thing.
Try this:

If Z = 1 Or If Z = 3 Or If Z = 5 Then[INDENT]SIDE = W - (V/3600#)[/INDENT]End if
If Z = 2 Or If Z = 4 Or If Z = 6 Then[INDENT]SIDE = W + (V/3600#)[/INDENT]End If
w00dy
Junior Poster in Training
70 posts since Jun 2005
Reputation Points: 11
Solved Threads: 2
 

Yeah that's true, but I got the idea...

grandfilth
Newbie Poster
16 posts since Sep 2005
Reputation Points: 10
Solved Threads: 0
 

if Z is interger, you also can do this way without using If

SIDE = W + ((-1) ^ Z)*(V / 3600#)
invisal
Posting Pro
562 posts since Mar 2005
Reputation Points: 350
Solved Threads: 64
 

I just realised it should have been

If Z = 1 Or Z = 3 Or Z= 5 (only one If at the start of the line, sorry, silly mistake)

However, Invisal, I like neat one-liners but I don't actually follow that one, could you explain it?

w00dy
Junior Poster in Training
70 posts since Jun 2005
Reputation Points: 11
Solved Threads: 2
 

first i check grandfilth code pattern
then we got

If Z = 1 Or Z = 3 Or Z= 5 Then
     SIDE = W - (V / 3600#)
Else
     SIDE = W + (V / 3600#)
End If


mean if Z = odd number then SIDE = W- (V / 3600#)
and if Z = not odd number then SIDE = W + (V / 3600#)

my solution is SIDE = W + ((-1) ^ Z) * (V / 3600#)

if Z = odd number like 1, 3, 5 and etc..., then (-1)^Z = -1
if Z <> odd number like 2, 4, 6 and etc..., then (-1)^Z = 1


I am not good on explain so i hope you understan

invisal
Posting Pro
562 posts since Mar 2005
Reputation Points: 350
Solved Threads: 64
 

and i got another one line solution

SIDE = IIf(Z Mod 2, W - (V / 3600#), W + (V / 3600#))
invisal
Posting Pro
562 posts since Mar 2005
Reputation Points: 350
Solved Threads: 64
 

Interesting, that. I'll do a test project and play with it, and the Iif one.
Have to say though that although I've used Iif before, I found that it didn't cope with complicated logic very well, so I stopped using it even though it meant a few more lines.

w00dy
Junior Poster in Training
70 posts since Jun 2005
Reputation Points: 11
Solved Threads: 2
 

hey man ur coding isnt working ..........plz work on it
Imran Hashmi www.visionstudio.co.uk 0044-7969012441
Imran Hashmi www.seo-professional.co.uk 0044-7969012441

dntchasemee
Newbie Poster
2 posts since Oct 2005
Reputation Points: 10
Solved Threads: 0
 
hey man ur coding isnt working ..........plz work on it

Which of us are you talking to ??

w00dy
Junior Poster in Training
70 posts since Jun 2005
Reputation Points: 11
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You