How Do I Reference a Method from Another Class in C#?

So I reference the method " Driver " from one class to another,

Example of code:

class Program {

    public static void Run() {
        IWebDriver driver = new ChromeDriver;
    }

    public static void Do() {
        driver.FindElement(By.Name("search"));
    }
}

but within Do() driver does not exist in the current context

I want to do commands in regards to driver in separate classes, so that I can call them individually within main()

0

2 Answers

It's an issue of scope in your code. You would need to, at least in the code you've put forward, do something like the following.

class Program {

    public static IWebDriver driver;

    public static void Run() {
         driver = new ChromeDriver;
    }

    public static void Do() {
        driver.FindElement(By.Name("search"));
    }
}

Although in the phrasing of your question, it sounds like you're confusing Classes with Functions.

To share over functions, it needs to adhere to the scope of the Class or Namespace. To share values with other classes, you would have to pass in an instance of that class in variable format or something like that.

First, there is a problem in your class as answered by @jameswhyte and @alwayslearning.

You should know first how variables work within class for it to work well. For this time, i'll address your problem.

How to reference a method from another class

We will use your example.

First you have to declare your subclass as public to detect it from main class.
Second you have to declare your methods (static or non-static is upto you but in your case, you used a static method)

public class Program {
    public static IWebDriver driver; //this become a global variable 

    public static void Run() {
        driver = new ChromeDriver; //But this seems to initialize the driver 
                                   //rather than to run it
    }

    public static void Do() {
        driver.FindElement(By.Name("search"));
    }
}

I don't know what is your goal in here but it looks like it is better with object rather than static access. Anyway, here is how to access static method

public class MainClass
{
    //You can do this way directly since program has a static method
    Program.Run(); // this part initialized the static driver
    Program.Do(); // this part used the driver to findElement;
}

This is another way to access method from another class using instantiation
This is your non static method and variables public class Program { public IWebDriver driver; //this become a global variable

public class Program
{
    public IWebDriver driver;

    public void Run() {
        driver = new ChromeDriver; //But this seems to initialize the driver 
                                   //rather than to run it
    }

    public static void Do() {
        driver.FindElement(By.Name("search"));
    }
}

And in MainClass

public class MainClass
{
    //instantiate the other class
    Program myProgramClass = new Program(); // this is your reference to Program class

    //then use it this way
    myProgramClass.Run(); // this part initialized the driver
    myProgramClass.Do(); // this part used the driver to findElement;
}

Just note that your driver is not initialized so you need to call Run method first before Do or else you'll catch an exception about uninitialized variable

You can do some experiment on your own . Using constructor, instantiation, static method and variables, etc.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest