-4

I have the following problem. I've created a variable and when I use it in code it gives me an error: Severity Code Description Project File Line Suppression State Error CS0120 An object reference is required for the non-static field, method, or property 'Form1.assemblyPath' Form Change Wallpaper

My code

public partial claass Form1: Form
{
   private string assemblyPath = Path.GetFullPath(Assembly.GetExecutingAssembly().Location);

   private static string[] GetAllWallpapers(string daytime)
   {
      string dataPath = assemblyPath + @"..\..\..\..\data"; // Error line
   }
}

The rest of the code has no problem

Martin
  • 43
  • 5

1 Answers1

3

assemblyPath is an instance member - it belongs to a specific instance of Form1. On the other hand, GetAllWallpapers is a static method - it belongs to the class as a whole, not to any specific instance, and thus can't access any member of a specific instance.

To solve this issue, you can either make assemblyPath static or remove the static modifier from GetAllWallpapers. From the context, assemblyPath doesn't seem to contain any information that's specific to an instance, and it can't be modified by any instance, so it makes sense to make it static and call it a day.

Mureinik
  • 297,002
  • 52
  • 306
  • 350