名称“ str”在当前上下文中不存在 [英] The name 'str' does not exist in the current context

查看:82
本文介绍了名称“ str”在当前上下文中不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里声明了一个类变量

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

    }
}

,我想使用该类此方法中的变量 str

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

那是显示错误的地方。

推荐答案

您几乎可以肯定在方法中声明了变量(即 local 变量),而不是直接在类本身中(作为 instance 变量)。例如:

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

作为旁注:如果您非常C#的新手,我会强烈建议您暂时停止使用Silverlight,并编写一些控制台应用程序只是为了入门,并教给您一些基础知识。这样,您可以专注于C#作为一种语言和核心框架类型(例如文本,数字,集合,I / O),然后在以后开始编写GUI。 GUI编程通常涉及学习更多东西(线程,XAML,绑定等),并且试图一口气学习所有东西只会使事情变得更难。

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.

这篇关于名称“ str”在当前上下文中不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆