Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.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#全局类-从一个方法添加,从另一个方法读取_C#_.net_Parallel Processing - Fatal编程技术网

C#全局类-从一个方法添加,从另一个方法读取

C#全局类-从一个方法添加,从另一个方法读取,c#,.net,parallel-processing,C#,.net,Parallel Processing,如何将下面的DoScrape方法添加到外部CompanyInfo变量中,然后在按钮1的循环处单击访问该列表以写入文本文件 namespace Test1 { public partial class Form1 : Form { public class LocationNameAndLink { public string Link { get; set; } public string Name { get; set; } } Lo

如何将下面的DoScrape方法添加到外部CompanyInfo变量中,然后在按钮1的循环处单击访问该列表以写入文本文件

namespace Test1
{
public partial class Form1 : Form
{

    public class LocationNameAndLink
    {
        public string Link { get; set; }
        public string Name { get; set; }
    }

LocationNameAndLink CompanyInfo = new List<LocationNameAndLink>();

private void button1_Click(object sender, EventArgs e)
{
        Parallel.ForEach(Brands, item =>
        {
            string initialSiteName = "http://www.site1.com/" + Brands;
            DoScrape(initialSiteName);
        });


        StreamWriter sw03 = new StreamWriter("websiteCompanyDetails.txt");
        foreach (var item in CompanyInfo)
        {
            sw03.WriteLine("\"" +  item.Name + "\",\"" + item.Link + "\""    );
        }
}

public static void DoScrape(string PageLink)
{
    string SiteLink;
    string SiteName;
    SourceCode = WorkerClass.getSourceCode(nextPageLink);
    SiteLink = SourceCode.Substring(0, 50);
    SiteName = SourceCode.Substring(51, 50);
     CompanyInfo.Add(new LocationNameAndLink 
                     {
                         Name = SiteName,
                         Link = SiteLink 
                     });
                 }
  }
namespace Test1
{
公共部分类Form1:Form
{
公共类位置名称和链接
{
公共字符串链接{get;set;}
公共字符串名称{get;set;}
}
LocationNameAndLink CompanyInfo=新列表();
私有无效按钮1\u单击(对象发送者,事件参数e)
{
Parallel.ForEach(品牌,项目=>
{
字符串initialSiteName=”http://www.site1.com/“+品牌;
DoScrape(初始站点名称);
});
StreamWriter sw03=新的StreamWriter(“websiteCompanyDetails.txt”);
foreach(公司信息中的var项目)
{
sw03.WriteLine(“\”“+item.Name+”,“\”“+item.Link+”);
}
}
公共静态void DoScrape(字符串页面链接)
{
字符串站点链接;
字符串SiteName;
SourceCode=WorkerClass.getSourceCode(nextPageLink);
SiteLink=SourceCode.Substring(0,50);
SiteName=SourceCode.Substring(51,50);
公司信息添加(新位置名称和链接
{
名称=站点名称,
Link=SiteLink
});
}
}

您需要使
DoScrape
成为一个实例方法,而不是静态方法

静态方法不特定于该类型的任何特定实例,因此它没有隐式的
引用-而您的
CompanyInfo
字段是一个实例变量-Form1的每个实例都有一个单独的变量


有关
static
含义的更多信息,请参阅。这是一个需要理解的重要概念。

resharper再次攻击?@paul-resharper与此有什么关系?抱歉,不得不隐藏原始代码,但DoScrape是一个递归方法。这会改变答案吗?@SaidurRahman:不,一点也不会。但不要在不理解的情况下修复它结束它-确保你真的,真的知道
静态的
是什么意思。@AppDeveloper当建议方法应该是静态的时候,它可能过于热心了