Overflow? What gives?
Question posted by: AmazingAccuracy.com
(Guest)
on
April 24th, 2007 08:35 PM
I'm puzzled.
The following code is designed to take a date and a weekday count value, and
return the future date.
For example, if I supply it a starting date of 4/25/2007 and a count of 37,
it should return a date that is 37 weekdays only from 4/25/2007. It should
not count any weekends, only Monday-Friday days.
When I ran this code, it came back with an OVERFLOW error. Huh?
Function ForwardDate(ByVal EndDate As Date, _
ByVal DaysForward As Double) As Date
Dim RoundedDays As Long
Dim x As Long
Dim CalendarDays As Long
Dim Weeks As Integer
Dim Remainder As Single
Dim TimeDay As Date
RoundedDays = CInt(DaysForward)
Weeks = Fix(RoundedDays / 5) 'trading day weeks
Remainder = RoundedDays Mod 5 'days left over
'***** THIS IS THE LINE THAT GIVES THE ERROR *****
'when Weeks = 5731. That comes out to 40117 which should be within
'the LONG range of CalendarDays.
CalendarDays = Weeks * 7 'convert trading day weeks to calendar days
TimeDay = EndDate + CalendarDays 'expand out by calendardays. should
fall on same weekday
'Now add the remainder days excluding weekends
For x = 1 To Remainder
TimeDay = TimeDay + 1
If Weekday(TimeDay) = vbSaturday Then: TimeDay = TimeDay + 2 'shift
from Saturday to Monday
Next x
ForwardDate = TimeDay
End Function
======================
Program stops with an Overflow error on the line CalendarDays = Weeks * 7.
Looking at Weeks, it was equalling 5731. That is within range for an INT.
And when multiplied by 7, that should be within range of a LONG.
Can anyone see what I did wrong here?
Thanks.
Webbiz
|
|
April 24th, 2007 09:05 PM
# 2
|
Re: Overflow? What gives?
Dim Weeks As Integer
Quote:
>
'***** THIS IS THE LINE THAT GIVES THE ERROR *****
'when Weeks = 5731. That comes out to 40117 which should be within
'the LONG range of CalendarDays.
>
CalendarDays = Weeks * 7 'convert trading day weeks to calendar days
|
Because Weeks is declared as an Integer and 7 can fit into an Integer, VB
stores the intermediate calculation in as Integer, or at least it tries
to... 40117 is too big to fit in an Integer, so you get an overflow error.
There is almost no reason to declare variables as Integer (Longs are faster
in a 32-bit world), so I would simply declare Weeks as Long, which should
take care of your problem (as long as one of the values is bigger than an
Integer, VB will not perform the intermediate calculations as Integer, even
if the other value is an Integer). If you need Weeks to be an Integer for
other reasons, then simply change 7 to 7& (which make 7 a Long value); or
change the 7 to CLng(7) which has the same effect.
Rick
|
|
April 24th, 2007 09:05 PM
# 3
|
Re: Overflow? What gives?
For future reference...
From a post by Jeff Johnson:
"You have posted this question individually to multiple groups.
This is called Multiposting and it's BAD. Replies made in one
group will not be visible in the other groups, which may cause
multiple people to respond to your question with the same answer
because they didn't know someone else had already done it. This
is a waste of time.
If you MUST post your message to multiple groups, post a single
message and select all the groups (or type their names manually
in the Newsgroups field, separated by commas) in which you want
it to be seen. This is called Crossposting and when used properly
it is GOOD."
Some additional comment previously posted by me:
"You may not see this as a problem, but those of us who volunteer
answering questions on newsgroups do see it as a problem. You can't
imagine how annoying it is for a volunteer to read a question,
research background material, test sample code and then formulate
and post an answer to the original question only to go to another
newsgroup and find the question posted and ALREADY answered over
there. On top of that, if you cross-post your question, all of the
readers in all the newsgroups it is cross-posted to see both the
original question and all of the answers given to it. This is
beneficial to you because then we can add additional material to,
add clarification to, as well as add additional examples to an
answer you have received previously... that means you end up with
a more complete solution to your problem. This is a win-win
situation for all of us."
Rick
|
|
April 24th, 2007 11:45 PM
# 4
|
Re: Overflow? What gives?
Thanks Rick.
After some thought, I thought that maybe this was the case, that VB was
'temporarily' storing the result in Weeks thus causing the overflow. So I
had changed it to Long type.
Thanks for the advice to do so for all my Ints in the future.
Regards,
Webbiz
"Rick Rothstein (MVP - VB)" <rickNOSPAMnews@NOSPAMcomcast.netwrote in
message news:UJCdnXx-XOHb8rPbnZ2dnUVZ_vOlnZ2d@comcast.com...
Quote:
Quote:
>Dim Weeks As Integer
>>
> '***** THIS IS THE LINE THAT GIVES THE ERROR *****
> 'when Weeks = 5731. That comes out to 40117 which should be within
> 'the LONG range of CalendarDays.
>>
> CalendarDays = Weeks * 7 'convert trading day weeks to calendar days
|
>
Because Weeks is declared as an Integer and 7 can fit into an Integer, VB
stores the intermediate calculation in as Integer, or at least it tries
to... 40117 is too big to fit in an Integer, so you get an overflow error.
There is almost no reason to declare variables as Integer (Longs are
faster in a 32-bit world), so I would simply declare Weeks as Long, which
should take care of your problem (as long as one of the values is bigger
than an Integer, VB will not perform the intermediate calculations as
Integer, even if the other value is an Integer). If you need Weeks to be
an Integer for other reasons, then simply change 7 to 7& (which make 7 a
Long value); or change the 7 to CLng(7) which has the same effect.
>
Rick
>
|
|
|
April 24th, 2007 11:45 PM
# 5
|
Re: Overflow? What gives?
Sorry about that.
I had lost all my newsgroups and wasn't sure which of the two VB newsgroups
was the one I used to participate in months back. I tried to find names I
recognized (like yours) and didn't see any in the initial list that came up
when I clicked on the newsgroup name.
In addition, I noted some 'not so nice' language used on one of the two and
figured it may not be the same newsgroup I participated in before. But I
wasn't sure. So I posted in both and thought to see which is actually the
one that has good participation.
Webbiz
"Rick Rothstein (MVP - VB)" <rickNOSPAMnews@NOSPAMcomcast.netwrote in
message news:_ZmdnUGqZZJq8rPbnZ2dnUVZ_o6gnZ2d@comcast.com. ..
Quote:
For future reference...
>
From a post by Jeff Johnson:
>
"You have posted this question individually to multiple groups.
This is called Multiposting and it's BAD. Replies made in one
group will not be visible in the other groups, which may cause
multiple people to respond to your question with the same answer
because they didn't know someone else had already done it. This
is a waste of time.
>
If you MUST post your message to multiple groups, post a single
message and select all the groups (or type their names manually
in the Newsgroups field, separated by commas) in which you want
it to be seen. This is called Crossposting and when used properly
it is GOOD."
>
Some additional comment previously posted by me:
>
"You may not see this as a problem, but those of us who volunteer
answering questions on newsgroups do see it as a problem. You can't
imagine how annoying it is for a volunteer to read a question,
research background material, test sample code and then formulate
and post an answer to the original question only to go to another
newsgroup and find the question posted and ALREADY answered over
there. On top of that, if you cross-post your question, all of the
readers in all the newsgroups it is cross-posted to see both the
original question and all of the answers given to it. This is
beneficial to you because then we can add additional material to,
add clarification to, as well as add additional examples to an
answer you have received previously... that means you end up with
a more complete solution to your problem. This is a win-win
situation for all of us."
>
Rick
>
|
|
|
April 24th, 2007 11:55 PM
# 6
|
Re: Overflow? What gives?
I had lost all my newsgroups and wasn't sure which of the two VB
Quote:
newsgroups was the one I used to participate in months back. I tried to
find names I recognized (like yours) and didn't see any in the initial
list that came up when I clicked on the newsgroup name.
|
You should consider using Microsoft's public newsgroups; specifically, set
up an account for
msnews.microsoft.com
and select from among the newsgroups with 'vb' in their names. Most activity
seems to occur in this one...
microsoft.public.vb.general.discussion
Rick
|
|
April 25th, 2007 12:05 AM
# 7
|
Re: Overflow? What gives?
>I had lost all my newsgroups and wasn't sure which of the two VB
Quote:
Quote:
>newsgroups was the one I used to participate in months back. I tried to
>find names I recognized (like yours) and didn't see any in the initial
>list that came up when I clicked on the newsgroup name.
|
>
You should consider using Microsoft's public newsgroups; specifically, set
up an account for
>
msnews.microsoft.com
>
and select from among the newsgroups with 'vb' in their names. Most
activity seems to occur in this one...
>
microsoft.public.vb.general.discussion
|
But if you want to stay in the comp.lang newsgroups, I would suggest using
comp.lang.basic.visual.misc
which seems to get more activity than the two you selected.
Rick
|
|
April 27th, 2007 02:25 AM
# 8
|
Re: Overflow? What gives?
Thanks again Rick. Will do!
"Rick Rothstein (MVP - VB)" <rickNOSPAMnews@NOSPAMcomcast.netwrote in
message news:dIWdnZBNQqE8BbPbnZ2dnUVZ_jCdnZ2d@comcast.com. ..
Quote:
Quote:
>I had lost all my newsgroups and wasn't sure which of the two VB
>newsgroups was the one I used to participate in months back. I tried to
>find names I recognized (like yours) and didn't see any in the initial
>list that came up when I clicked on the newsgroup name.
|
>
You should consider using Microsoft's public newsgroups; specifically, set
up an account for
>
msnews.microsoft.com
>
and select from among the newsgroups with 'vb' in their names. Most
activity seems to occur in this one...
>
microsoft.public.vb.general.discussion
>
Rick
>
|
Not the answer you were looking for? Post your question . . .
189,817 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).
|