Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用webclient获得的值迭代公共常量?_C#_Winforms_Webclient - Fatal编程技术网

C# 如何使用webclient获得的值迭代公共常量?

C# 如何使用webclient获得的值迭代公共常量?,c#,winforms,webclient,C#,Winforms,Webclient,我正在编写一些东西,我的指针在GitHub repo中,所以我使用client.DownloadString(“rawgithubEtc.com”);为了获取它们,我想用指针创建一个公共变量,因此请求只需执行一次,我尝试了以下方法: public partial class Form1 : Form { WebClient client = new WebClient(); public string stuff = client.DownloadString("raw

我正在编写一些东西,我的指针在GitHub repo中,所以我使用client.DownloadString(“rawgithubEtc.com”);为了获取它们,我想用指针创建一个公共变量,因此请求只需执行一次,我尝试了以下方法:

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();
    }
…但是Visual Studio说:

字段初始值设定项不能用于引用字段、方法或非静态属性

..在客户机中:

public string stuff = client.DownloadString("rawgithubetc.com");

不能在另一个字段的初始化表达式中使用一个字段。但是,您可以将初始化移到构造函数体中,一切都会正常工作

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();
    }

声明变量时需要提供变量名。您发布的代码还存在其他重大问题,例如公共字段、UI和非UI代码的不正确耦合等。但导致您似乎要问的问题的直接问题是,您只是没有在需要变量名的地方包含变量名。您缺少示例代码中的一些内容,例如变量名。我们怎么知道你在没有变量名的情况下试图在哪里使用下载?对不起,不要否决我,我已经修改了itAlso,当C#的英语讨论中正确的单词是“field”时,你使用的任何翻译工具都会吐出“camp”。对不起,现在修改了