By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,573 Members | 1,927 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,573 IT Pros & Developers. It's quick & easy.

This is weird...

P: n/a
Look at the code below. All three variables are initialized to the same
value, -9223372036854775808. The first two work OK but the third throws a
overflow error. Why? (Framework v1.0.3705)

'-- OK, no overflow
Dim i as Long = Long.MinValue
Dim k as Long = CLng(-2 ^ 63)

'-- Overflow error
Dim m as Long = -9223372036854775808
Nov 20 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a

"Brian" <no****@prairie.lakes.com> escreveu na mensagem
news:vo************@corp.supernews.com...
Look at the code below. All three variables are initialized to the same
value, -9223372036854775808. The first two work OK but the third throws a
overflow error. Why? (Framework v1.0.3705)

'-- OK, no overflow
Dim i as Long = Long.MinValue
Dim k as Long = CLng(-2 ^ 63)

'-- Overflow error
Dim m as Long = -9223372036854775808


You should add the literal type charecter 'L'.
Something like -9223372036854775808L
----
Rafael Pivato
Nov 20 '05 #2

P: n/a
Nope, already tried to append the L.
"Rafael Pivato" <rp*****@cpovo.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...

"Brian" <no****@prairie.lakes.com> escreveu na mensagem
news:vo************@corp.supernews.com...
Look at the code below. All three variables are initialized to the same
value, -9223372036854775808. The first two work OK but the third throws a overflow error. Why? (Framework v1.0.3705)

'-- OK, no overflow
Dim i as Long = Long.MinValue
Dim k as Long = CLng(-2 ^ 63)

'-- Overflow error
Dim m as Long = -9223372036854775808


You should add the literal type charecter 'L'.
Something like -9223372036854775808L
----
Rafael Pivato

Nov 20 '05 #3

P: n/a
The 1st example returns a Long that is well in range.
The 2nd example tries to convert the number to long since you explicitly
told it to do so using CLng(operation). The computer doesn't try figure
out exact value at compile time and because you told it to use CLng() to
change whatever number resulted into a long it assumes you know what you
are doing so it doesn't complain.
The 3nd example is obvious to the compiler the number you typed in will
never behave as the value as it is entered so it's an error.

Nov 20 '05 #4

P: n/a
"Rafael Pivato" <rp*****@cpovo.net> schrieb

"Brian" <no****@prairie.lakes.com> escreveu na mensagem
news:vo************@corp.supernews.com...
Look at the code below. All three variables are initialized to the
same value, -9223372036854775808. The first two work OK but the
third throws a overflow error. Why? (Framework v1.0.3705)

'-- OK, no overflow
Dim i as Long = Long.MinValue
Dim k as Long = CLng(-2 ^ 63)

'-- Overflow error
Dim m as Long = -9223372036854775808


You should add the literal type charecter 'L'.
Something like -9223372036854775808L


Also doesn't work here (VB 2003). Seems to be a code editor bug.
This works: m = &H8000000000000000
--
Armin

Nov 20 '05 #5

P: n/a
"Ting Liang (MSFT)" <tl****@online.microsoft.com> schrieb
Dim m as Long = -9223372036854775808

The 3nd example is obvious to the compiler the number you typed in
thirdond? ;-)
will never behave as the value as it is entered so it's an error.


Isn't it a Long literal within the valid range for Long literals?

Seem to be still the same code editor programmers like for VB6: Try this in
VB6:

Dim l As Long
l = -2147483648&

It also doesn't work.

*LOL*

--
Armin

Nov 20 '05 #6

P: n/a
hmmmmmm...

I think that the compiler misunderstood.

He is trying to make negative the absolute value, which is an overflow for
Long.

In this case you should use the hexadecimal literal 0x8000000000000000
(&H8000000000000000 in VB)

---

Rafael Pivato

"Brian" <no****@prairie.lakes.com> escreveu na mensagem
news:vo************@corp.supernews.com...
Nope, already tried to append the L.
"Rafael Pivato" <rp*****@cpovo.net> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...

"Brian" <no****@prairie.lakes.com> escreveu na mensagem
news:vo************@corp.supernews.com...
Look at the code below. All three variables are initialized to the same value, -9223372036854775808. The first two work OK but the third
throws
a overflow error. Why? (Framework v1.0.3705)

'-- OK, no overflow
Dim i as Long = Long.MinValue
Dim k as Long = CLng(-2 ^ 63)

'-- Overflow error
Dim m as Long = -9223372036854775808


You should add the literal type charecter 'L'.
Something like -9223372036854775808L
----
Rafael Pivato


Nov 20 '05 #7

P: n/a
tl****@online.microsoft.com (Ting Liang (MSFT)) scripsit:
The 3nd example is obvious to the compiler the number you typed in will
never behave as the value as it is entered so it's an error.


I don't understand that. -9223372036854775807 is in the range of the
Long datatype. When specifying it in hex format, it will work.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #8

P: n/a
Thanks for all of your input. I found out the cause of this problem. If you
want to check it out yourself go to:

http://support.microsoft.com/default...b;en-us;820657

"Armin Zingler" <az*******@freenet.de> wrote in message
news:e%***************@TK2MSFTNGP12.phx.gbl...
"Rafael Pivato" <rp*****@cpovo.net> schrieb

"Brian" <no****@prairie.lakes.com> escreveu na mensagem
news:vo************@corp.supernews.com...
Look at the code below. All three variables are initialized to the
same value, -9223372036854775808. The first two work OK but the
third throws a overflow error. Why? (Framework v1.0.3705)

'-- OK, no overflow
Dim i as Long = Long.MinValue
Dim k as Long = CLng(-2 ^ 63)

'-- Overflow error
Dim m as Long = -9223372036854775808


You should add the literal type charecter 'L'.
Something like -9223372036854775808L


Also doesn't work here (VB 2003). Seems to be a code editor bug.
This works: m = &H8000000000000000
--
Armin

Nov 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.