none
交错数组 的文本型索引器 RRS feed

  • 问题

  • 怎样为交错数组 设置文本型索引器?

        public class aaa
        {
            int[][] _numbers;
            public int this[int index1][string index2] //这里该怎么设置?
            {
                set
                {
                    int index2 = ConvertToIndex(index2); //调用另外的函数
                    _numbers[index1][index2] = value;
                }
                get
                {
                    int index2 = ConvertToIndex(index2); //调用另外的函数
                    return _numbers[index1][index2];
                }
            }
        }

    2012年12月18日 23:05

答案

  • dear

    索引器,顾名思义就是要用数字 int 当索引,若你使用字符,只会让代码变得难以维护,个人不建议写这样的代码

    http://msdn.microsoft.com/zh-cn/library/2549tw02(v=vs.80).aspx

    再多定义一个class,参考以下

    public class Person
    {
        public int Age { get; set; }
    
        public string Name { get; set; }
    }
    
    public class Member
    {
        private List<Person> _persons;
    
        public string Title { get; set; }
    
        public List<Person> Persons
        {
            get
            {
                return _persons;
            }
            set { _persons = value; }
        }
    
        public Person this[string name]
        {
            get
            {
                if (this.Persons == null)
                {
                    return null;
                }
                var query = (from person in this.Persons
                             where person.Name == name
                             select person).FirstOrDefault();
    
                return query;
            }
            set
            {
                if (this.Persons == null)
                {
                    return;
                }
                var query = (from person in this.Persons
                             where person.Name == name
                             select person).FirstOrDefault();
                if (query != null)
                {
                    query = value;
                }
            }
        }
    }
    
    public class Group
    {
        private List<Member> _members;
    
        public List<Member> Members
        {
            get
            {
                if (this._members == null)
                {
                    this._members = new List<Member>()
                        {
                           new Member() {
                               Title = "鴻海",
                               Persons = new List<Person>(){
                                   new Person() { Age = 31, Name = "余小章" },
                                   new Person() { Age = 13, Name = "jordan"},
                                   new Person() { Age = 20, Name = "jason"}
                               }},
                           new Member() {
                               Title = "蘋果",
                               Persons = new List<Person>(){
                                   new Person() { Age = 45, Name = "john" },
                                   new Person() { Age = 35, Name = "mary" },
                                   new Person() { Age = 40, Name = "luke" }
                               }},
                        };
                }
                return _members;
            }
            set { _members = value; }
        }
    
        public Member this[int index]
        {
            get { return this.Members[index]; }
            set { this.Members[index] = value; }
        }
    }

    调用方式

    Group group = new Group();
    var member = group[0];
    var person = group[0]["余小章"];


    秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/



    2012年12月20日 15:58

全部回复

  • 索引器貌似不允许定义成交错的,建议这样看看:

    public class aaa
        {
            int[][] _numbers;
            public int this[int index1,string index2]         {
                set
                {
                    int i2 = Convert.ToInt32(index2); //调用另外的函数
                    _numbers[index1][i2] = value;
                }
                get
                {
                    int i2 = ConvertToIndex(index2); //调用另外的函数
                    return _numbers[index1][index2];
                }
            }
        }

    我的博客园
    慈善点击,点击此处
    和谐拯救危机,全集下载,净化人心

    2012年12月19日 2:17
    版主
  • 谢谢您!您这样的调用方法是 aaa[0,"one"],想得到这样的形式 aaa[0][“one”]

    2012年12月19日 2:29
  • dear

    索引器,顾名思义就是要用数字 int 当索引,若你使用字符,只会让代码变得难以维护,个人不建议写这样的代码

    http://msdn.microsoft.com/zh-cn/library/2549tw02(v=vs.80).aspx

    再多定义一个class,参考以下

    public class Person
    {
        public int Age { get; set; }
    
        public string Name { get; set; }
    }
    
    public class Member
    {
        private List<Person> _persons;
    
        public string Title { get; set; }
    
        public List<Person> Persons
        {
            get
            {
                return _persons;
            }
            set { _persons = value; }
        }
    
        public Person this[string name]
        {
            get
            {
                if (this.Persons == null)
                {
                    return null;
                }
                var query = (from person in this.Persons
                             where person.Name == name
                             select person).FirstOrDefault();
    
                return query;
            }
            set
            {
                if (this.Persons == null)
                {
                    return;
                }
                var query = (from person in this.Persons
                             where person.Name == name
                             select person).FirstOrDefault();
                if (query != null)
                {
                    query = value;
                }
            }
        }
    }
    
    public class Group
    {
        private List<Member> _members;
    
        public List<Member> Members
        {
            get
            {
                if (this._members == null)
                {
                    this._members = new List<Member>()
                        {
                           new Member() {
                               Title = "鴻海",
                               Persons = new List<Person>(){
                                   new Person() { Age = 31, Name = "余小章" },
                                   new Person() { Age = 13, Name = "jordan"},
                                   new Person() { Age = 20, Name = "jason"}
                               }},
                           new Member() {
                               Title = "蘋果",
                               Persons = new List<Person>(){
                                   new Person() { Age = 45, Name = "john" },
                                   new Person() { Age = 35, Name = "mary" },
                                   new Person() { Age = 40, Name = "luke" }
                               }},
                        };
                }
                return _members;
            }
            set { _members = value; }
        }
    
        public Member this[int index]
        {
            get { return this.Members[index]; }
            set { this.Members[index] = value; }
        }
    }

    调用方式

    Group group = new Group();
    var member = group[0];
    var person = group[0]["余小章"];


    秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/



    2012年12月20日 15:58
  • 谢谢余老师的引导!

    2012年12月21日 6:04