读书人

编辑属性时出现的有关问题

发布时间: 2012-02-21 16:26:23 作者: rapoo

编辑属性时出现的问题?
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.cs1613MyPhotoAlbum
为什么啊???

[解决办法]

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

读书人网 >C#

热点推荐