2
class Program
{
    static int[] x = {1, 2, 3, 4};
    static int z = 10;
    static int c = 20;
    static int v = 30;
    static int b = 40;

    static void Main(string[] args)
    {
        Console.WriteLine(x[0]);
        Console.ReadLine();

    }



    public Program()
    {
        x = new int[4]{z, c, v, b};

    }

In this code snippet, I'm trying to update an array with different variables through a constructor. However, when I debug, it only prints the original [0] index.

I have a feeling I'm doing something really stupid and I'm just tired, but could anyone tell me why the array is not updating?

5
  • 7
    your constructor is nowhere called there... either you do the changes in a static Program() { } constructor, or you do new Program() to execute the instance constructor
    – metadings
    Jun 30, 2013 at 15:59
  • And then use collections for dynamic issues? That makes sense. Thank you
    – kmartmvp
    Jun 30, 2013 at 16:03
  • @kmartmvp - If it's going to be dynamic why not use a List<T> instead of converting to and from an array? (I assume dynamic refers to changes in the number of items)
    – keyboardP
    Jun 30, 2013 at 16:05
  • I was really just messing around here. When something pops into my head, I hop onto my computer and do it. I'm new to probramming in general, so I often just try and do "problems" that enter my mind. The dynamic List<T> would for sure be better.
    – kmartmvp
    Jun 30, 2013 at 16:06
  • 1
    @kmartmvp - That's fine, I was just wondering if I was missing something :)
    – keyboardP
    Jun 30, 2013 at 16:08

2 Answers 2

8

You're not creating an object of type Program, so the constructor never runs.

1
  • I knew it was something incredibly stupid that I looked over somehow. Wow. Well thank you lol.
    – kmartmvp
    Jun 30, 2013 at 16:01
1

You are creating and operating on static data so you need the static constructor.

        static int[] x = { 1, 2, 3, 4 };
        static int z = 10;
        static int c = 20;
        static int v = 30;
        static int b = 40;

        static void Main(string[] args)
        {
            //since its static data you can easily update array here
            //x = new int[] { z, c, v, b };
            Console.WriteLine(x[0]);
            Console.ReadLine();
        }

        static Program()
        {
            x = new int[4] { z, c, v, b };

        }

this will print out the 10.

This will print 5 and 10,5 of the non-static array and 10 of the static array,by creating a static variable of type program it will call the default constructor.

    class Program
    {
        private static Program MyInstance = new Program();
        int[] y = { 5, 6, 7, 8 };
        static int[] x = { 1, 2, 3, 4 };
        static int z = 10;
        static int c = 20;
        static int v = 30;
        static int b = 40;

        static void Main(string[] args)
        {
            Console.WriteLine(x[0]);
            Console.ReadLine();
        }

        static Program()
        {
            x = new int[4] { z, c, v, b };

        }

        public Program()
        {
            Console.WriteLine(y[0]);
        }

    }

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.