Casting error - why?

A

Anodes

I have a class property 'FileSize_MB_InitialFile', declared as Int16.
When I set it like this it works:
this.FileSize_MB_InitialFile = Convert.ToInt16(fileInfo.Length);


But when I set it like this (since I want MB, not bytes), it throws an
error:
this.FileSize_MB_InitialFile = Convert.ToInt16(fileInfo.Length) /
1000;


The error is "Cannot implicitly convert type 'int' to 'short'. An
explicit conversion exists (are you missing a cast?)"

Placing the division operation inside the parentheses works though:
this.FileSize_MB_InitialFile = Convert.ToInt16(fileInfo.Length /
1000);


I'm curious why it generates this error in the second instance.
 
D

David R. Longnecker

The 'short' keyword is the same as Int16 Type in .net.

Better said, you're trying to take the result of your conversion (an int16)
and divide it (which, turns it into an int32 because any plainly typed number
is an int32), then place it back into an int16 without converting the result
to int16--returning a casting error.

Placing the division inside the convert statement works because you're taking
the result of the division (an int32) then explicitly converting it to an
int16 (though there's the potential to lose data).

That should work, though I have to ask--why an int16? Are you sure there
will never be an overflow--especially on file sizes? Int16s only range from
+/-32768, so be wary.

Here's a quick example:

FileInfo info = new FileInfo(@"C:\WindowsServer2003-KB340178-SP2-x86-ENU.msi");

Console.WriteLine(Convert.ToInt16((info.Length / 1024) / 1024)
+ " MB");
Console.WriteLine(Convert.ToInt16(info.Length / 1024) + " KB");
Console.WriteLine(info.Length + " Bytes");

HTH.

-dl
 
A

Anodes

The 'short' keyword is the same as Int16 Type in .net.

Better said, you're trying to take the result of your conversion (an int16)
and divide it (which, turns it into an int32 because any plainly typed number
is an int32), then place it back into an int16 without converting the result
to int16--returning a casting error.

Placing the division inside the convert statement works because you're taking
the result of the division (an int32) then explicitly converting it to an
int16 (though there's the potential to lose data).

That should work, though I have to ask--why an int16? Are you sure there
will never be an overflow--especially on file sizes? Int16s only range from
+/-32768, so be wary.

Here's a quick example:

FileInfo info = new FileInfo(@"C:\WindowsServer2003-KB340178-SP2-x86-ENU.msi");

Console.WriteLine(Convert.ToInt16((info.Length / 1024) / 1024)
+ " MB");
Console.WriteLine(Convert.ToInt16(info.Length / 1024) + " KB");
Console.WriteLine(info.Length + " Bytes");

HTH.

-dl

Thanks for the example. That makes perfect sense regarding numeric
operations with literals defaulting to Int32.

As to your question, I want the property units to be in megabytes
(rounded to the nearest hundredth). So if the file's a gigabyte my
property's value would be 1000.00 (or 1024.00)...I won't be working
with any 32GB+ files, so Int16 will be more than adequate.
 
H

Hans Kesting

Anodes has brought this to us :
this.FileSize_MB_InitialFile = Convert.ToInt16(fileInfo.Length /
1000);

Note: FileInfo.Length returns the number of *bytes*, so you need to
divide it by 1.000.000 (or rather 1024 * 1024) to get the number of
MegaBytes.

Hans Kesting
 
A

Anodes

Anodes has brought this to us :


Note: FileInfo.Length returns the number of *bytes*, so you need to
divide it by 1.000.000 (or rather 1024 * 1024) to get the number of
MegaBytes.

Hans Kesting

Of course! Thanks for the correction. I would've figured it out in due
course during testing, once all my return values were orders of
magnitude higher.
 

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

Similar Threads


Top