Click here to Skip to main content
16,004,727 members
Please Sign up or sign in to vote.
4.60/5 (3 votes)
See more:
Hi,

C#
public enum Clothes
{
public enum Skirts{FLORAL,SHORT}
public enum Shirts{BUTTONED,TUNIC}
}

I want to be able to write in my program something like this:
C#
Shop s=new Shop(Clothes.Skirts.FLORAL,Clothes.Shirts.TUNIC}




How can I do something like this in C#?

Thanks
Posted
Updated 10-Jan-14 2:22am
v2
Comments
ZurdoDev 10-Jan-14 8:08am    
Your Shop class will need a constructor that takes those arguments.
Member 10304617 10-Jan-14 8:12am    
First - it's just example I need this for something more complicated,
Second - this enum does not compile...

1 solution

you can try like this..


C#
class Program
{
    static void Main(string[] args)
    {
        Shop s = new Shop(Clothes.Skirts.FLORAL, Clothes.Shirts.TUNIC);
    }


}
public class Clothes
{
    public enum Skirts { FLORAL, SHORT }
    public enum Shirts { BUTTONED, TUNIC }
}
public class Shop
{
    public Shop(Clothes.Skirts param1, Clothes.Shirts param2)
    {

    }
}
 
Share this answer
 
Comments
Member 10304617 10-Jan-14 8:49am    
Thanks
Karthik_Mahalingam 10-Jan-14 8:57am    
welcome :)
BillWoodruff 10-Jan-14 11:52am    
Good answer. +4

I suggest you make the class 'Clothes static so no one can possibly try and create an instance of it. However, if you need non-static Fields, Properties, or Methods, in the 'Clothes class, then leaving it non-static would be necessary.

If you have not inspected what happens if you create an instance of a class, like 'Clothes, I suggest you do so, and then examine what intellisense exposes when you type "Clothes."

Try this: Clothes myNewClothes = new Clothes(); and then type "myNewClothes." in the VS editor window: surprised ?
Karthik_Mahalingam 10-Jan-14 20:21pm    
thanks bill,
if we try to create instance then we wont be able to access the enum values with the object,
all we can do is as you said , make it as a static class or to make the constructor as private :)
BillWoodruff 11-Jan-14 0:01am    
Hi Karthik,

fyi: Classes ... in a NameSpace scope ... cannot be defined as private. Your definition of 'Clothes above will compile, and is usable with either no access modifier on the class declaration, or the 'public modifier.

Adding a private constructor to the Class to prevent creating instances of it seems less "direct" in expressing "intent" than making it static, but I don't have "religious" convictions about this :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900