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.
David R. Longnecker - 27 Mar 2008 16:27 GMT
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
--
David R. Longnecker
http://blog.tiredstudent.com
> I have a class property 'FileSize_MB_InitialFile', declared as Int16.
> When I set it like this it works:
[quoted text clipped - 11 lines]
>
> I'm curious why it generates this error in the second instance.
Anodes - 27 Mar 2008 16:48 GMT
On Mar 27, 11:27 am, David R. Longnecker
<dlongnec...@community.nospam> wrote:
> The 'short' keyword is the same as Int16 Type in .net.
>
[quoted text clipped - 42 lines]
>
> > I'm curious why it generates this error in the second instance.
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.
Peter Duniho - 27 Mar 2008 16:33 GMT
> [...]
> The error is "Cannot implicitly convert type 'int' to 'short'. An
[quoted text clipped - 5 lines]
>
> I'm curious why it generates this error in the second instance.
Because dividing by 1000 causes the expression to be an int, not a short.
Then you try to assign the whole thing to a short, which can't be done
implicitly (as the error says).
I personally would just use a single cast on the division expression and
forget the Convert class altogether in this case. But the solution you
found is fine too.
Pete
Hans Kesting - 27 Mar 2008 16:50 GMT
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
Anodes - 27 Mar 2008 17:12 GMT
On Mar 27, 11:50 am, Hans Kesting <invalid.han...@spamgourmet.com>
wrote:
> Anodes has brought this to us :
>
[quoted text clipped - 6 lines]
>
> 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.