ShadowsFall88
ShadowsFall88

Reputation: 23

C# - calling a class with parameters in another class

Sorry for the noob question, I am teaching myself c#.

I want to be able to call a class into a form but it requires some form of parameter and I am not sure what it means.

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

    
}

This is the error message I am getting:

CS7036 There is no argument given that corresponds to the required formal parameter 'browserType' of 'TestLauncher.TestLauncher(string)' AtpSelenium C:\Coding\ATP\AtpSelenium2\AtpSelenium\AtpTester2.cs

I have tried adding browserType into the = new TestLauncher() section but it still gives an error.

Upvotes: 0

Views: 163

Answers (2)

srikarkowsika
srikarkowsika

Reputation: 39

TestLauncher class has a TestLauncher method which was defined with a parameter browserType variable is of type string. When this method is called again it would expect a string to passed in the method when its called. Ex: TestLauncher.TestLauncher(chrome); If you don't want this parameter to be passed everytime; method overloading could be setup for the method where its defined.

More resources about method overloading could found here:

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

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

In your case; you could have another constructor for Testlauncher method inside the class file for Testlauncher. Ex: public Testlauncher() { /* Have the same method as previous testlauncher method or modified method definition */ }

Upvotes: 1

Nikolay B.
Nikolay B.

Reputation: 19

Constructor of your class TestLauncher need argument "BrowserType"

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

}

or

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

}

Upvotes: 1

Related Questions