Thread: dim trouble
View Single Post
  #3   Report Post  
JE McGimpsey
 
Posts: n/a
Default

The integer data type can contain positive integers up to 32767.

Since both HOUR and MINUTE are declared as Integer, and both 3600 and 60
are less than 32767, the internal multiplication will use the more
restrictive type (Integer). So if HOUR9, the multiplication creates an
overflow even though the result would eventually be cast as a Long
(since SEC is declared as long).

You don't really gain anything by typing as Integer - the storage
required is the same as for Longs, and, IIRC, math operations are no
less efficient, so declare HOUR and MINUTE as Longs.

Alternatively, you can coerce the internal result to a Long by appending
the Long type-declaration character (&) to your constant:

SEC = (HOUR * 3600&) + (MINUTE * 60)

In article ,
jocke wrote:

HI, i've made a small function that counts the seconds i hours and
minutes

if i try to convert hours larger then 9 i get an error that says my
variable isn't
declared right. but i can't see anything wrong

Help wanted


Function sec(Invarde As String) As Long
Dim mystring As String
Dim antal As Integer
Dim hour As Integer
Dim minute As Integer
mystring = Invarde
antal = Len(mystring)
hour = Left(mystring, antal - 3)
minute = Right(mystring, 2)
SEC = (HOUR * 3600) + (MINUTE * 60)
End Function