Search code examples
c#visual-studio-2008.net-2.0

Default parameter specifiers are not permitted in c# desktop application


private string formatSizeBinary(Int64 size, Int32 decimals = 2)
        {
            string[] sizes = { "Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
            double formattedSize = size;
            Int32 sizeIndex = 0;
            while (formattedSize >= 1024 & sizeIndex < sizes.Length)
            {
                formattedSize /= 1024;
                sizeIndex += 1;
            }
            return string.Format("{0} {1}", Math.Round(formattedSize, decimals).ToString(), sizes[sizeIndex]);
        }

I got this

"Default parameter specifiers are not permitted"

error on "Int32 decimals = 2"


Solution

  • Since your code looks fine to me but Optional parameters come with Visual Studio 2010 (and probably .NET 4.0 framework)

    Visual C# 2010 introduces named and optional arguments

    You need a method like;

    private string formatSizeBinary(Int64 size, Int32 decimals, int value)
            {
                decimals = value;
                string[] sizes = { "Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" };
                double formattedSize = size;
                Int32 sizeIndex = 0;
                while (formattedSize >= 1024 & sizeIndex < sizes.Length)
                {
                    formattedSize /= 1024;
                    sizeIndex += 1;
                }
                return string.Format("{0} {1}", Math.Round(formattedSize, decimals).ToString(), sizes[sizeIndex]);
            }
    

    Then you can call it which value you want;

    formatSizeBinary(yoursize, decimals, 2);
    formatSizeBinary(yoursize, decimals, 3);
    formatSizeBinary(yoursize, decimals, 4);