Vũ Đức Dũng
Vũ Đức Dũng

Reputation: 101

C# : How to assign and retrieve values into member variables of BASE class

I'm a beginner. I'm trying to write a program which has a color class that does various things ( red, green, blue and alpha value) to get the grayscale value for color. But I don't know how to assign a value into a member variable of a Base class.
Firstly, I create a constructor that takes red, blue, green and alpha value like this

private byte red;
private byte green;
private byte blue;
private byte alpha;
public Color(byte red, byte green, byte blue, byte alpha)
{
    this.red = red;
    this.green = green;
    this.blue = blue;
    this.alpha = alpha;
 }

Then I declare a color variable (I want people to be able to input the value)

Color color = new Color(
  Convert.ToByte(
    Console.ReadLine()
  ), Convert.ToByte(
    Console.ReadLine()
  ), Convert.ToByte(
    Console.ReadLine()
  ), 255
);

Is it right? Will the red variable be assigned to the value that user inputs?

If it's right, how can I ask the user before they input ?
For example, before they input the red value, I will ask them:

input your red value

Then I will ask them

input your green value

and they continue to input their values,... etc...

Another problem: I also want to create methods in the color class to get(retrieve) the red, green, blue values from color objects. I created them, but I don't know if it's right. Can you check it for me, please?

public byte Getred(byte red)
{
    return red;
}

public byte Getgreen(byte green)
{
    return green;
}

public byte Getblue (byte blue)
{
    return blue;
}

public byte Getalpha(byte alpha)
{
    alpha = 255;
    return alpha;
}

Upvotes: 1

Views: 1550

Answers (2)

Ghasan غسان
Ghasan غسان

Reputation: 5857

You can use Console.WriteLine to display a prompt message to the user and receive input from them.

Console.WriteLine("Please enter your red value: ");
byte redValue = Convert.ToByte(Console.ReadLine());

Console.WriteLine("Please enter your green value: ");
byte greenValue = Convert.ToByte(Console.ReadLine());

Console.WriteLine("Please enter your blue value: ");
byte blueValue = Convert.ToByte(Console.ReadLine());

Color color = new Color(redValue, greenValue, blueValue, 255);

Your way of getting the values are correct if you want them to be private, and only expose them through specific methods.

Edit:

In case you only want to allow class fields to be changed from within the class, but allow other callers to get the value only, not setting it, then you can use properties, which will save you from writing those get methods.

public class Color
{
    public byte Red { get; private set; }
    public byte Green { get; private set; }
    public byte Blue { get; private set; }
    public byte Alpha { get; private set; }

    public Color(byte red, byte green, byte blue, byte alpha)
    {
        this.Red = red;
        this.Green = green;
        this.Blue = blue;
        this.Alpha = alpha;
    }
}

Color color = new Color(100, 100, 100, 255);
byte redValue = color.Red;
color.Red = 0; // Error, cannot set value outside the class.

Upvotes: 2

dgorti
dgorti

Reputation: 1240

For C#, you might use properties instead of getmethods like

    class MyColor
    {
        private byte red;
        private byte green;
        private byte blue;
        private byte alpha;
        public MyColor(byte red, byte green, byte blue, byte alpha)
        {
            this.red = red;
            this.green = green;
            this.blue = blue;
            this.alpha = alpha;
        }

        public byte Red
        {
            get
            {
                return this.red;
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            byte red;

            while (true)
            {
                Console.WriteLine("Input a byte in the range of 0 to 255 - for Red:");
                string line = Console.ReadLine();
                if (line.Length > 3 || !byte.TryParse(line, out red))
                {
                    Console.WriteLine("Invalid Entry - try again");
                    Console.WriteLine("Input a byte in the range of 0 to 255 - for Red:");
                }
                else
                {
                    break;
                }
            }
        }
    }

Upvotes: 1

Related Questions