日期:2014-05-18  浏览次数:20809 次

编辑属性时出现的问题?
public class PhotoAlbum : Collection<Photograph>,IDisposable
  {
  private bool _hasChanged = false;

  public bool HasChanged
  {
  get
  {
  if (_hasChanged)

  return true;

  foreach (Photograph p in this)
  {
  if (p.HasChanged)
  return true;
  return false;
  }
  }
  internal set
  {
  _hasChanged = value;
  if (value == false)
  {
  foreach (Photograph p in this)
  p.HasChanged = false;
  }
  }


  }
  public Photograph Add(string fileName)
  {
  Photograph p = new Photograph(fileName);
  base.Add(p);
  return p;
  }

  protected override void InsertItem(int index, Photograph item)
  {
  base.InsertItem(index, item);
  HasChanged = true;
  }
  protected override void RemoveItem(int index)
  {
  Items[index].Dispose();
  base.RemoveItem(index);
  HasChanged = true;
  }
  protected override void SetItem(int index, Photograph item)
  {
  base.SetItem(index, item);
  HasChanged = true;
  }
  public void Dispose()
  {
  foreach (Photograph p in this)
  p.Dispose();
  }
  protected override void ClearItems()
  {
  if (Count > 0)
  {
  Dispose();
  base.ClearItems();
  HasChanged = true;
  }
  }

  }
}
编译时报错:错误 1 “Manning.MyPhotoAlbum.PhotoAlbum.HasChanged.get”: 并非所有的代码路径都返回值 C:\Documents and Settings\XuShuXiang\My Documents\Visual Studio 2005\Projects\MyPhotos\MyPhotoAlbum\PhotoAlbum.cs 16 13 MyPhotoAlbum
为什么啊???

------解决方案--------------------
C# code

        public  bool HasChanged 
        { 
            get 
            { 
                if (_hasChanged) 

                    return true; 

[color=#FF0000]                foreach (Photograph p in this) [/color]                { 
                    if (p.HasChanged) 
                        return true; 
                    return false; 
                } 
            } 
            internal set 
            { 
                _hasChanged = value; 
                if (value == false) 
                { 
                    foreach (Photograph p in this) 
                        p.HasChanged = false; 
                } 
            }