If functions

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2005
Posts: 16
Reputation: grandfilth is an unknown quantity at this point 
Solved Threads: 0
grandfilth grandfilth is offline Offline
Newbie Poster

If functions

 
0
  #1
Sep 28th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 75
Reputation: purplegerbil is an unknown quantity at this point 
Solved Threads: 6
purplegerbil's Avatar
purplegerbil purplegerbil is offline Offline
Junior Poster in Training

Re: If functions

 
0
  #2
Sep 29th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 16
Reputation: grandfilth is an unknown quantity at this point 
Solved Threads: 0
grandfilth grandfilth is offline Offline
Newbie Poster

Re: If functions

 
0
  #3
Sep 29th, 2005
cheers, that's very helpful...
have a good one
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 70
Reputation: w00dy is an unknown quantity at this point 
Solved Threads: 2
w00dy w00dy is offline Offline
Junior Poster in Training

Re: If functions

 
0
  #4
Sep 29th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 16
Reputation: grandfilth is an unknown quantity at this point 
Solved Threads: 0
grandfilth grandfilth is offline Offline
Newbie Poster

Re: If functions

 
0
  #5
Sep 29th, 2005
Yeah that's true, but I got the idea...
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: If functions

 
0
  #6
Oct 2nd, 2005
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#)
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 70
Reputation: w00dy is an unknown quantity at this point 
Solved Threads: 2
w00dy w00dy is offline Offline
Junior Poster in Training

Re: If functions

 
0
  #7
Oct 2nd, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: If functions

 
0
  #8
Oct 2nd, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: If functions

 
0
  #9
Oct 2nd, 2005
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#))
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 70
Reputation: w00dy is an unknown quantity at this point 
Solved Threads: 2
w00dy w00dy is offline Offline
Junior Poster in Training

Re: If functions

 
0
  #10
Oct 2nd, 2005
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2497 | Replies: 11
Thread Tools Search this Thread



Tag cloud for Visual Basic 4 / 5 / 6
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC