0

I am using .NET 2.0, need to implement VAR datatype in here:

var doc = XDocument.Parse(result);
var sw = doc.Descendants("viewport").Elements("southwest").SingleOrDefault();
if (sw != null)
{
   var lat = (double)sw.Element("lat");
   var lng = (double)sw.Element("lng");
   // do stuff
}

I used STRING instead

public string getLatLang(string address)
{
    string latlang = "";
    string url = "http://maps.googleapis.com/maps/api/geocode/xml?address=" + address + "&sensor=false";
    System.Net.WebClient client = new System.Net.WebClient();
    string result = client.DownloadString(url);
    string doc = System.Xml.Linq.XDocument.Parse(result).ToString();
    string sw = doc.Descendants("viewport").Elements("southwest").SingleOrDefault();
    if (sw != null)
    {
        string lat = (double)sw.Element("lat");
        string lng = (double)sw.Element("lng");
        latlang = lat + "," + lang;
        // do stuff
    }
    return latlang;
}

But I get an error :

'string' does not contain a definition for 'Descendants'

Please help me to replace VAR here.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
sajanyamaha
  • 3,119
  • 2
  • 26
  • 44
  • 3
    `var` is not itself a datatype, but represents a compiler inferred type. Not all `var` variables here have the same type. Start at the top, and work your way to the bottom to figure out each type. – Michael Graczyk Aug 11 '12 at 11:50
  • am tryng to get Latitude and Longitude using google maps in C# code behind and this line is causing me problems: string sw = doc.Descendants("viewport").Elements("southwest").SingleOrDefault(); Can you guys help me out here. I understand Not all var variables here have the same type,but wat is the possible solution here with .NET 2.0 – sajanyamaha Aug 11 '12 at 11:57
  • Also if you have Resharper you can do this: http://stackoverflow.com/questions/289743/tool-to-refactor-c-sharp-var-to-explicit-type – Michael Graczyk Aug 11 '12 at 18:58

4 Answers4

4

To replace var, research the actual return type of the method and change it to that. For example, XDocument.Parse can be found on MSDN here In the documentation, it states "Creates a new XDocument from a string", therefore, the return type must be XDocument. And if you drill down into one of the method overloads (like this one), you'll see the actual method's signature which confirms that it does indeed return an XDocument.

Also, Visual Studio has intellisense, so if you hover over something you can generally get details about it. Try typing System.Xml.Linq.XDocument.Parse(, When you type the first paren, you should see a popup in Visual Studio that tells you what the return type is for the method you're using. If intellisense is not working, then check to make sure you have a reference to the DLL.

Also note that Visual Studio has what is known as Object Explorer. This will allow you to see the method signatures of each object you're working with which includes the return types. Simply right click on any object or method and select "Go To Definition". Hopefully, the Visual Studio version you're using has this, if not, consider upgrading because it's extremely useful.

public string getLatLang(string address) 
{ 
    string latlang = ""; 
    string url = "http://maps.googleapis.com/maps/api/geocode/xml?address=" + address + "&sensor=false"; 
    System.Net.WebClient client = new System.Net.WebClient(); 
    string result = client.DownloadString(url); 
    XDocument doc = System.Xml.Linq.XDocument.Parse(result); 
    XElement sw = doc.Descendants("viewport").Elements("southwest").SingleOrDefault(); 
    if (sw != null) 
    { 
        string lat = sw.Element("lat").Value;  
        string lng = sw.Element("lng").Value;  
        latlang = String.Format("{0},{1}", lat, lng); 
        // do stuff 
    } 
    return latlang; 
} 

Edit: Please note that this solution will not work in .NET 2.0 without some hacks due to LINQ and redistributing System.Core is against the EULA, so you'll likely have to change XDocument to XmlDocument and figure out how to integrate it with Google's return value. I believe it has a Load method, or LoadXml method, can't remember which one does which.

Chris Gessler
  • 22,727
  • 7
  • 57
  • 83
0

var in itself isnt a data type it just takes the data type from the other side of the equality operator. read more about it here

therefore replacing every var with string wont help you will have to use the correct data type instead of var if you cannot use a var.
error in your case is originating because you i think on this line string doc = System.Xml.Linq.XDocument.Parse(result).ToString(); when replacing var you should have used XDocument but instead you used a string to accomodate errors used a ToString() function.
Edit:- after the comment

public string getLatLang(string address)
{
    string latlang = "";
    string url = "http://maps.googleapis.com/maps/api/geocode/xml?address=" + address + "&sensor=false";
    System.Net.WebClient client = new System.Net.WebClient();
    string result = client.DownloadString(url);
    XDocument doc = System.Xml.Linq.XDocument.Parse(result);
    XElement sw = doc.Descendants("viewport").Elements("southwest").SingleOrDefault();
    if (sw != null)
    {
        string lat = (double)sw.Element("lat").Value;
        string lng = (double)sw.Element("lng").Value;
        latlang = lat + "," + lang;
        // do stuff
    }
    return latlang;
}


Parv Sharma
  • 12,581
  • 4
  • 48
  • 80
  • yes,I understand this is not correct code snippet here,I did this to make the readers understand my objective. I need help returning latlang = lat + "," + lang in string format.Thanks – sajanyamaha Aug 11 '12 at 12:05
  • I tried but shows Error: 'System.Collections.Generic.IEnumerable' does not contain a definition for 'Elements' – sajanyamaha Aug 11 '12 at 12:14
0

The var keyword is used to implicitly type variables. You're letting the compiler infer the type of your variables. But under the covers, once compilation occurs, variables are still assigned specific types.

What you've done wrong is call .ToString() on your XDocument. Now you just have one big string and you're attempting to treat it like it's still an XDocument.

Try this instead.

XDocument doc = System.Xml.Linq.XDocument.Parse(result);
XElement = doc.Descendants("viewport").Elements("southwest").SingleOrDefault();

Edit

Finally got in front of a computer with Visual Studio on it. You have a few more problems.

The following will compile in .NET 3.5+.

public string getLatLang(string address)
{
    string latlang = "";    
    string url = "http://maps.googleapis.com/maps/api/geocode/xml?address=" + address + "&sensor=false";
    System.Net.WebClient client = new System.Net.WebClient();
    string result = client.DownloadString(url);
    XDocument doc = System.Xml.Linq.XDocument.Parse(result);
    XElement sw = doc.Descendants("viewport").Elements("southwest").SingleOrDefault();

    if (sw != null)
    {
        string lat = sw.Element("lat").ToString();
        string lng = sw.Element("lng").ToString();
        latlang = lat + "," + lng;
        // do stuff
    }
    return latlang;
}

(Notice that you were later explicitly casting values to double and still defining their types as string).

As for the error you mention in your comment, I'm not sure... the Elements<T>(IEnumerable<T>, XName) member is included in the extensions class of System.Xml.Linq (MSDN), so it should work. Make sure that you have also included the System.Linq directive with your other using directives:

using System.Linq;

As an aside, I actually don't see how a lot of the code you have written will work in .NET 2.0. The System.Xml.Linq and System.Linq namespaces weren't introduced until .NET 3.5. If you're really using .NET 2.0, then you may want to reference this thread for a work-around for the framework version you're using.

Community
  • 1
  • 1
Jeremy Wiggins
  • 7,239
  • 6
  • 41
  • 56
0

Ok ,Now I tried everything possible with .NET 2.0 ,I came up with this solution which I know is very low level coding but is the only possible solution with 2.0 , this works EXCELLENT FOR .NET 2.0 !!

public string getLatLang(string address)
{
    string latlang = "";
    string url = "http://maps.googleapis.com/maps/api/geocode/xml?address=" + address + "&sensor=false";
    System.Net.WebClient client = new System.Net.WebClient();
    string result = client.DownloadString(url);

    int firstlat = result.IndexOf("<lat>");
    int lastlat = result.IndexOf("</lat>");

    int firstlng = result.IndexOf("<lng>");
    int lastlng = result.IndexOf("</lng>");

    string _latitude = result.Substring(firstlat+5, (lastlat-5) - firstlat);
    string _longitude = result.Substring(firstlng+5, (lastlng-5) - firstlng);

    latlang = _latitude + "," + _longitude;
    return latlang;
}
sajanyamaha
  • 3,119
  • 2
  • 26
  • 44