-5

System.IO.File.GetLength gives me the size of a file in bytes. What I'm attempting to do is take that size, in bytes, and convert it into the highest unit possible, without ever letting the size become less than 1. For example, instead of "2048" showing in the size column, I want "2 KB" to show. Here's the code:

 private string sizeDown(string size) { decimal sizeoffile = Convert.ToDecimal(size); int downed = 0; do { sizeoffile = sizeoffile / 1024; downed += 1; } while (sizeoffile > 1024); if (downed > 3) { return ""; } else { switch (downed) { case 0: return Convert.ToString(sizeoffile) + " bytes"; break; case 1: return Convert.ToString(sizeoffile) + " KB"; break; case 2: return Convert.ToString(sizeoffile) + " MB"; break; case 3: return Convert.ToString(sizeoffile) + " GB"; } } } 

However, as you probably see from the title, Visual Studio tells me that not all code paths return a value. I'm pretty confused. What value of size could result in my code not returning a value?

(By the way, I am aware that I take size as a string and then convert it into a decimal - I do that for convenience.)

5
  • what if downed is negative? Commented Jun 15, 2014 at 11:07
  • 1
    Remove the if(downed > 3) and add it as default to the switch statement. Commented Jun 15, 2014 at 11:07
  • else part deafult case is missing Commented Jun 15, 2014 at 11:08
  • You code will also not work with bytes. Remove the do keyword and replace it with the while. Or see my example. Commented Jun 15, 2014 at 11:21
  • A better duplicate: stackoverflow.com/q/2071345 Commented Jun 15, 2014 at 12:32

4 Answers 4

0

Change the below set of lines

if (downed > 3) { return ""; } else { switch (downed) { case 0: return Convert.ToString(sizeoffile) + " bytes"; break; case 1: return Convert.ToString(sizeoffile) + " KB"; break; case 2: return Convert.ToString(sizeoffile) + " MB"; break; case 3: return Convert.ToString(sizeoffile) + " GB"; } } 

to

 switch (downed) { case 0: return Convert.ToString(sizeoffile) + " bytes"; break; case 1: return Convert.ToString(sizeoffile) + " KB"; break; case 2: return Convert.ToString(sizeoffile) + " MB"; break; case 3: return Convert.ToString(sizeoffile) + " GB"; default : return ""; } } 
Sign up to request clarification or add additional context in comments.

4 Comments

You need to add break; to default case as well - msdn.microsoft.com/en-us/library/06tc147t.aspx
you dont really required break if its the last statement .programmers.stackexchange.com/questions/201777/…
@KamleshArya, Thanks for sharing the inside about break
You really don't need to use break anywhere in a switch that you are using a return statement since the return will effectively break out of your switch and the break will never actually get hit.
0

Use this code with Switch....Case

 private string sizeDown(string size) { decimal sizeoffile = Convert.ToDecimal(size); int downed = 0; do { sizeoffile = sizeoffile / 1024; downed += 1; } while (sizeoffile > 1024); switch (downed) { case 0: return Convert.ToString(sizeoffile) + " bytes"; break; case 1: return Convert.ToString(sizeoffile) + " KB"; break; case 2: return Convert.ToString(sizeoffile) + " MB"; break; case 3: return Convert.ToString(sizeoffile) + " GB"; break; default: return ""; break; } } 

2 Comments

Thanks! It removed the error. I was actually looking for something like "case else" also. Two birds with one stone, thanks!
"case else", it is already taken care in default case of switch... case statement
0

The reason for the error here is that the compiler has no way of knowing what value downed will be since it will be determined at runtime. You need to have a codepath for every value even if you know a runtime value will never hit it. As mentioned in other responses, you could add a default return value in your switch or you could throw an exception if it is a condition you don't expect to ever get hit.

switch (downed) { case 0: return Convert.ToString(sizeoffile) + " bytes"; break; case 1: return Convert.ToString(sizeoffile) + " KB"; break; case 2: return Convert.ToString(sizeoffile) + " MB"; break; case 3: return Convert.ToString(sizeoffile) + " GB"; default: throw new InvalidOperationException(); } 

Comments

0

First of all: Your switch-case will return nothing when downed > 3. That is what causes the error. Second, your solution will fail for bytes. Remove the do keyword and replace it with the while statement.

To create a totally different answer compared to the rest:

private string sizeDown(string size) { long sizeOfFile = long.Parse(size); if (sizeOfFile == 1) return sizeOfFile + " byte"; if (sizeOfFile < 1024) return sizeOfFile + " bytes"; if (sizeOfFile < 1024 * 1024) return sizeOfFile / 1024 + " KB"; if (sizeOfFile < 1024 * 1024 * 1024) return sizeOfFile / (1024 * 1024) + " MB"; if (sizeOfFile < 1024 * 1024 * 1024 * 1024) return sizeOfFile / (1024 * 1024 * 1024) + " GB"; return sizeOfFile / (1024 * 1024 * 1024 * 1024) + " TB"; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.