簡體   English   中英

C# - 在另一個類中使用參數調用一個類

[英]C# - calling a class with parameters in another class

對不起,菜鳥問題,我正在自學 C#。

我希望能夠將一個類調用到一個表單中,但它需要某種形式的參數,我不確定這意味着什么。

public partial class AtpTester2 : Form
{
    TestLauncher tLaunch = new TestLauncher();
    OpenFileDialog openFileDialog = new OpenFileDialog();
    string browser;

    public AtpTester2()
    {
        InitializeComponent();
    }

    private void UrlFilePickerBtn_Click(object sender, EventArgs e)
    {
        var fileContent = string.Empty;
        var filePath = string.Empty;

        openFileDialog.InitialDirectory = Application.StartupPath;
        openFileDialog.Filter = "txt files (*.txt)|*.txt|All Files (*.*)|*.*";
        openFileDialog.FilterIndex = 2;
        openFileDialog.RestoreDirectory = true;

        if(openFileDialog.ShowDialog() == DialogResult.OK)
        {
            filePath = openFileDialog.FileName;

            var fileStream = openFileDialog.OpenFile();
            StreamReader reader = new StreamReader(fileStream);
            fileContent = reader.ReadToEnd();
        }
        MessageBox.Show(fileContent, "URLs to test:", MessageBoxButtons.OK);
        tLaunch.OpenBrowser();
    }

    
}

這是我收到的錯誤消息:

CS7036 沒有給出對應於 'TestLauncher.TestLauncher(string)' AtpSelenium C:\\Coding\\ATP\\AtpSelenium2\\AtpSelenium\\AtpTester2.cs 的所需形式參數 'browserType' 的參數

我嘗試將 browserType 添加到 = new TestLauncher() 部分,但它仍然給出錯誤。

您的類 TestLauncher 的構造函數需要參數“BrowserType”

public partial class AtpTester2 : Form
{
   
   TestLauncher tLauncher = new TestLauncher(browserType.name);

}

或者

public partial class AtpTester2 : Form
{
   
   TestLauncher tLauncher = new TestLauncher(someString);

}

TestLauncher 類有一個 TestLauncher 方法,該方法使用參數 browserType 變量定義為字符串類型。 當再次調用此方法時,它會期望在調用該方法時傳入一個字符串。 例如:TestLauncher.TestLauncher(chrome); 如果你不想每次都傳遞這個參數; 可以為其定義的方法設置方法重載。

更多關於方法重載的資源可以在這里找到:

-https://www.c-sharpcorner.com/UploadFile/0c1bb2/method-oveloading-and-overriding-C-Sharp/

-https://www.geeksforgeeks.org/c-sharp-method-overloading/

在你的情況下; 您可以在 Testlauncher 的類文件中為 Testlauncher 方法設置另一個構造函數。 例如:public Testlauncher() { /* 與之前的 testlauncher 方法或修改的方法定義具有相同的方法 */ }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM