名字“str”当前上下文中不存在

发布于 2024-11-07 03:30:50 字数 2965 浏览 1 评论 0原文

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

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

这就是错误显示的地方。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贩梦商人 2024-11-14 03:30:50

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

// 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、绑定等),尝试一次性学习所有内容只会让事情变得更加困难。

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.

枕梦 2024-11-14 03:30:50

它不起作用,因为 str 没有在另一个变量中声明。这是 sscopong 问题。您可以将 str 作为输入传递给另一个函数吗?

It doesn't work because str is not declared in the other variable. It's sscopong problem. Can you pass str as an input to the other function?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文