在 C# 中调用静态方法 [英] Calling a Static method in C#

查看:49
本文介绍了在 C# 中调用静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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...

老实说,我在这里很困惑,我是否需要实例化addresscity等?

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

}

推荐答案

你去

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

这篇关于在 C# 中调用静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆