Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
As the title says and as you can see into my code example below, I am trying to make a List<string> of values from properties which are placed into nested classes. My problem is that I can't make my class and sub classes usable in a foreach statement!!!

Any idea?

What I have tried:

C#
namespace Something
{
    public class Theme
    {
        public class Custom
        {
            public class BackColor
            {
                public static string Name { get => "[Custom]BackColor"; }
                //...
            }
            public class EmphasisColor
            {
                public static string Name { get => "[Custom]EmphasisColor"; }
                //...
            }
            public class ForeColor
            {
                public static string Name { get => "[Custom]ForeColor"; }
                //...
            }
        }
    }

    public static List<string> GetNamesList()
    {
        List<string> list = new List<string>{};

        foreach (string name in ???) // <---•
        {
            list.Add(name);
        }

        return list;
    }
}
Posted
Updated 15-Dec-22 4:10am
Comments
[no name] 15-Dec-22 14:00pm    
You haven't actually "created" anything. Once you have an "object", you can use "reflection" to examine the properties.

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/reflection

You can't. There's no enumerator for the Theme or Custom classes. There's no collection to enumerate.

I really can't say for sure because I have no idea what you're really trying to do with this code, but nested classes like this is not the way to go about creating a theme.
 
Share this answer
 
Comments
Simos Sigma 15-Dec-22 10:15am    
How would you do it?
Dave Kreskowiak 15-Dec-22 10:15am    
Do what? I have no idea what you're trying to do with this code?
Simos Sigma 15-Dec-22 10:18am    
You wrote "nested classes like this is not the way to go about creating a theme". And I asked, "How would you do it?".
Dave Kreskowiak 15-Dec-22 10:20am    
In WinForms, just setting the properties of controls, I would implement a Dictionary<string, string>, where the first string is the name of the control and the property, and the second string is the serialized value to set the property to.
Simos Sigma 15-Dec-22 10:27am    
Nice, and in case you need to have more than two strings?
"Nesting classes" doesn't create any instances of those classes - or even allocate any space within the "outer class" to hold such an instance.

I'd strongly suggest that you go back to your course notes and re read them a few times to try and get the idea of classes and instances sorted in your mind, then reread your assignment in the light of that information - because at the moment, I'm not at all sure you understand any of it enough to do your task.
That code certainly doesn't show any clear idea of what you need to do!
 
Share this answer
 

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