-2

Full code:

using System.Net
using System.Threading
namespace SoundBoard
{
    class Program
    {
        WebClient client;
        string fileName;
        static void Main(string[] args)
        {
          string url = "";
          Thread thread = new Thread(() =>
          {
              Uri uri = new Uri(url);
              fileName = System.IO.Path.GetFileName(uri.AbsolutePath); //error CS0120
              client.DownloadFileAsync(uri, Application.StartupPath + "/" + fileName); //error CS0120
          });
          thread.Start();
          }
    }
}

as I was following a tutorial, I encounter this error. I declared "client" and "fileName" outside the main method because I was gonna need them later in other methods outside the main method. I've looked at a couple posts, but atleast with client I don't seem to figure it out. I tried moving the declaration of the two variables inside of the main method, though this would not only mean that I couldn't use them outside of main, but also gave me a CS0165 "use of unassigned local variable "client".

    static void Main(string[] args)
    {
        WebClient client;
        string fileName;
        string url = "";
        Thread thread = new Thread(() =>
        {
            Uri uri = new Uri(url);
            fileName = System.IO.Path.GetFileName(uri.AbsolutePath); //solved
            client.DownloadFileAsync(uri, Application.StartupPath + "/" + fileName); //error CS0165
        });
        thread.Start();
    }

so, how do I solve this, preferrably keeping client and fileName outside of main?

1 Answers1

1

If you want to access properties and fields within a class without an instance of that class you will need to declare them as static.

class Program
    {
        static WebClient client;
        static string fileName;
        static void Main(string[] args)
        {
          string url = "";
          Thread thread = new Thread(() =>
          {
              Uri uri = new Uri(url);
              fileName = System.IO.Path.GetFileName(uri.AbsolutePath); //error CS0120
              client.DownloadFileAsync(uri, Application.StartupPath + "/" + fileName); //error CS0120
          });
          thread.Start();
          }
    }

That being said, you don't actually assign them any values either. You would need to instantiate the objects somewhere before trying to use them.

client = new WebClient();
fileName = "";
Ibrennan208
  • 1,345
  • 3
  • 14
  • 31
  • `client` variable is not initialized, so `client.DownloadFileAsync(uri, Application.StartupPath + "/" + fileName);` will not work. – kosist Jun 27 '22 at 21:26
  • Yes, but the real error is that they are trying to access a non-static variable in a static method. Even if they actually had a value, they would be getting the same error they mentioned in the question title. – Ibrennan208 Jun 27 '22 at 21:29
  • No, why would he get this error? One can use non-static variables in static classes. – kosist Jun 27 '22 at 21:30
  • @kosist *No, why would he get this error? One can use non-static variables in static classes.* Try actually doing this and tell me what you find out. – Ibrennan208 Jun 27 '22 at 21:35
  • Well, check this - https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient?view=net-6.0. Is `client` static? – kosist Jun 27 '22 at 21:35
  • 1
    You're missing where the variables are declared. WebClient isn't static **inside** of the static method, but if they declare it **outside**, such as at the class level, they would need it to be declared static. – Ibrennan208 Jun 27 '22 at 21:36
  • Based on provided sample code, they are declared in Main method, aren't they? – kosist Jun 27 '22 at 21:37
  • 1
    The OP literally states: *so, how do I solve this, preferrably keeping client and fileName outside of main?* – Ibrennan208 Jun 27 '22 at 21:38
  • @kosist Here is a github link to an example of a program which will not compile even though the variable has been declared and instantiated with a value: https://github.com/brennani/ExampleStaticError/blob/main/Program.cs Feel free to try it on a fiddle: https://dotnetfiddle.net/ or in your own environment – Ibrennan208 Jun 27 '22 at 21:44
  • When it is declared outside Main method - then yes, I agree of course. But I was referring to posted code, which had variables declared inside of Main method. Plus, your very initial answer (before edit) did not had initialized variable - thus it would also not work. You added that later. We are both right, but we were referring different context :) – kosist Jun 28 '22 at 06:29
  • 1
    The title of the post is *An object reference is required for the non-static field, method, or property 'Program.fileName' and 'Program.client'* and their posted code has the variables outside of the main method. They quite literally are asking how to have the variables outside of the `Main`. I gave them the answer to their question, you tried to give them an answer that they explicitly requested to avoid and then got upset when it was downvoted. It wasn't wrong that you point it out, it just makes it seem like you didn't read the whole post when you posted as an answer instead of a comment. – Ibrennan208 Jun 28 '22 at 06:38
  • Yep, you are right, I haven't read the whole post. Peace. – kosist Jun 28 '22 at 06:44