Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Casting error - why?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Anodes - 27 Mar 2008 15:51 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.