943,630 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Sep 28th, 2005
0

If functions

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
grandfilth is offline Offline
16 posts
since Sep 2005
Sep 29th, 2005
0

Re: If functions

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
Reputation Points: 24
Solved Threads: 6
Junior Poster in Training
purplegerbil is offline Offline
78 posts
since Apr 2005
Sep 29th, 2005
0

Re: If functions

cheers, that's very helpful...
have a good one
Reputation Points: 10
Solved Threads: 0
Newbie Poster
grandfilth is offline Offline
16 posts
since Sep 2005
Sep 29th, 2005
0

Re: If functions

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

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. If Z = 1 Or If Z = 3 Or If Z = 5 Then<blockquote>SIDE = W - (V/3600#)</blockquote>End if
  2. If Z = 2 Or If Z = 4 Or If Z = 6 Then<blockquote>SIDE = W + (V/3600#)</blockquote>End If
Reputation Points: 11
Solved Threads: 2
Junior Poster in Training
w00dy is offline Offline
70 posts
since Jun 2005
Sep 29th, 2005
0

Re: If functions

Yeah that's true, but I got the idea...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
grandfilth is offline Offline
16 posts
since Sep 2005
Oct 2nd, 2005
0

Re: If functions

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

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. SIDE = W + ((-1) ^ Z)*(V / 3600#)
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005
Oct 2nd, 2005
0

Re: If functions

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?
Reputation Points: 11
Solved Threads: 2
Junior Poster in Training
w00dy is offline Offline
70 posts
since Jun 2005
Oct 2nd, 2005
0

Re: If functions

first i check grandfilth code pattern
then we got

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. If Z = 1 Or Z = 3 Or Z= 5 Then
  2. SIDE = W - (V / 3600#)
  3. Else
  4. SIDE = W + (V / 3600#)
  5. 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
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005
Oct 2nd, 2005
0

Re: If functions

and i got another one line solution

Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. SIDE = IIf(Z Mod 2, W - (V / 3600#), W + (V / 3600#))
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005
Oct 2nd, 2005
0

Re: If functions

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.
Reputation Points: 11
Solved Threads: 2
Junior Poster in Training
w00dy is offline Offline
70 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Visual Basic 4 / 5 / 6 Forum Timeline: hi
Next Thread in Visual Basic 4 / 5 / 6 Forum Timeline: Brand new to VB





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC