在 C# 中调用静态方法

发布于 2024-12-10 14:08:16 字数 1091 浏览 1 评论 0原文

如何调用静态方法?我想从我创建的类中调用它,我想从 IP 获取位置。我已经声明了它,但我需要做的是调用该方法...作为 static...

老实说,我在这里很困惑,我需要实例化 地址城市等?

到目前为止我已经这样做了;

LocationTools.cs

public static class LocationTools
    {
        public static void GetLocationFromIP(string address, out string city, out string region, out string country, out double? latitude, out double? longitude)
        {

Home.cs

   public string IPAPIKey
    {
       get
        {
            return WebConfigurationManager.AppSettings["IPAPIKey"];
        }
    }

    ////To get the ip address of the machine and not the proxy use the following code
    static void GetLocationFromIP()
    {
        string strIPAddress = Request.UserHostAddress.ToString();
        strIPAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

        if (strIPAddress == null || strIPAddress == "")
        {
            strIPAddress = Request.ServerVariables["REMOTE_ADDR"].ToString();
        }
    }
}

}

How do I call a static method? I want to call this from a class I have created, I want to get the location from IP. I've declared it but what I need to do is call the method... as static...

To be honest with you, I'm quite confused here, do I need to instantiate address, city, etc.?

I have done this so far;

LocationTools.cs

public static class LocationTools
    {
        public static void GetLocationFromIP(string address, out string city, out string region, out string country, out double? latitude, out double? longitude)
        {

Home.cs

   public string IPAPIKey
    {
       get
        {
            return WebConfigurationManager.AppSettings["IPAPIKey"];
        }
    }

    ////To get the ip address of the machine and not the proxy use the following code
    static void GetLocationFromIP()
    {
        string strIPAddress = Request.UserHostAddress.ToString();
        strIPAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

        if (strIPAddress == null || strIPAddress == "")
        {
            strIPAddress = Request.ServerVariables["REMOTE_ADDR"].ToString();
        }
    }
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

素罗衫 2024-12-17 14:08:16

静态类通常在您想要提供一些实用程序时使用,因此您不必创建这些类的对象。您可以通过简单地按类名调用并调用成员函数来从其他类调用这些方法。

例如,在这里您可以调用 LocationTools.GetLocationFromIP();

希望有帮助!

Static classes are generally used when you want to provide some utilities, so you do not have to create objects of those classes. You can call those methods from other classes by simply calling by class name and invoking the member function.

For example here you can call as LocationTools.GetLocationFromIP();

Hope it helps!

债姬 2024-12-17 14:08:16

就这样吧

static void GetLocationFromIP()
{
    string strIPAddress = Request.UserHostAddress.ToString();
    strIPAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

    if (strIPAddress == null || strIPAddress == "")
    {
        strIPAddress = Request.ServerVariables["REMOTE_ADDR"].ToString();
    }

    string city = string.Empty;
    string region = string.Empty;
    string country = string.Empty;
    double latitude = -1.00;
    double longitude = -1.00;

    LocationTools.GetLocationFromIP(strIPAddress, out city, out region, out country, out latitude, out longitude)
}

There you go

static void GetLocationFromIP()
{
    string strIPAddress = Request.UserHostAddress.ToString();
    strIPAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

    if (strIPAddress == null || strIPAddress == "")
    {
        strIPAddress = Request.ServerVariables["REMOTE_ADDR"].ToString();
    }

    string city = string.Empty;
    string region = string.Empty;
    string country = string.Empty;
    double latitude = -1.00;
    double longitude = -1.00;

    LocationTools.GetLocationFromIP(strIPAddress, out city, out region, out country, out latitude, out longitude)
}
童话里做英雄 2024-12-17 14:08:16
LocationTools.GetLocationFromIP( ... ) ;

您应该在 MSDN 上阅读有关静态类和成员的信息

静态类和类成员用于创建无需创建类实例即可访问的数据和函数。静态类成员可用于分离独立于任何对象标识的数据和行为:无论对象发生什么,数据和函数都不会改变。当类中没有依赖于对象标识的数据或行为时,可以使用静态类。

LocationTools.GetLocationFromIP( ... ) ;

You should read up about Static Classes and Members on MSDN

Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class. Static class members can be used to separate data and behavior that is independent of any object identity: the data and functions do not change regardless of what happens to the object. Static classes can be used when there is no data or behavior in the class that depends on object identity.

一向肩并 2024-12-17 14:08:16

您需要做两件事:

  1. 首先,导入静态类所在的库:
    import blabla;

  2. 然后,调用静态方法执行如下操作:
    LocationTools.GetLocationFromIP(address, city...);

它应该有效。

You need to do two things:

  1. First, import the library where the static class is:
    import blabla;

  2. Then, call your static method doing something liked this:
    LocationTools.GetLocationFromIP(address, city...);

It should work.

夜未央樱花落 2024-12-17 14:08:16

它就像这样简单:

LocationTools.GetLocationFromIP(strIP, strCity, strRegion, strCountry, fLat, fLong)

只需调用类,然后直接调用方法。静态意味着您不需要类的实例来调用该方法。

It's as easy as:

LocationTools.GetLocationFromIP(strIP, strCity, strRegion, strCountry, fLat, fLong)

Just call the Class, and straight from that the method. Static means that you do not need an instance of the class to call the method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文