Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
508 views
in Technique[技术] by (71.8m points)

c# - The name 'str' does not exist in the current context

I have declared a class variable in here

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

    }
}

and I want to use the class variable str in this method

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

That's where the error shows.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You've almost certainly declared the variable in a method (i.e. as a local variable), instead of directly in the class itself (as an instance variable). For example:

// 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)
        {
            ...
        }
    }
}

As a side-note: if you're very new to C#, I would strongly recommend that you stop working on Silverlight temporarily, and write some console apps just to get you going, and to teach you the basics. That way you can focus on C# as a language and the core framework types (text, numbers, collections, I/O for example) and then start coding GUIs later. GUI programming often involves learning a lot more things (threading, XAML, binding etc) and trying to learn everything in one go just makes things harder.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...