question

StevenYoung-5410 avatar image
0 Votes"
StevenYoung-5410 asked StevenYoung-5410 commented

Convert GB to Byte

Dim b As Long = 5 1024 1024 * 1024

Why it's not right?

dotnet-visual-basic
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

The code you posted is not compilable :

 Dim b As Long = 5 1024 1024 * 1024



0 Votes 0 ·

Yes, why can't it compile?

0 Votes 0 ·
     Dim a As Long = 5368709120 'No problem
     Dim b Long = 5 * 1024 * 1024 * 1024  'It's not compile

(5*1024*1024*1024 = 5368709120)

0 Votes 0 ·

Thank you, could you please tell me why?

0 Votes 0 ·

5 and 1024 are interpreted as Integer. The “*” operation for such values is also limited to Integer by VB, but the result is larger than the maximum value of Integer, therefore it cannot be computed. If you add “L” to any term, the value and the result becomes a Long, which is enough for 5GB. The “L” suffix is not required in case of 5368709120 because it is clear that this is a large, Long value.


0 Votes 0 ·

1 Answer

Viorel-1 avatar image
0 Votes"
Viorel-1 answered StevenYoung-5410 commented

Try this:

 Dim b As Long = 5 * 1024L * 1024 * 1024
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you so much.

0 Votes 0 ·