How do I create a 3D Control Array Handler

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

33 Replies - 863 Views - Last Post: 21 January 2013 - 04:42 PM Rate Topic: -----

#1 AnimeWarrior  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 44
  • Joined: 28-December 12

How do I create a 3D Control Array Handler

Posted 21 January 2013 - 12:48 PM

Object reference not set to an instance of an object.
Error is given when trying to set Pic Location.

        PictureBox[,] Pic = new PictureBox[31, 31];

        public Form1()
        {
            InitializeComponent();
                
                for (int X = 0; X < 32; X++)
                {
                    for (int Y = 0; Y < 32; Y++)
                    {
                        Pic[X, Y].Location = new System.Drawing.Point(400 + (X * 32), 100 + (Y * 32));
                        Pic[X, Y].Size = new System.Drawing.Size(32,32);
                    }
                }
        }



The goal of the above code is to create a control array of 32 by 32 picture boxes and place them on the form to create a 32x32 grid.

Is This A Good Question/Topic? 0
  • +

Replies To: How do I create a 3D Control Array Handler

#2 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: How do I create a 3D Control Array Handler

Posted 21 January 2013 - 12:54 PM

You're not creating your array with enough elements. You're creating the array to be 31 x 31, which starts at 0 as all arrays do so therefore your final element will be at [30, 30]. Your for loops are going to go past that part, and then throw an error because you're counting from 0 to 32. You will need to change either the for loop to search through less elements, or given the fact that you want a 32x32 grid of picture boxes, you need to increase your array to 32 x 32. You will also need to adjust your for loop a bit as it will still try to find an element at 32, 32, but given a 32 x 32 array which starts at 0 the final element would be [31, 31].
Was This Post Helpful? 0
  • +
  • -

#3 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9487
  • View blog
  • Posts: 35,773
  • Joined: 12-June 08

Re: How do I create a 3D Control Array Handler

Posted 21 January 2013 - 12:54 PM

If you put a break point on your 'for loop' and inspect an element of 'Pic' in your watch bar.. what do you see? You see that the collection for the array is created, but nowhere did it imply it would instantiate each and every single object in the array.

So your best bet is to create a temp picturebox object, diddle around with the location and size, and then set the specific pic[x,y] to that temp picture box.
Was This Post Helpful? 0
  • +
  • -

#4 AnimeWarrior  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 44
  • Joined: 28-December 12

Re: How do I create a 3D Control Array Handler

Posted 21 January 2013 - 01:05 PM

View Postmodi123_1, on 21 January 2013 - 12:54 PM, said:

If you put a break point on your 'for loop' and inspect an element of 'Pic' in your watch bar.. what do you see? You see that the collection for the array is created, but nowhere did it imply it would instantiate each and every single object in the array.

So your best bet is to create a temp picturebox object, diddle around with the location and size, and then set the specific pic[x,y] to that temp picture box.


I put a break point were it keeps giving the error. Turns out it happens during the first loop(X=0 Y=0). Ive modified the array to properly make a 32x32 array. I have also looked and each picturebox is set to Null when the loop occurs. Im not sure what more I must do besides:
PictureBox[,] Pic = new PictureBox[33, 33];



Do I need to change Pic to Pic[]?

This post has been edited by AnimeWarrior: 21 January 2013 - 01:06 PM

Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9487
  • View blog
  • Posts: 35,773
  • Joined: 12-June 08

Re: How do I create a 3D Control Array Handler

Posted 21 January 2013 - 01:09 PM

Then stop and look at pic[0,0]! Keep in mind that, as I said, you created the array collection but that *DOES NOT* guarantee instantiated objects for each node in the array. _You_ need to create them.
Was This Post Helpful? 2
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5627
  • View blog
  • Posts: 12,071
  • Joined: 02-June 10

Re: How do I create a 3D Control Array Handler

Posted 21 January 2013 - 01:18 PM

First error covered in this tutorial:
What does this error message mean?

Beyond that learning basic debugging will make the rest of your life a lot easier.

Q: I do x and y happens which I didn't expect but I don't know how to figure out why. How do I debug and find my problem?
A:
Was This Post Helpful? 0
  • +
  • -

#7 AnimeWarrior  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 44
  • Joined: 28-December 12

Re: How do I create a 3D Control Array Handler

Posted 21 January 2013 - 01:27 PM

View Postmodi123_1, on 21 January 2013 - 01:09 PM, said:

Then stop and look at pic[0,0]! Keep in mind that, as I said, you created the array collection but that *DOES NOT* guarantee instantiated objects for each node in the array. _You_ need to create them.


Pic[0,0] is Initiated! Every place I've looked have initiated the Control Array, set the properties, and then added it to the form. All of which I'm currently doing except the last one.

Edit: Im not used to adding controls at runtime. Im used to adding the at design time.

This post has been edited by AnimeWarrior: 21 January 2013 - 01:31 PM

Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5627
  • View blog
  • Posts: 12,071
  • Joined: 02-June 10

Re: How do I create a 3D Control Array Handler

Posted 21 January 2013 - 01:28 PM

View Postmodi123_1, on 21 January 2013 - 02:09 PM, said:

Then stop and look at pic[0,0]! Keep in mind that, as I said, you created the array collection but that *DOES NOT* guarantee instantiated objects for each node in the array. _You_ need to create them.


Physical world analogy:

You open up a car parking lot. It is a place with lots of slots for cars.
You then tell an employee to move the car in slot 4.
Only you haven't actually parked any cars on your lot: Its still just an empty parking lot.

That's what your array is. Its a parking lot for PictureBoxes - but you haven't parked any PictureBoxes yet.

PictureBox[,] Pic = new PictureBox[31, 31]; // Empty parking lot for PictureBoxes

public Form1()
{
    InitializeComponent();
        
        for (int X = 0; X < 32; X++)
        {
            for (int Y = 0; Y < 32; Y++)
            {
PictureBox temp = new Picturebox(); // Make a new PictureBox
Pic[x,y] = temp; // Park the PictureBox on the lot
// Now that something exist there you can change its properties
                Pic[X, Y].Location = new System.Drawing.Point(400 + (X * 32), 100 + (Y * 32));
                Pic[X, Y].Size = new System.Drawing.Size(32,32);
            }
        }
}

Was This Post Helpful? 0
  • +
  • -

#9 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9487
  • View blog
  • Posts: 35,773
  • Joined: 12-June 08

Re: How do I create a 3D Control Array Handler

Posted 21 January 2013 - 01:29 PM

Okay.. then show me a screen shot of the watch window indicating that pic[0,0] has something behind it other than 'null' or 'nothing'.
Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5627
  • View blog
  • Posts: 12,071
  • Joined: 02-June 10

Re: How do I create a 3D Control Array Handler

Posted 21 January 2013 - 01:31 PM

By the way, your loop is going to fail.
You define the loop as having 31 slots (indexes 0-30). Then you try to add 31 items (0-31)

So you either need to increase the size of the array to [32,32].
Was This Post Helpful? 1
  • +
  • -

#11 AnimeWarrior  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 44
  • Joined: 28-December 12

Re: How do I create a 3D Control Array Handler

Posted 21 January 2013 - 01:34 PM

View PosttlhIn`toq, on 21 January 2013 - 01:28 PM, said:

View Postmodi123_1, on 21 January 2013 - 02:09 PM, said:

Then stop and look at pic[0,0]! Keep in mind that, as I said, you created the array collection but that *DOES NOT* guarantee instantiated objects for each node in the array. _You_ need to create them.


Physical world analogy:

You open up a car parking lot. It is a place with lots of slots for cars.
You then tell an employee to move the car in slot 4.
Only you haven't actually parked any cars on your lot: Its still just an empty parking lot.

That's what your array is. Its a parking lot for PictureBoxes - but you haven't parked any PictureBoxes yet.

PictureBox[,] Pic = new PictureBox[31, 31]; // Empty parking lot for PictureBoxes

public Form1()
{
    InitializeComponent();
        
        for (int X = 0; X < 32; X++)
        {
            for (int Y = 0; Y < 32; Y++)
            {
PictureBox temp = new Picturebox(); // Make a new PictureBox
Pic[x,y] = temp; // Park the PictureBox on the lot
// Now that something exist there you can change its properties
                Pic[X, Y].Location = new System.Drawing.Point(400 + (X * 32), 100 + (Y * 32));
                Pic[X, Y].Size = new System.Drawing.Size(32,32);
            }
        }
}


Hmm... I thought the new PictureBox[] would create a picturebox with just the properties null, not make a null picturebox. If this is the case I have no clue how to proceed. I've just started c#. Before I was making control arrays at design time with VB6.
Was This Post Helpful? 0
  • +
  • -

#12 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9487
  • View blog
  • Posts: 35,773
  • Joined: 12-June 08

Re: How do I create a 3D Control Array Handler

Posted 21 January 2013 - 01:37 PM

Quote

PictureBox[] would create a picturebox with just the properties null, not make a null picturebox.

I am not sure why you would think that.. I mean it does the job you told it to - create the collection/array called 'pic'... Zero expectation that it would bloat the memory up with creating each and every object in said array.
Was This Post Helpful? 0
  • +
  • -

#13 AnimeWarrior  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 44
  • Joined: 28-December 12

Re: How do I create a 3D Control Array Handler

Posted 21 January 2013 - 01:39 PM

View Postmodi123_1, on 21 January 2013 - 01:37 PM, said:

Quote

PictureBox[] would create a picturebox with just the properties null, not make a null picturebox.

I am not sure why you would think that.. I mean it does the job you told it to - create the collection/array called 'pic'... Zero expectation that it would bloat the memory up with creating each and every object in said array.


Because instead of saying
Pic[,]



I am specifically saying to make a PictureBox array. Therefore it SHOULD create a PictureBox array not an array of Null.
Was This Post Helpful? 0
  • +
  • -

#14 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9487
  • View blog
  • Posts: 35,773
  • Joined: 12-June 08

Re: How do I create a 3D Control Array Handler

Posted 21 January 2013 - 01:52 PM

Hmm.. odd.. I always had the view that if I create a collection I do not want my memory burgeoned with sub objects of said array to come into existence... you know.. because those objects are different than the array/collection.
Was This Post Helpful? 0
  • +
  • -

#15 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2270
  • View blog
  • Posts: 9,496
  • Joined: 29-May 08

Re: How do I create a 3D Control Array Handler

Posted 21 January 2013 - 01:54 PM

It creates 2D array (with no dimensions) where you can put an instance of a picture box.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3