0

I understand the object reference is required for the non-static field has been asked many times before but reading them, I just didn't understand and I'm just stuck on this code, where the purpose is to generate a new random color each time it is called in a while loop.

I try to call it into my main function but it won't let me, and changing private to static void creates about 5x the issues. Yes I am new, and yes I didn't understand the other answers one bit. Sorry!

Here is all the code: (I know it's terrible, just trying to get it working first)

using System;
using System.Drawing;

namespace Number_things
{
    class Program
    {
        static void Main(string[] args)
        {
            int value;

            Console.Write("Enter a value: ");
            value = Convert.ToInt32(Console.ReadLine());

            while (value < 696969697)
            {

                GetRandomColor();

                for (int i = 1; i <= value; i++)
                {
                    for (int j = 1; j <= i; j++)
                    {
                        Console.Write(i);
                    }
                }

                Console.ReadKey();
            }
        }

        private static Random random = new Random();

        private Color GetRandomColor()
        {
            return Color.FromArgb(
                random.Next(0, 256),
                random.Next(0, 256), 
                random.Next(0, 256));
        }
    }
}
Jodrell
  • 34,946
  • 5
  • 87
  • 124
  • What exactly do you mean by "won't let me"? Can you show the code you're trying to use that it won't let you use? – Lasse V. Karlsen Dec 09 '19 at 14:01
  • 1
    Your function is going to call itself until a stack overflow happens, because of this recursive call: `Color RandomColor = GetRandomColor();` that doesn't have a base case. – Youssef13 Dec 09 '19 at 14:03
  • I see that you have no conditional statement or breaks in your loop. Make sure to update the value of "value" in your loop, otherwise it will run infinitely. Also, set your GetRandomColor() method as static, otherwise you will not be able to access the method from your "static" Main entry point. Refer to https://stackoverflow.com/questions/9924223/static-vs-non-static-class-members on static vs. instance. – Dekryptid Dec 09 '19 at 14:47

2 Answers2

0

You've probably noted you need to keep the Random instance to get new pseudo-random numbers, so I propose you this:

private static Random random = new Random();

private Color GetRandomColor()
{
    return Color.FromArgb(random.Next(256), random.Next(256), random.Next(256));
}

This way you'll get a new Random instance whenever your program starts and you can keep it until it ends executing, without worrying about getting a new one.

EDIT: The problem seems to be you trying to call a private method from outside the class that defines it; try this:

private static Random random = new Random();

private static Color GetRandomColor()
{
    return Color.FromArgb(random.Next(256), random.Next(256), random.Next(256));
}

add this code just after your Main method

Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
  • Shouldn't it be 256 ? See the [Next(Int32, Int32)](https://learn.microsoft.com/en-us/dotnet/api/system.random.next?view=netframework-4.8#System_Random_Next_System_Int32_System_Int32_) documentation. A 32-bit signed integer greater than or equal to minValue and less than maxValue; that is, the range of return values includes minValue but not maxValue. If minValue equals maxValue, minValue is returned. – Youssef13 Dec 09 '19 at 14:05
  • I'm not sure what I'm doing wrong, but it's still giving me the same error when I try to call it. – C1510205203 Dec 09 '19 at 14:06
  • is the while loop you mentioned, is it inside the class that contains the methods above? Can you update the question with all the relevant code? – Rubens Farias Dec 09 '19 at 14:11
  • @C1510205203, add a _static_ in your `GetRandomColor` – Rubens Farias Dec 09 '19 at 14:32
0

Without unsafe code, this works,

using System;
using System.Drawing;

public class Program
{
    public static System.Random Random = new Random();

    static void Main(string[] args)
        {
            // ...
            var color = GetRandomColor();
            // ...
        }

    public static Color GetRandomColor()
    {
        Span<byte> bytes = stackalloc byte[4];
        Random.NextBytes(bytes);
        return Color.FromArgb(bytes[0], bytes[1], bytes[2], bytes[3]);
    }
}

Jodrell
  • 34,946
  • 5
  • 87
  • 124