user2028792
user2028792

Reputation:

How to assign a value to an Array in a for- loop

First time the loop is being executed: A value is assigned to PlaatSnoepArray[0,0] and PlaatSnoepArray[0,1].

Second time the loop is being executed: A value is assigned to PlaatSnoepArray[1,0] and PlaatSnoepArray[1,1] AND the values of PlaatSnoepArray[0,0] and PlaatSnoepArray[0,1] are set to 0.

Third time the loop is being executed: A value is assigned to PlaatSnoepArray[2,0] and PlaatSnoepArray[2,1]. AND the values of PlaatSnoepArray[1,0] and PlaatSnoepArray[1,1] are set to 0.

How can i prevent that the values are set back to 0 ?

    static Random Rangen = new Random();
    static void PlaatsSnoep(int aantal)
    {

        for (int i = 0; i < aantal; i++)
        {

            int SnoepX = Rangen.Next(25, 94);
            int SnoepY = Rangen.Next(3, 23);
            Console.SetCursorPosition(SnoepX, SnoepY);
            Console.WriteLine("0");

            int[,] PlaatssnoepArray = new int[aantal,2];

            PlaatssnoepArray[i, 0] = SnoepX;
            PlaatssnoepArray[i, 1] = SnoepY;

        }

Upvotes: 0

Views: 2011

Answers (5)

Steve
Steve

Reputation: 216293

You are initializing your array inside the loop, move it out

static Random Rangen = new Random();
static void PlaatsSnoep(int aantal)
{

    int[,] PlaatssnoepArray = new int[aantal,2];
    for (int i = 0; i < aantal; i++)
    {

        int SnoepX = Rangen.Next(25, 94);
        int SnoepY = Rangen.Next(3, 23);
        Console.SetCursorPosition(SnoepX, SnoepY);
        Console.WriteLine("0");


        PlaatssnoepArray[i, 0] = SnoepX;
        PlaatssnoepArray[i, 1] = SnoepY;

    }
}

And what about using a List<Point>() instead of an array?

    List<Point> PlaatssnoepList = new List<Point>(); 
    for (int i = 0; i < aantal; i++)
    {
        Point p = new Point(Rangen.Next(25, 94), Rangen.Next(3, 23));
        Console.SetCursorPosition(p.X, p.Y);
        Console.WriteLine("0");
        PlaatssnoepList.Add(p)
    }

Upvotes: 0

Habib
Habib

Reputation: 223247

Your array declaration is inside the loop, move it outside.

int[,] PlaatssnoepArray = new int[aantal,2];
for (int i = 0; i < aantal; i++)
    {
        int SnoepX = Rangen.Next(25, 94);
        int SnoepY = Rangen.Next(3, 23);
        Console.SetCursorPosition(SnoepX, SnoepY);
        Console.WriteLine("0");
        PlaatssnoepArray[i, 0] = SnoepX;
        PlaatssnoepArray[i, 1] = SnoepY;

    }

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726539

How can i prevent that the values are set back to 0 ?

You need to move the creation of the PlaatssnoepArray outside the loop. Currently, each iteration assigns to its own instane of int[aantal,2], which goes out of scope and gets thrown away as soon as the loop iteration is over.

int[,] PlaatssnoepArray = new int[aantal,2];
for (int i = 0; i < aantal; i++)
{
    // The rest of your code
}

Upvotes: 1

Simon Whitehead
Simon Whitehead

Reputation: 65079

Take your array out of the loop..

Upvotes: 0

Henrik
Henrik

Reputation: 23324

Create the array outside the for loop:

    int[,] PlaatssnoepArray = new int[aantal,2];

    for (int i = 0; i < aantal; i++)
    {

        int SnoepX = Rangen.Next(25, 94);
        int SnoepY = Rangen.Next(3, 23);
        Console.SetCursorPosition(SnoepX, SnoepY);
        Console.WriteLine("0");

        PlaatssnoepArray[i, 0] = SnoepX;
        PlaatssnoepArray[i, 1] = SnoepY;

    }

Upvotes: 1

Related Questions