【问题标题】:How can I move this function into a Class如何将此功能移动到类中
【发布时间】:2013-08-07 05:30:57
【问题描述】:
public string F03_veri_textbox(string veriadi3)
      {
          string Veritext;
          Veritext = webBrowser_sample.Document.GetElementById(veriadi3).GetAttribute("value");

          return Veritext;

      }

我在 Form1 中有一个 webBrowser_sample 对象。我使用此功能从特定网页收集数据。它工作正常。

我想从一个类中使用这个函数。

但是当我尝试移动它时,C# 会说“名称 'webBrowser_sample' 在当前上下文中不存在”。

如果我在 Class 中定义一个新的 webBrowser_sample,它将创建新的 webBrowser_sample 对象。

所以我不能使用它,因为我在浏览此浏览器时使用此功能收集数据。

【问题讨论】:

    标签: c# class function methods


    【解决方案1】:

    将“mytype”替换为 webBrowser_sample 的对象类型。您需要传入对该对象的引用,如下面的代码所示。另一种选择是使用扩展方法。

    public string F03_veri_textbox(string veriadi3, mytype browser)
      {
          string Veritext;
          Veritext = browser.Document.GetElementById(veriadi3).GetAttribute("value");
    
          return Veritext;
    
      }
    

    【讨论】:

      猜你喜欢
      • 2017-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-01
      • 2021-03-06
      • 1970-01-01
      • 2021-05-14
      • 1970-01-01
      相关资源
      最近更新 更多