Question

Since i am using asp.net2.0 when i used the below class i am getting the following error.

Error 1 : The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?)

The class i have used.How can i use this code in asp.net2.0 without getting any error

public static XElement GetGeocodingSearchResults(string address)
{
    // Use the Google Geocoding service to get information about the user-entered address
    // See http://code.google.com/apis/maps/documentation/geocoding/index.html for more info...
    var url = String.Format("http://maps.google.com/maps/api/geocode/xml?  
    address={0}&sensor=false", HttpContext.Current.Server.UrlEncode(address));

    // Load the XML into an XElement object (whee, LINQ to XML!)
    var results = XElement.Load(url);

    // Check the status
    var status =results.Element ("status").Value;
    if (status != "OK" && status != "ZERO_RESULTS")
        // Whoops, something else was wrong with the request...
        throw new ApplicationException("There was an error with Google's Geocoding Service: " + status);

    return results;
}
Was it helpful?

Solution

var is simply a shortcut for the actual type of the right-side expression.

public static XElement GetGeocodingSearchResults(string address)
{
    // Use the Google Geocoding service to get information about the user-entered address
    // See http://code.google.com/apis/maps/documentation/geocoding/index.html for more info...
    string url = String.Format("http://maps.google.com/maps/api/geocode/xml?address={0}&sensor=false",
       HttpContext.Current.Server.UrlEncode(address));

    // Load the XML into an XElement object (whee, LINQ to XML!)
    XElement results = XElement.Load(url);

    // Check the status
    string status =results.Element ("status").Value;
    if (status != "OK" && status != "ZERO_RESULTS")
        // Whoops, something else was wrong with the request...
        throw new ApplicationException("There was an error with Google's Geocoding Service: " + status);

    return results;
}

BUT LINQ to XML (and whole LINQ functionality) is only available in .NET 3.5 and above. You should either upgrade to .NET 3.5 or switch to System.Xml

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top