Click here to Skip to main content
15,911,762 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
A real newbie question and I'm not even sure if I'm using the correct terminology.
I'm wanting to build a Class which has 7 color properties plus a whole bunch of other properties (not colors). The color properties are all related to the same thing in the class and are basically a level indicator from 1 to 7 (Think of a water level meter/gauge).
Should I create the class and name/create each color property individually by name e.g. Level1Color, Level2Color...Level7Color
OR
Should I create a property array of 7 colors e.g. LevelColors[] ? How do I limit it to 7 colors?
I think the code might look something like this:
C#
Public Class MyObject1
{
    Level1Color {get; set};
    Level2Color {get; set};
//etc
}

Public Class MyObject2
{
    LevelColor[] {get; set}:
}


What I have tried:

I haven't tried anything yet as I am uncertain of which is the best practice, single properties or an array.
Posted
Updated 2-Nov-19 0:47am

1 solution

If they are literally just "levels", then individual properties don;g make a lot of sense - the names aren't particularly useful, and having them as an array makes some code rather simpler: you can loop through them easily for example when saving to non-volatile storage.

I'd go with the array (but do note that your code won't compile: C# is case sensitive, so "Public" and "Class" are not the same as public and class; the colon at the end of the array based version is wrong; the whole property declaration is wrong for both types).
C#
public class MyClass
    {
    public const int MaxColors = 7;
    public Color[] ColorLevels { get; set; } = new Color[MaxColors];
    }


Quote:
Thanks, OriginalGriff. That did the trick for a known number of colors. What if I want to make it a variable number of colors, only known at the time the object is instatiated. How do I use a property to pass in the and read the number of items in the array?


That's easy: instead of allocating the array when you declare the property, you allocate it as part of your class constructor:
C#
public class MyClass
    {
    public Color[] ColorLevels { get; private set; }
    public MyClass(int maxColours)
        {
        ColorLevels = new Color[maxColours];
        }
    }
Your code can check how many levels are allowed by querying the ColourLevels.Length property.
 
Share this answer
 
v3
Comments
wkiess01 2-Nov-19 7:36am    
Thanks, OriginalGriff. That did the trick for a known number of colors. What if I want to make it a variable number of colors, only known at the time the object is instatiated. How do I use a property to pass in the and read the number of items in the array?
OriginalGriff 2-Nov-19 7:45am    
Answer updated.
wkiess01 2-Nov-19 7:59am    
Once again, that did the trick. So how do I get the number of levels to appear as a property? Something like this perhaps:
public int LevelCount
{
  get { return ColorLevels.Length; }
}
OriginalGriff 2-Nov-19 8:10am    
Yes - but there probably isn't any need, as ColorLevels is public and external code can access the Length property directly.

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