An If Then but Different

  • Thread starter Thread starter Brian P. Hammer
  • Start date Start date
B

Brian P. Hammer

All,

I am trying to calculate an if statement base of a value of 12 multiplier. I'd much rather not have to type this all the way up for 60 years. How can I turn my if statement into something like If Period = 12 or multiplier of 12 then?


For Period = 1 To TotPmts
'My period Work
If Period = 12 Or Period = 24 Or Period = 36 Or Period = 48 Or Period = 60 _
Or Period = 72 Or Period = ...........Then

End If

Next Period


Thanks,
Brian P. Hammer
 
Brian,

On solution would be to use the Mod operator

If Period Mod 12 = 0 Then
'Its a multiple of 12
End If

Stephen

All,

I am trying to calculate an if statement base of a value of 12 multiplier.
I'd much rather not have to type this all the way up for 60 years. How can
I turn my if statement into something like If Period = 12 or multiplier of
12 then?


For Period = 1 To TotPmts
'My period Work
If Period = 12 Or Period = 24 Or Period = 36 Or Period = 48 Or
Period = 60 _
Or Period = 72 Or Period = ...........Then

End If

Next Period


Thanks,
Brian P. Hammer
 
You know, I didn't even think about that. I'll give it a try. I am sure it
will work because it's so simple. :-)
 
Brian,
If the Mod operator does apply. (It should in this case, I'm saying in other
cases).

- You can use a Select Case.

Select Case period
Case 12, 24, 36, 48, 60
' good period
Case Else
End Select

- Create a function that does the check.

If IsAnnual(period) Then
' good period
End If

Function IsAnnual(period as integer) As Boolean
Return period mod 12
End Function

- Store the values in an array, and check the array for the value.

Dim periods() As Integer = {12, 24, 36, 48, 60}
If Array.IndexOf(periods, period) >= 0 Then
' good period
End If

' If periods is sorted, binary search is faster!
If Array.BinarySearch(periods, period) >= 0 Then
' good period
End If

Of course with multiples of 12, Mod is easier. Even with the Mod operator I
would consider a function, as the function name indicates what you are
checking, were as Mod just says you are doing a Mod. Of course you could put
a comment before the Mod, However the function name would be my comment...
Actually depending on how I was using period throughout my app I would
consider making it a Type itself, then my check would be "If period.IsAnnual
Then" as the Period Type would encapsulate all the functionality unique to
Periods... By Type I mean a Structure or Class depending on how I was using
the Period Type.

Hope this helps
Jay


All,

I am trying to calculate an if statement base of a value of 12 multiplier.
I'd much rather not have to type this all the way up for 60 years. How can
I turn my if statement into something like If Period = 12 or multiplier of
12 then?


For Period = 1 To TotPmts
'My period Work
If Period = 12 Or Period = 24 Or Period = 36 Or Period = 48 Or
Period = 60 _
Or Period = 72 Or Period = ...........Then

End If

Next Period


Thanks,
Brian P. Hammer
 
Wow Jay, thanks for the pointers. I had thought about a case select but
after about 132, I have to break out the calculator. :)

Using a type in this case is beyond what I need. I merely made a function
for my some loan numbers and return the principle and interest in the
function and then give the user a chance to view the amortization schedule.
I needed the Mod to separate the years, then I total each column in my array
and display it all in a rich text.

Thanks,
 
Function IsAnnual(period as integer) As Boolean
Return period mod 12
End Function

Will this work with Option Strict On? In any event, I think it is better
like this:

Function InAnnual(period As Integer) As Boolean
Return (period mod 12) = 0
End Function

Your original function returned an integer (?) when a boolean was expected.
I realize that the compiler my be able to do an implicit conversion, but my
opinion is that conversions between integer and boolean should not be
allowed.

Just my 2 cents (probably worth less due to inflation and the economy!)

Chris
 
Chris,
Remember, the intent of the sample was a quick sample of encapsulating the
formula in a function. It was not an example of the correct syntax to use
when writing said function.

You are correct it will fail with Option Strict on!

Thanks
Jay
 
Back
Top