保存到类变量

时间:2011-05-13 02:55:06

标签: c# silverlight class variables

我需要将“pri1”变量保存到类变量中,以便同一类的其他方法能够访问。 在这些行之间

“pri1.Remove(最近);

foreach(pri1中的字符串项)“

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);

                foreach (string item in pri1)
                {
                    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);
                            graphicsLayer.Graphics.Add(graphic);
                        }
                    }
                }
        }
    }

2 个答案:

答案 0 :(得分:2)

您需要在课程中添加field

List<string> myField;

然后,您可以将该字段用作类中任何位置的变量。

答案 1 :(得分:0)

假设类中有一个字段可以存储List<string>类型

this.VariableName = pri1;

如果类中没有变量,并且您没有自定义它,则需要子类化并添加它。