none
casting to a private enum outside of the class which contains that enum via a Property RRS feed

  • Question

  • I've got a class which contains the following method

            public void PopulateListBox(ListBox lb)
            {
                foreach (SiteFile item in run)
                {
                    if (item.Site != "None")
                        lb.Items.Add(new KeyValuePair<string, string>(item.Site, item.FilePath));
                }
            }

    The definition for SiteFile includes a Property called "Site" which maps to an enum.

    It, also, has a static Property called "Sites" which returns the type of that enum.

    I'd like to check Site of each SiteFile and only add those sites to the listbox whose site is not set to Sites.None.

    I'd like to do this outside of the SiteFile class.

    I thought I could pass the Type of Sites to other classes and then cast a temp variable to that Type.  This, I thought, would allow me to get a Sites.None which I could compare to the item.Site. 

    However, this doesn't work.

    Is there another way that I should be doing this?

    Monday, January 9, 2012 11:38 PM

Answers

  • Did you make your Sites enum public? If yes, then you can access thta anywhere outside of your class,

    public enum Sites
    {
            None,
            ...
    }
    
    public class SiteFile
    {
            public Sites Site { get; set; }
            ...
    }
    
    class SomeDifferentClass: Form
    {
           public void PopulateListBox(ListBox lb)
           {
                foreach (SiteFile item in run)
                {
                       if ( item.Site != Sites.None )
                       {
                              lb.Items.Add(new KeyValuePair<string, string>(item.Site, item.FilePath));
                        }
                 }
            }
    }
    

    Does it make sense?


    Please mark this post as answer if it solved your problem. Happy Programming!
    • Marked as answer by Paul Zhou Tuesday, January 17, 2012 8:47 AM
    Tuesday, January 10, 2012 5:47 AM

All replies

  • Did you make your Sites enum public? If yes, then you can access thta anywhere outside of your class,

    public enum Sites
    {
            None,
            ...
    }
    
    public class SiteFile
    {
            public Sites Site { get; set; }
            ...
    }
    
    class SomeDifferentClass: Form
    {
           public void PopulateListBox(ListBox lb)
           {
                foreach (SiteFile item in run)
                {
                       if ( item.Site != Sites.None )
                       {
                              lb.Items.Add(new KeyValuePair<string, string>(item.Site, item.FilePath));
                        }
                 }
            }
    }
    

    Does it make sense?


    Please mark this post as answer if it solved your problem. Happy Programming!
    • Marked as answer by Paul Zhou Tuesday, January 17, 2012 8:47 AM
    Tuesday, January 10, 2012 5:47 AM
  • Hi,

     

    Has your issue been resolved? Would you mind letting us know the result of the suggestions?

     

    Now I will mark an answer, you can mark others that you think to be so useful to your issue.

    If you still have any questions about this issue, please feel free to let me know.

     

    Have a nice day!


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Tuesday, January 17, 2012 8:48 AM