2

I am learning C# and I am trying to put color schemes/themes into Multidimentional Array so I can access them from any other class.

This is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace sFirstApp
{
    public class colorSchemes
    {
        string[,] colorBase = new string[3, 4];
        colorBase[0, 0] = "D60000";     //Main Background.
        colorBase[0, 1] = "DB3E25";     //Text Color.
        colorBase[0, 2] = "FF5940";     //Text Shadow.
    }
}

Visual Studio keeps saying: The name 'colorBase' does not exist in the current context.

Where is my problem and how is this kind of class called?

Also the reason I am trying to use 2D array is because I want 1st dimension of array to specify Theme #.

I am planning to store Theme # in App Settings as number, then simply use 2D arrays like:

ColorTheme[theme #][theme element]

3
  • 1
    This is a perfectly fine beginner question IMHO. It contains a short piece of code illustrating the problem (though the using and namespace parts could be omitted) and a clear problem statement. Commented Jun 8, 2016 at 18:51
  • You should expose a method in this class to get access to the internal data. And call your class by the correct casing: ColorSchemes. Commented Jun 8, 2016 at 18:51
  • 1
    Why are those values being assigned in plain class declaration and not inside a constructor or method? Commented Jun 8, 2016 at 18:56

3 Answers 3

5

Don't use array indexes to arbitrarily mean something more than a position. Use named properties for each of these colors, and even better, use System.Drawing.Color.

This would be a good style for maintanable, modern C#:

namespace FirstApp
{
    public static class ColorSchemes
    {
        public struct ThemeColors
        {
            public Color MainBackground { get; internal set;  }
            public Color TextColor { get; internal set; }
            public Color TextShadow { get; internal set;  }
        }

        private static readonly ThemeColors[] Colors =
        {
            new ThemeColors
            {
                MainBackground = Color.FromArgb(0xD6, 0x00, 0x00),
                TextColor = Color.FromArgb(0xDB, 0x3E, 0x25),
                TextShadow = Color.FromArgb(0xFF, 0x59, 0x40)
            }
        };

        public static ThemeColors GetThemeColors(int themeIndex)
        {
            if (themeIndex < 0 || themeIndex >= Colors.Length)
                throw new ArgumentOutOfRangeException();
            return Colors[themeIndex];
        }
    }
}

The colors can now be accessed with a simple, readable ColorSchemes.GetThemeColors(0).TextColor. They can also be loaded from other locations dynamically without breaking an API by using properties. Additional themes can be added simply.

For proper object oriented design, there should probably be a Theme class that returns a ThemeColors struct when requested for its colors instead of using a static ColorSchemes class.

That could be implemented something like this (keep in mind explicit constructors should probably be used for much of this):

namespace FirstApp
{
    public class Theme
    {
        public struct ThemeColors
        {
            public Color MainBackground { get; internal set; }
            public Color TextColor { get; internal set; }
            public Color TextShadow { get; internal set; }
        }

        // Example other property
        public string Name { get; set; }

        public ThemeColors Colors { get; set; }
    }

    public static class DefaultThemes
    {
        public static Theme MainTheme =>
            new Theme
            {
                Name = "Main Theme",
                Colors = new ThemeColors
                {
                    MainBackground = Color.FromArgb(0xD6, 0x00, 0x00),
                    TextColor = Color.FromArgb(0xDB, 0x3E, 0x25),
                    TextShadow = Color.FromArgb(0xFF, 0x59, 0x40)
                }
            };
    }
}

Then, you'd have DefaultThemes.MainTheme.Colors.TextShadow.

Sign up to request clarification or add additional context in comments.

3 Comments

The reason I wanted to store colors in 2 Dimensional Arrays is so I could store SelectedTheme # in App Settings and then simply load it everywhere as: ColorScheme[theme#][theme Element]
@AleRemote225 In this case, the theme could very well be a number. I'll edit the question.
@AleRemote225 Sorry, I should have said "edit my answer".
2

You can create a public method in the class of the array.

public string GetColorBase(int position1, int position2){
    return colorBase[position1, position2];
}

Then use the method, for example

colorSchemes oColorSchemes = new colorSchemes();
string ColorBase = oColorSchemes.GetColorBase(0, 1);

Thanks @JeremyKato for correction

1 Comment

You probably should change the parameters to take two ints - it's easily possible that he could add items to the other dimensions of the array.
0

This can be done by marking the array public

public string[,] colorBase = new string[3, 4];
colorBase[0, 0] = "D60000";     //Main Background.
colorBase[0, 1] = "DB3E25";     //Text Color.
colorBase[0, 2] = "FF5940";     //Text Shadow.

To use the class from another namespace you need add the following using statement on top. More about Access Modifiers can be found here

using sFirstApp;

From another place inside the app you can then say:

var theme = new colorSchemes();
var colors = theme.colorBase;

5 Comments

He also needs an instance
Not really a good idea to be exposing the array publicly. He should access this via a well defined method.
@ManoDestra, you are absolutely right. the code above was just to demonstrate how to mark a property public.
@FlorianSchaal To be completely accurate, this is a field, not a property. Properties use getters and setters, while fields are like variables for an object.
@Kupiakos you are correct. I have edited the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.