名称“str”在当前上下文中不存在
本文关键字:上下文 不存在 str 名称 | 更新日期: 2023-09-27 17:56:13
我在这里声明了一个类变量
void downloader_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null)
{
Stream responseStream = e.Result;
StreamReader responseReader = new StreamReader(responseStream);
string response = responseReader.ReadToEnd();
string[] split1 = Regex.Split(response, "},{");
List<string> pri1 = new List<string>(split1);
pri1.RemoveAt(0);
string last = pri1[pri1.Count() - 1];
pri1.Remove(last);
}
}
我想在此方法中使用类变量str
void AddPrimaryMarkerGraphics(object sender, getPrimaryListCompletedEventArgs e)
{
List<PrimaryClass> primaryList = new List<PrimaryClass>(e.Result);
PrimaryClass sc = new PrimaryClass();
for (int a = 0; a <= e.Result.Count - 1; a++)
{
string schname = e.Result.ElementAt(a).PrimarySchool;
string tophonour = e.Result.ElementAt(a).TopHonour;
string cca = e.Result.ElementAt(a).Cca;
string topstudent = e.Result.ElementAt(a).TopStudent;
string topaggregate = e.Result.ElementAt(a).TopAggregate;
string topimage = e.Result.ElementAt(a).TopImage;
foreach (string item in str)
{
string abc = "[{" + item + "}]";
byte[] buf = System.Text.Encoding.UTF8.GetBytes(abc);
MemoryStream ms = new MemoryStream(buf);
JsonArray users = (JsonArray)JsonArray.Load(ms);
var members = from member in users
//where member["SEARCHVAL"]
select member;
foreach (JsonObject member in members)
{
string schname = member["SEARCHVAL"];
string axisX = member["X"];
string axisY = member["Y"];
// Do something...
string jsonCoordinateString = "{'Coordinates':[{'X':" + axisX + ",'Y':" + axisY + "}]}";
CustomCoordinateList coordinateList = DeserializeJson<CustomCoordinateList>(jsonCoordinateString);
GraphicsLayer graphicsLayer = MyMap.Layers["MyGraphicsLayer_Primary"] as GraphicsLayer;
for (int i = 0; i < coordinateList.Coordinates.Count; i++)
{
Graphic graphic = new Graphic()
{
Geometry = new MapPoint(coordinateList.Coordinates[i].X, coordinateList.Coordinates[i].Y),
Symbol = i > 0 ? PrimarySchoolMarkerSymbol : PrimarySchoolMarkerSymbol
};
graphic.Attributes.Add("PrimarySchool", schname);
graphic.Attributes.Add("xcoord", axisX);
graphic.Attributes.Add("ycoord", axisY);
graphicsLayer.Graphics.Add(graphic);
}
}
}
}
}
这就是错误显示的地方。
您几乎可以肯定已经在方法中声明了变量(即作为局部变量),而不是直接在类本身中声明(作为实例变量)。例如:
// Wrong
class Bad
{
void Method1()
{
List<string> str = new List<string>();
}
void Method2()
{
foreach (string item in str)
{
...
}
}
}
// Right
class Good
{
private List<string> str = new List<string>();
void Method1()
{
str = CreateSomeOtherList();
}
void Method2()
{
foreach (string item in str)
{
...
}
}
}
作为旁注:如果你对 C# 非常陌生,我强烈建议你暂时停止使用 Silverlight,并编写一些控制台应用程序,只是为了让你开始学习,并教你基础知识。这样,您就可以专注于 C# 作为一种语言和核心框架类型(例如文本、数字、集合、I/O),然后稍后开始编写 GUI。GUI 编程通常涉及学习更多的东西(线程、XAML、绑定等),并且试图一次性学习所有内容只会让事情变得更加困难。
它不起作用,因为 str 没有在另一个变量中声明。这是sscopong的问题。您可以将 str 作为输入传递给另一个函数吗?