Calling a static method in C #

How to call a static method? I want to call it from the class that I created, I want to get the location from IP. I declared this, but I need to make a method call ... as static ...

To be honest with you, I'm pretty confused here, do I need to create an instance of address , city , etc.?

I have done it 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(); } } } 

}

+7
source share
5 answers

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) } 
+4
source

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

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

Hope this helps!

+5
source
 LocationTools.GetLocationFromIP( ... ) ; 

You should read 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 independent of any object identifier: 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 the identity of the object.

+2
source

You need to do two things:

  • First import the library where the static class is: import blabla;

  • Then call your static method to like something: LocationTools.GetLocationFromIP (address, city ...);

It should work.

+1
source

It's simple:

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

Just call the class directly from this method. Static means you don't need an instance of the class to call a method.

+1
source

All Articles