0

I.m trying to setup and build https://resources.samsungdevelopers.com/Gear_VR/030_Create_VR_App_or_Game_Using_a_Game_Engine/Exercise_2%3A_Create_a_Splash_Screen/Step_7%3A_Build_and_Run_the_Application

When I try and build the application it throws the following error: Assets/Workshop/Scripts/TextureLoader.cs(114,31): error CS1105: `TextureLoader.GetFilesWithExtensions(this DirectoryInfo, params string[])': Extension methods must be declared static

// Gets the list of files in the directory by extention and filters for our specified ones (images)
public IEnumerable<FileInfo> GetFilesWithExtensions (this DirectoryInfo dir, params string[] extensions) {
    IEnumerable<FileInfo> files = dir.GetFiles();
    return files.Where(f => extensions.Contains(f.Extension));
}

public bool isFinishedLoadingFirstTexture() {
    return mFinishedFirstTexture;
}

I have already changed it to a public static class in the header.

Greg Lukosek
  • 1,774
  • 20
  • 40
Det
  • 11
  • 4

3 Answers3

0

The class being static doesn't do much for the methods. You need to make your method static for the extensions to work!

public static IEnumerable<FileInfo> GetFilesWithExtensions (this DirectoryInfo dir, params string[] extensions) {
    IEnumerable<FileInfo> files = dir.GetFiles();
    return files.Where(f => extensions.Contains(f.Extension));
}

I wrote an extension method here if you need another example!

Community
  • 1
  • 1
Fredrik Schön
  • 4,888
  • 1
  • 21
  • 32
0

I order to make an extension method, the class must be public and static.

The function must be public and static too. You missed this part.

public static class TextureLoader
{
    public static IEnumerable<FileInfo> GetFilesWithExtensions(this DirectoryInfo dir, params string[] extensions)
    {
        IEnumerable<FileInfo> files = dir.GetFiles();
        return files.Where(f => extensions.Contains(f.Extension));
    }
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • when I change it to a static class it through a whole host of errors. This was file that came with the example project. – Det Feb 23 '17 at 13:11
  • it was originally `private IEnumerable GetFilesWithExtensions (this DirectoryInfo dir, params string[] extensions) { IEnumerable files = dir.GetFiles(); return files.Where(f => extensions.Contains(f.Extension)); }` – Det Feb 23 '17 at 13:19
  • Sorry, extension method is supposed to be public if you are calling it from class other the class it resides in. I can't help without access to the project since it looks like there are other problems in your code. – Programmer Feb 23 '17 at 17:50
0

Modify your code this way, the error will disappear

private IEnumerable<FileInfo> GetFilesWithExtensions(DirectoryInfo dir, string[] extensions)
{
    IEnumerable<FileInfo> files = dir.GetFiles();
    return files.Where(f => extensions.Contains(f.Extension));
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253