-1

In form1 i added two classes

public class FileSizeFormatProvider : IFormatProvider, ICustomFormatter
{
    public object GetFormat(Type formatType)
    {
        if (formatType == typeof(ICustomFormatter)) return this;
        return null;
    }

    private const string fileSizeFormat = "fs";
    private const Decimal OneKiloByte = 1024M;
    private const Decimal OneMegaByte = OneKiloByte * 1024M;
    private const Decimal OneGigaByte = OneMegaByte * 1024M;

    public string Format(string format, object arg, IFormatProvider formatProvider)
    {
        if (format == null || !format.StartsWith(fileSizeFormat))
        {
            return defaultFormat(format, arg, formatProvider);
        }

        if (arg is string)
        {
            return defaultFormat(format, arg, formatProvider);
        }

        Decimal size;

        try
        {
            size = Convert.ToDecimal(arg);
        }
        catch (InvalidCastException)
        {
            return defaultFormat(format, arg, formatProvider);
        }

        string suffix;
        if (size > OneGigaByte)
        {
            size /= OneGigaByte;
            suffix = "GB";
        }
        else if (size > OneMegaByte)
        {
            size /= OneMegaByte;
            suffix = "MB";
        }
        else if (size > OneKiloByte)
        {
            size /= OneKiloByte;
            suffix = "kB";
        }
        else
        {
            suffix = " B";
        }

        string precision = format.Substring(2);
        if (String.IsNullOrEmpty(precision)) precision = "2";
        return String.Format("{0:N" + precision + "}{1}", size, suffix);

    }

    private static string defaultFormat(string format, object arg, IFormatProvider formatProvider)
    {
        IFormattable formattableArg = arg as IFormattable;
        if (formattableArg != null)
        {
            return formattableArg.ToString(format, formatProvider);
        }
        return arg.ToString();
    }

}

public static class ExtensionMethods
{
    public static string ToFileSize(this long l)
    {
        return String.Format(new FileSizeFormatProvider(), "{0:fs}", l);
    }
}

And then i'm using it like this:

FileInfo fi = new FileInfo(ListViewCostumControl.lvnf.Items[ListViewCostumControl.lvnf.SelectedIndices[0]].Text);
label17.Text = ExtensionMethods.ToFileSize(fi.Length);

But getting error on the ExtensionMethods class on the ToFilesize:

Error 1 Extension method must be defined in a top level static class; ExtensionMethods is a nested class

nozzleman
  • 9,529
  • 4
  • 37
  • 58
מני מנחם
  • 235
  • 3
  • 12
  • See this answer: http://stackoverflow.com/a/13071326/170138 – Pelle Jun 22 '16 at 09:05
  • 1
    Your posted code here and the error message don't match. Can you show us the entire CS file the code resides in? – Patrick Hofman Jun 22 '16 at 09:07
  • 1
    @מני מנחם is there a class "above" `FileSizeFormatProvider`? – nozzleman Jun 22 '16 at 09:11
  • @nozzleman yes there is but it's not directly above the FileSizeFormatProvider class there some methods first before. – מני מנחם Jun 22 '16 at 09:13
  • that doesn't matter, ill update my answer – nozzleman Jun 22 '16 at 09:13
  • I tried to move the extensionmethods class to the top of form1 before i declare vars same error and then i tried to move it before the: public partial class Form1 : Form but then i'm getting another error: Error 1 The type or namespace name 'FileSizeFormatProvider' could not be found (are you missing a using directive or an assembly reference?) – מני מנחם Jun 22 '16 at 09:15
  • try using `new SomeTopClass.FileSizeFormatProvider()` instead of `new FileSizeFormatProvider()`in the return statement of your extension method, as seen in my answer – nozzleman Jun 22 '16 at 09:20
  • By the way, `ExtensionMethods.ToFileSize(fi.Length)` does not use the "extension method syntax", that would be: `label17.Text = fi.Length.ToFileSize()` – Hans Kesting Jun 22 '16 at 11:27
  • @HansKesting Tried it too label17.Text = fi.Length.ToFileSize() but ToFilesSize() not exist from the fi.Length i guess the error make it not to be exist. – מני מנחם Jun 22 '16 at 12:53
  • 1
    You will need to define that extension method correctly (in a top-level static class). Or remove that "this" keyword to use it in your current way as a regular helper method. – Hans Kesting Jun 22 '16 at 13:15

1 Answers1

1

The C# language doesn't allow you to define extension methods from within nested classes, see Why are extension methods only allowed in non-nested, non-generic static class?

Based on the discussion in the comments, then problem should be solved by the following approach:

Structure causing this Error

public class SomeTopClass
{

    // ...

    public class FileSizeFormatProvider : IFormatProvider, ICustomFormatter
    {
        // ...
    }

    // class is in SomeClass, therefore it is a nested class. You cannot define extension mathods here
    public static class ExtensionMethods
    {
        public static string ToFileSize(this long l)
        {
            return String.Format(new FileSizeFormatProvider(), "{0:fs}", l);
        }
    }
}

Structure Preventing this Error

public class SomeTopClass
{

    // ...

    public class FileSizeFormatProvider : IFormatProvider, ICustomFormatter
    {
        // ...
    }           
}


// now you can, because it is not nested inside some other class
public static class ExtensionMethods
{
    public static string ToFileSize(this long l)
    {
        // note the SomeTopClass before FileSizeFormatProvider!!
        return String.Format(new SomeTopClass.FileSizeFormatProvider(), "{0:fs}", l);
    }
}
Community
  • 1
  • 1
nozzleman
  • 9,529
  • 4
  • 37
  • 58