| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 666 人关注过本帖
标题:新手入门,大家帮看下下面这个代码是哪里出错了
只看楼主 加入收藏
仲子陵
Rank: 2
等 级:论坛游民
帖 子:37
专家分:10
注 册:2011-6-1
结帖率:90%
收藏
已结贴  问题点数:20 回复次数:4 
新手入门,大家帮看下下面这个代码是哪里出错了
系统提示说错误    1    当前上下文中不存在名称“persons”   
前面明明定义了一个集合‘persins'的,,想不明白是哪里写错了


using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace WindowsFormsApplication1
{
    public partial class SayHi : Form
    {
   
        public SayHi()
        {
            InitializeComponent();
        }
        class PersonInfo
        {
            public string Name { get; set; }
            public char Sex { get; set; }
            public int Age { get; set; }

            public PersonInfo(string name, char sex, int age)
            {
                this.Name = name;
                this.Sex = sex;
                this.Age = age;
            }
        }
        class Sample
        {
            List<PersonInfo> persons = new List<PersonInfo>   //定义persons集合

            {
                new PersonInfo("赵灵儿",'女',16),
                new PersonInfo("林月如",'男',18),
                new PersonInfo("李逍遥",'女',22),
                new PersonInfo("一如",'女',7),
            };
        }
      
        private void btnCount_Click(object sender, EventArgs e)
        {
            
            var orderBy = persons.Select     //提示说上下文没有“persons"这个名称
                (name => "你好!" + name)
                .OrderBy(name => name.Sustring(0, 1));
            string nameList = "";
            foreach (var item in orderBy)
            {
                nameList += item + "\t";
            }
            MessageBox.Show(nameList);
        }
   
        
 
   
    }

}
搜索更多相关主题的帖子: public 上下文 
2011-09-26 00:25
serious
Rank: 6Rank: 6
等 级:侠之大者
威 望:1
帖 子:81
专家分:497
注 册:2009-8-18
收藏
得分:20 
错是"persons"就在"sample"存:
程序代码:
class Sample
{
    public static List<PersonInfo> persons = new List<PersonInfo>   //定义persons集合
    {
        new PersonInfo("赵灵儿",'',16),
        new PersonInfo("林月如",'',18),
        new PersonInfo("李逍遥",'',22),
        new PersonInfo("一如",'',7),
    };
}

private void btnCount_Click(object sender, EventArgs e)
{
   
    var orderBy = Sample.persons.Select
        (name => "你好!" + name)
        .OrderBy(name => name.Sustring(0, 1));
    string nameList = "";
    foreach (var item in orderBy)
    {
        nameList += item + "\t";
    }
    MessageBox.Show(nameList);
}
此外你应该写:
程序代码:
...
private void btnCount_Click(object sender, EventArgs e)
{
    var orderBy = persons.OrderBy(person => person.Name.Substring(0, 1))
                         .Select(person => "你好!" + person.Name);
    ...

2011-09-26 02:01
仲子陵
Rank: 2
等 级:论坛游民
帖 子:37
专家分:10
注 册:2011-6-1
收藏
得分:0 
   private void btnCount_Click(object sender, EventArgs e)
        {
            var orderBy = persons.OrberBy(person => person.Name.Substring(0, 1))//
                                .Select(person => "你好!" + Name);
            string nameList = "";
            foreach (var item in orderBy)
            {
                nameList += item + "\n";
            }
            MessageBox.Show(nameList);
        }
    }
照你说的写,还是报相同的错误“persons"不存在!会不会类的顺序放错了??
2011-09-26 23:10
serious
Rank: 6Rank: 6
等 级:侠之大者
威 望:1
帖 子:81
专家分:497
注 册:2009-8-18
收藏
得分:0 
这应该OK :

private void btnCount_Click(object sender, EventArgs e)
{
    var orderBy = Sample.persons.OrderBy(person => person.Name.Substring(0, 1))
                                .Select(person => "你好!" + person.Name);
2011-09-27 06:02
仲子陵
Rank: 2
等 级:论坛游民
帖 子:37
专家分:10
注 册:2011-6-1
收藏
得分:0 

namespace WindowsFormsApplication1
{
    public partial class SayHi : Form
    {

        public SayHi()
        {
            InitializeComponent();
        }

        List<PersonInfo> persons = new List<PersonInfo>

            {
                new PersonInfo("赵灵儿",'女',16),
                new PersonInfo("林月如",'男',18),
                new PersonInfo("李逍遥",'女',22),
                new PersonInfo("一如",'女',7),
            };

        private void btnCount_Click(object sender, EventArgs e)
        {

            var orderBy = persons.Select(person => "你好!"+Name)
                                .OrderBy(person => Name.Substring(0,1));
            string nameList = "";
            foreach (var item in orderBy)
            {
                nameList += item + "\n";
            }
            MessageBox.Show(nameList);
        }
    }

    class PersonInfo
    {
        public string Name { get; set; }
        public char Sex { get; set; }
        public int Age { get; set; }

        public PersonInfo(string name, char sex, int age)
        {
            this.Name = name;
            this.Sex = sex;
            this.Age = age;
        }

    }
   应该这样写才对,集合应该写在和方法写在同一个类里面
2011-09-28 00:06
快速回复:新手入门,大家帮看下下面这个代码是哪里出错了
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.027020 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved