简体   繁体   中英

C# How do I call a method from another class inside of my interface without making the method static?

I am trying to ping google to check to see if I am connected to the internet, and I want to implement this in one of my interfaces, but I am unsure how to do this as my method is not static. Any help or insight would be much appreciated. The concepts of interfaces are new to me, so any resources that I could use to understand them better would also be very helpful! checkConnection() was where I was looking to start, maybe creating a constructor to instantiate the method? but I really am unsure how to implement this. This is what I have so far:

public class ApplicationManager : IApplicationManager
{

    public void ManagerRun()
    {

    if (InternetConnectionCheck.CheckForInternetConnection() == true)
    {

    }
    else if(InternetConnectionCheck.CheckForInternetConnection() == false)
    {

    }

public interface IApplicationManager
{
    void ManagerRun();
    bool checkConnection();
}

}

I cut out most of my code as it isn't relevant to the problem, but the issue is that "InternetConnectionCheck.CheckForInternetConnection() used to be a static method but it is no longer a static method, as I want to implement it in the interface so that I can call it once, and then just reference it throughout the rest of my code, whenever I want to make an internet connection check. This is the code for the check:

class InternetConnectionCheck
{
    public bool CheckForInternetConnection()
    {
        try
        {
            using (var client = new WebClient())
            {
                using (var stream = client.OpenRead("http://www.google.com"))
                {
                    return true;
                }
            }
        }
        catch
        {
            return false;
        }
    }

}

}

I want to implement this in one of my interfaces

Interfaces only allow you to declare what properties, methods, and events an implementer must have. An interface method cannot have a body. In your example, ApplicationManager needs to implement the checkConnection() method required by the IApplicationManager interface.

To call a method on one class from another, it must either be static or you must instantiate an instance of that class.

Generally speaking, if a method does not refer to any instance members of its class, it should be static (there's not much point of it being non-static).

A few other tips based on your code:

  1. Your interface should probably be in its own file, rather than be a child of ApplicationManager .
  2. checkConnection should be CheckConnection to conform with C# norms.
  3. You can omit == true in a conditional and use ! instead of == false , as in if (CheckForInternetConnection()) and if (!CheckForInternetConnection()) . Better yet, just use an else instead of testing for the false condition. (As @Servy pointed out, you're making two separate calls in your example, which is not what you want).

The method uses no instance state, so the method is conceptually static .

It doesn't make sense as being an instance method. Making it an instance method would be implying that the method uses some state of the object, and requires anyone wishing to perform the method to have an instance of the type on hand in order to perform the operation, even though you don't actually plan to use that object for anything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM