Home > Blockchain >  C# How to use values in array from one Class to another Class
C# How to use values in array from one Class to another Class

Time:06-16

I need help to complete this C# programming.

Here are what I made to prompt user for 3 code of colour and display the names of colour.

(Assume that they are supposed to input only 'A','B', or'C')

This is Class1

using System;
using static System.Console;

    class Class1
    {
        private char code;
        public char Code { get; set; }
        public Class1(char aCode)
        {
            code = aCode;
        }
    }

This is Class2

class Class2
    {
        public static void Main()
        {
            int x;
            char charCode;
            Class1[] valueCodeArray = new Class1[3];
            
            for (x = 0; x < valueCodeArray.Length;   x)
            {
                WriteLine("Input color code No{0}",x 1);
                charCode = Convert.ToChar(ReadLine());
                valueCodeArray[x] = new Class1(charCode);
            }

            for (x = 0; x <valueCodeArray.Length ;   x)
            {
                string NameOfCode = changeCodeToName(valueCodeArray[x].Code);
                WriteLine("No{0}: color is {1}", x   1, NameOfCode);
            }
        }
        public static string changeCodeToName(char charCode)
        {

            string NameOfCode = "";
            switch (charCode)
            {
                case 'A':
                        NameOfCode = "Red";
                    return NameOfCode;

                case 'B':
                    NameOfCode = "Blue";
                    return NameOfCode;
                case 'C':
                    NameOfCode = "Green";
                    return NameOfCode;
            }
            return "";


        }

    }

The output displays... No1: color is Red. No2: color is Blue. No3: color is Green

Then I want to include this array in Class1

readonly string[] nameArray = new string[3] { "Red", "Blue", "Green"};

To use this in Class2, I have to change something in the method.

public static string changeCodeToName(char charCode)

    {

        string NameOfCode = "";
        switch (charCode)
        {
            case 'A':
                    NameOfCode = //nameArray[0];
                return NameOfCode;

            case 'B':
                NameOfCode = //nameArray[1];
                return NameOfCode;
            case 'C':
                NameOfCode = //nameArray[2];
                return NameOfCode;
        }
        return "";


    }

I changed "Red" to nameArray[0] but didn't work.

CodePudding user response:

I would encapsulate the name into your Class1.

class Class1
{
    private static Dictionary<char, string> __names = new Dictionary<char, string>()
    {
        { 'A', "Red" },
        { 'B', "Blue" },
        { 'C', "Green" },
    };
    public char Code { get; private set; }
    public string Name => __names.ContainsKey(this.Code) ? __names[this.Code] : "";
    public Class1(char code)
    {
        this.Code = code;
    }
}

That's really where it belongs.

Now the rest of your code becomes simple:

for (x = 0; x < valueCodeArray.Length;   x)
{
    Console.WriteLine("No{0}: color is {1}", x   1, valueCodeArray[x].Name);
}

CodePudding user response:

You can use an enum to store the colours like this.

public enum KnownColours
{
    Red,
    Blue,
    Green,
}

You can then initialize your class with this array of colours

class Class1
{

    public readonly Array Colours = typeof(KnownColours).GetEnumValues();
    ...
}

Since it's an array of string values, you can access them via their index

var MyClass = new Class1();
MyClass.Colours.GetValue(0); //Red

enum is a great way of storing read-only constants such as colours in this case. You can read more about it here: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/enum

CodePudding user response:

Since you are attempting to store it as string array, you could initialize your class like

class Class1
{
    public readonly string[] Colours = new string[]{ "red", "blue","green"};
    ...
}

You can access them like

var MyColour = new Class1();
Mycolour.Colours.GetValue(0)
  • Related