-3

I'm coding some stuff and my pointers are inside a GitHub repo so I use the client.DownloadString("rawgithubEtc.com"); to get them, but I want to make a PUBLIC variable with the pointer, so the request will be done just ONE TIME, I tried this:

public partial class Form1 : Form
{
    WebClient client = new WebClient();
    public string stuff = client.DownloadString("rawgithubetc.com");
    public int speedTimer = 0; public string speed_data;
    Overlay frm = new Overlay();
    public Mem m = new Mem();
    public Form1()
    {
        InitializeComponent();
    }

..but Visual Studio says:

a field initializer can't be used to reference the field, method or non static properties.

..in the .client here:

public string stuff = client.DownloadString("rawgithubetc.com");
  • You need to provide a variable _name_ when declaring a variable. There are other huge issues with the code you posted, such as public fields, improper coupling of UI and non-UI code, etc. But the immediate problem causing the problem you seem to be asking about is that you simply did not include a variable name where one is required. – Peter Duniho May 07 '21 at 22:30
  • You're missing some stuff from your example code, like variable names. How can we figure out where you're trying to use the download without a variable name? – Ben Voigt May 07 '21 at 22:30
  • sorry, dont downvote me, I have modfied it – José Antonio May 07 '21 at 22:31
  • Also, whatever translation tool you used spit out "camp" when the correct word in English discussions of C# is "field". – Ben Voigt May 07 '21 at 22:31
  • sorry, modified now – José Antonio May 07 '21 at 22:32
  • See also https://stackoverflow.com/questions/27725571/why-c-sharp-wont-allow-field-initializer-with-non-static-fields – Peter Duniho May 07 '21 at 22:34

1 Answers1

0

You can't use one field inside the initialization expression of another field. But you can move the initialization to the constructor body and things will work just fine.

public partial class Form1 : Form
{
    WebClient client = new WebClient();
    public string stuff; /* not allowed to use "client" here */
    public Form1()
    {
        /* using "client" here is perfectly fine */
        stuff = client.DownloadString("rawgithubetc.com");
        InitializeComponent();
    }
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720