Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a Static method in C#

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

}

like image 609
mjcoder Avatar asked Oct 17 '11 10:10

mjcoder


People also ask

How do you call a static method?

To call a static method from another class, you use the name of the class followed by the method name, like this: ClassName. methodName().

How do you call a static method in a class?

Calling Static FunctionIt is invoked by using the class name. For example: Math. sqrt(a); //calling the square root function of the Math class.

Are there static methods in C?

Static functions in C are functions that are restricted to the same file in which they are defined. The functions in C are by default global. If we want to limit the scope of the function, we use the keyword static before the function.

Can you call a static method on an object?

Static class methods are defined on the class itself. You cannot call a static method on an object, only on an object class.


2 Answers

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!

like image 178
pan4321 Avatar answered Oct 04 '22 06:10

pan4321


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)
}
like image 23
Pete Houston Avatar answered Oct 04 '22 05:10

Pete Houston