Simple math overflow problem (and var declared as int64 in IDE shows as Int)

S

Simon Devlin

Hi folks,

I've been bashing my head against this all afternoon and am now totally
baffled.

Given this (a simple routine to turn a ip address string into an decimal)

<snip>
Dim Parts(3) as string ' --> each element is an integer in the range 0-255
Dim Big as int64 = 0

Big =16777216 * cint(Parts(0))
Big = big + (65536 * cint(Parts(1)))
big = big + (256 * cint(parts(2)))
big = big + cint(parts(3))

I get arithmetic overflow errors on execution (at the first line).
Interestingly if I play around with constants then by doing

big = 16777216 * 255

The IDE reports "Constant expression not representable in type integer".
This doesn't make a whole lot of sense given that big is declared as an
int64. Values greater than 127 result in the same - clearly suggesting an
overflow somewhere, but these numbers are well within the scope of an int64
(ordinarilly a uint32 is sufficient)

Does anyone have any suggestions to stop me going mad. I'm not that
experienced VB.NET / ASP.NET, so hopefully I'm missing something really
obvious.

Thanks
 
N

Nick Savoiu

Kevin,

I'm not an expert either but I don't see any 'int64' type for Visual Basic.
Looks like you're just getting an 'Integer' which will overflow after 2**31.
Have you tried "Long"?

Nick
 
K

Kevin Spencer

Use CLng() instead of CInt().

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
K

Kevin Spencer

int64 is a .Net Data Type, available via C# OR VB.Net, or any other .Net
language.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
S

Simon Devlin

Thanks. That's done the trick, but I don't really understand why. Surely
the parenethesis should mean that the cint conversion should occur first
(and I know that the target of that operation is always a 8 bit value).

I'm also not clear why the constant expression in the IDE errors too.

I'm happy that this has resolved my problem, but it all seems a little odd.

Regards
 
M

mikeb

Simon said:
Thanks. That's done the trick, but I don't really understand why. Surely
the parenethesis should mean that the cint conversion should occur first
(and I know that the target of that operation is always a 8 bit value).

I'm also not clear why the constant expression in the IDE errors too.

I'm happy that this has resolved my problem, but it all seems a little odd.

Regards

The problem is that the expression:

16777216 * cint(Parts(0))

is an Int32 expression, since both of it's components are Int32's. That
expression overflows. The type of the destination is not taken into
consideration at the point of evaluating the expression - that's done
when the assignment takes place (after the expression is evaluated).

When you use CLng() instead of CInt(), you change the expression's type
to Int64, since one of its components is an Int64.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top