2012-03-20 31 views
0

下面是我的課。我需要讓它變得可枚舉。我看了網上,雖然我發現很多文件,我仍然失去了。我認爲這絕對是我第一次問這個問題,但是有人可能只是爲了讓我覺得這件事情可以枚舉,我會從那裏弄清楚。 我使用C#ASP.Net 4.0IEnumerable C#類操作方法

public class ProfilePics 
{ 
    public string status { get; set; } 
    public string filename { get; set; } 
    public bool mainpic { get; set; } 
    public string fullurl { get; set; } 

} 
+0

你會想什麼枚舉?我沒有看到任何明顯的數據突出作爲一個很好的候選人。 – aldo 2012-03-20 05:36:59

回答

1

嗯......如果你想要的是有人「只是讓這個混賬東西可枚舉」,在這裏不用...

public class ProfilePics : System.Collections.IEnumerable 
{ 
    public string status { get; set; } 
    public string filename { get; set; } 
    public bool mainpic { get; set; } 
    public string fullurl { get; set; } 

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 
    { 
     yield break; 
    } 
} 

它不枚舉任何東西,但它是可枚舉。

現在,我會盡量搞一些讀心術,不知道,如果你想是這樣的:

public class ProfilePicture 
{ 
    public string Filename { get; set; } 
} 

public class ProfilePics : IEnumerable<ProfilePicture> 
{ 
    public List<ProfilePicture> Pictures = new List<ProfilePictures>(); 

    public IEnumerator<ProfilePicture> GetEnumerator() 
    { 
     foreach (var pic in Pictures) 
      yield return pic; 
     // or simply "return Pictures.GetEnumerator();" but the above should 
     // hopefully be clearer 
    } 
} 
+0

哇!......沒有意識到這個問題已經過時了。編輯將它帶到主頁... – 2014-06-25 23:46:56

+0

+1爲「只是讓這件事情可枚舉」解決方案 – vesan 2014-06-25 23:49:13

+0

完全是我徹底忘記了這個問題,可悲的是我剛剛通過我的問題列表有一天嘗試以確保我沒有忘記任何。好,謝謝 – scripter78 2014-06-26 02:18:05

0

的問題是:你要列舉什麼?

我猜你想要某種容器,包括您的ProfilePics類型的項目與

List<ProfilePics> 

喜歡這樣一個類內部,以便去:

public class ProfilePic 
{ 
    public string status { get; set; } 
    public string filename { get; set; } 
    public bool mainpic { get; set; } 
    public string fullurl { get; set; } 

} 

public class ProfilePics : IEnumerable<ProfilePic> 
{ 
    private pics = new List<ProfilePic>(); 

    // ... implement the IEnumerable members 
} 

或只是清楚地使用List<ProfilePics>那些你需要容器​​的地方。

如果你沒有錯過它:這裏是MSDN的Docu此IEnumerable(有一些更多的例子)

0

要枚舉類應該實現一些集合。我的例子中沒有看到任何集合屬性。如果你想收集配置文件圖片,請將你的班級重命名爲'ProfiePic'並使用列表。

如果要將某些屬性公開爲集合,請將其設置爲IEnumerable或List或其他集合。

0

我會用一個類繼承無:

using System; 
using System.Collections.Generic; 

namespace EnumerableClass 
{ 
    public class ProfilePics 
    { 
     public string status { get; set; } 
     public string filename { get; set; } 
     public bool mainpic { get; set; } 
     public string fullurl { get; set; } 

     public IEnumerator<object> GetEnumerator() 
     { 
      yield return status; 
      yield return filename; 
      yield return mainpic; 
      yield return fullurl; 
     } 
    } 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      ProfilePics pic = new ProfilePics() { filename = "04D2", fullurl = "http://demo.com/04D2", mainpic = false, status = "ok" }; 
      foreach (object item in pic) 
      { 
       Console.WriteLine(item); 
       //if (item is string) ... 
       //else if (item is bool) ... 
      } 
     } 
    } 
}