我已經創建了一個名爲telephoneNumber的類和一個全局變量。這個變量是在一個方法中設置的,並在另一個方法中使用。但是這個變量返回null。所有方法和這個全局變量在同一個類中。請幫助理解這個問題。非常感謝。我的課程是:爲什麼實例變量在C#中返回null?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Net.Http;
using Newtonsoft.Json;
using System.Collections;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string telephoneNumber;
private async void GetSingleLocationInfo(string href)
{
var hereNetUrl = string.Format(
href+"&accept=application/json"
);
// get data from HERE.net REST API
var httpClient = new HttpClient();
var hereNetResponse = await httpClient.GetStringAsync(hereNetUrl);
// deseralize JSON from Here.net
using (var tr = new StringReader(hereNetResponse))
using (var jr = new JsonTextReader(tr))
{
var rootObjectResponse = new JsonSerializer().Deserialize<Object>(jr);
String contacts = rootObjectResponse.ToString();
int startIndex = contacts.IndexOf("phone");
if (startIndex != -1)
{
String value = contacts.Substring(startIndex, 50);
telephoneNumber=value.Substring(value.IndexOf("+"));
}
else
{
telephoneNumber="";
}
}
}
private async void GeocodingWin8Query()
{
// build URL for Here.net REST service
string currentgeoLoc = "37.75075,-122.393472";
string queryString = "taxi";
string appID = "dV04O71v5F3f2W"; // MAKE SURE TO GET YOUR OWN from developers.here.net
object appCode = "8QVr5uSXwfcowDrA"; // MAKE SURE TO GET YOUR OWN from developers.here.net
var hereNetUrl = string.Format(
"http://demo.places.nlp.nokia.com/places/v1/discover/search?at={0}&q={1}&app_id={2}&app_code={3}&accept=application/json",
currentgeoLoc, queryString, appID, appCode);
// get data from HERE.net REST API
var httpClient = new HttpClient();
var hereNetResponse = await httpClient.GetStringAsync(hereNetUrl);
// deseralize JSON from Here.net
using (var tr = new StringReader(hereNetResponse))
using (var jr = new JsonTextReader(tr))
{
var rootObjectResponse = new JsonSerializer().Deserialize<RootObject>(jr);
List<Item> items=rootObjectResponse.results.items;
foreach(Item item in items){
string href = item.href;
GetSingleLocationInfo(href);
Console.WriteLine (telephoneNumber);//returns null
}
}
}
private void button1_Click(object sender, EventArgs e)
{
GeocodingWin8Query();
}
}
public class Category
{
public string id { get; set; }
public string title { get; set; }
public string href { get; set; }
public string type { get; set; }
}
public class Item
{
public List<double> position { get; set; }
public int distance { get; set; }
public string title { get; set; }
public Category category { get; set; }
public string icon { get; set; }
public string vicinity { get; set; }
public List<object> having { get; set; }
public string type { get; set; }
public string href { get; set; }
public string id { get; set; }
public double? averageRating { get; set; }
}
public class Context
{
public Location location { get; set; }
public string type { get; set; }
}
public class Search
{
public Context context { get; set; }
}
public class RootObject
{
public Results results { get; set; }
public Search search { get; set; }
}
}
它在哪裏返回null?你已經嘗試了什麼?你究竟想達到什麼目的? – Clint 2013-04-07 15:19:12
Clint的評論借調 - 你有沒有嘗試調試任何代碼來看看telephoneNumber被設置在哪裏? – 2013-04-07 15:20:07
把斷點放在你的變量賦值的地方。確保它沒有得到空值。 – ankurtr 2013-04-07 15:20:21