using System;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Album al = new Album(5);
            photo ph1 = new photo();
            ph1.Name = "杭州";
            photo ph2 = new photo();
            ph2.Name = "北海";
            photo p = al["杭州"];
            Console.WriteLine(p.Name.ToString());
            Console.ReadLine();
        }
    }    class Album
    {
        photo[] phs;        internal photo[] Phs
        {
            get { return phs; }
            set { phs = value; }
        }
        public Album(int count)
        {
            phs = new photo[count];
        }
        public photo this[string name]
        {
            get 
            {
                photo ph = null;
                for (int i = 0; i < phs.Length; i++)
                {
                    if (phs[i].Name == name)
                    {
                        ph = phs[i];
                    }
                }
                return ph;
            }
        }
    }    class photo
    {
        string _name;        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
    }
}在这段索引器代码中“if (phs[i].Name == name)”会报错说没有实例化对象是怎么回事啊?我明明已经实例化了啊

解决方案 »

  1.   

    你只是new了一个数组,数组里的元素仍然是null啊
      

  2.   

    那我在
      static void Main(string[] args) 
            { 
                Album al = new Album(5); 
                photo ph1 = new photo(); 
                ph1.Name = "杭州"; 
                photo ph2 = new photo(); 
                ph2.Name = "北海"; 
                photo p = al["杭州"]; 
                Console.WriteLine(p.Name.ToString()); 
                Console.ReadLine(); 
            } 
    里面加上
                  al.Phs[0] = ph1;
                al.Phs[1] = ph2;
    变成
     static void Main(string[] args)
            {
                Album al = new Album(5);
                photo ph1 = new photo();
                ph1.Name = "杭州";
                photo ph2 = new photo();
                ph2.Name = "北海";
                al.Phs[0] = ph1;
                al.Phs[1] = ph2;
                photo p = al["杭州"];
                Console.WriteLine(p.Name.ToString());
                Console.ReadLine();
            }
    还是不行啊。
      

  3.   


    当然不行了,你那个循环到2的时候依然数组Phs[2~5]依然是null
      

  4.   

    索引器可以这样写public photo this[string name]
            {
                get 
                {
                    foreach (photo ph in phs)
                    {
                        if (ph != null)
                        {
                            if (ph.Name == name)
                            {
                                return ph;
                            }
                        }
                    }
                    return null;
                }
            }
      

  5.   

    建议按照下面的写法:using System; 
    using System.Collections.Generic; 
    using System.Text; 
    using System.IO;namespace ConsoleApplication1 
    {     class Program 
        { 
            static void Main(string[] args) 
            { 
                Album al = new Album();
                photo ph1 = new photo();
                ph1.Name = "杭州";
                photo ph2 = new photo();
                ph2.Name = "北海";
                al.AddPhoto(ph1);
                al.AddPhoto(ph2);
                photo p = al["杭州州"];
                if (p != null)
                {
                    Console.WriteLine(p.Name.ToString());
                }
                else
                {
                    Console.WriteLine("对象不存在");
                }            Console.ReadLine();        } 
        } 
        class Album
        {
            List<photo> phs=new List<photo>();
            public Album()
            {
            }        public void AddPhoto(photo ph)
            {
                FindPhoto f = new FindPhoto(ph.Name);
                if (!this.phs.Exists(new Predicate<photo>(f.hasPhoto)))
                {
                    this.phs.Add(ph);
                }
            }                public photo this[string name]
            {
                get
                {
                    FindPhoto f = new FindPhoto(name);
                    if (this.phs.Exists(new Predicate<photo>(f.hasPhoto)))
                    {
                        return this.phs.Find(new Predicate<photo>(f.hasPhoto));
                    }
                    return null;
                }
            }
        }    class FindPhoto
        { 
           private string photoName=String.Empty;
            public FindPhoto(string pName)
            {
                this.photoName = pName;
            }        public bool hasPhoto(photo ph)
            {
                if (ph.Name.Equals(this.photoName))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }    class photo
        {
            string _name;        public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
        } 
    }