Thread: Creating object arrays for VC++

  1. #16
    Registered User
    Join Date
    Feb 2006
    Posts
    18
    Quote Originally Posted by SlyMaelstrom
    Why are you including both cmath and math.h? I'd imagine if you opened math.h, you would see a small library that includes cmath and directs the use of all of it's functions under the std namespace.

    Anyway, I don't know the API you are using, so I'm not familiar with the add() method you have there. Does it take a PictureBox or a PictureBox*? If it's the latter, then use the second code I gave you.
    The code I originally posted was something my professor gave to us and later said, "Oh, this doesn't work". So, I don't even have to use that. I can start from scratch with the whole array thing. What I am trying to do is add x number of objects to a panel when I click a button. I then want to be able to reference them anywhere else in the code as individual object so I can easily change the properties.

    like this:
    picBlock[1]->BackColor = Color::Black;
    picBlock[4]->Left = 100;
    etc.

    These item should appear in a panel in my program. The panel name is pnlGameField. And I just want to call each item picBlock[i].

  2. #17
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Ah, managed C++, I'm pretty sure there are a ton of differnces in that. Didn't realize what you were working with.
    Sent from my iPadŽ

  3. #18
    Registered User
    Join Date
    Feb 2006
    Posts
    18
    Quote Originally Posted by ZuK
    There seem to be very different rules for pointer arrays in "managed C++" code. Guess you should look for a forum that specifically deals with this kind of "C++ extension"
    Kurt
    Are there any good VC++ forums that you can recommend. I don't normally frequent programming forums (as you can see by my whopping 12 posts)?

  4. #19
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I know practically nothing about managed extensions but did you try this:
    Code:
          PictureBox* picBlocks[] = new PictureBox*[5];
    
          for(int i = 0; i < 5; i++)
          {
            picBlocks[i] = new PictureBox();
            pnlGameField->Controls->Add(picBlocks[i]);
          }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #20
    Registered User
    Join Date
    Feb 2006
    Posts
    18
    Quote Originally Posted by SlyMaelstrom
    Ah, managed C++, I'm pretty sure there are a ton of differnces in that. Didn't realize what you were working with.
    I'm using Visual C++ .NET to create a Windows Forms Application.

  6. #21
    Registered User
    Join Date
    Feb 2006
    Posts
    18
    Quote Originally Posted by JaWiB
    I know practically nothing about managed extensions but did you try this:
    Code:
          PictureBox* picBlocks[] = new PictureBox*[5];
    
          for(int i = 0; i < 5; i++)
          {
            picBlocks[i] = new PictureBox();
            pnlGameField->Controls->Add(picBlocks[i]);
          }
    Amazing!! That works perfectly. Thank you so much. My teacher has been really lazy and doesn't seem to want to help us out. So, I have to figure this stuff out myself. This is all I needed to keep going with my project.

    Thanks everybody for helping me out too!! It is much appreciated.

    Aaron

  7. #22
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Ok, well try JaWiB's code, which is probably a better bet than what I wrote because I assumed you wanted to pass a pointer to a PictureBox. If that doesn't work, then god help Visuall C++ .NET.
    Sent from my iPadŽ

  8. #23
    Registered User
    Join Date
    Feb 2006
    Posts
    18
    Quote Originally Posted by SlyMaelstrom
    Ok, well try JaWiB's code, which is probably a better bet than what I wrote because I assumed you wanted to pass a pointer to a PictureBox. If that doesn't work, then god help Visuall C++ .NET.
    I do appreciate the help. I have one other question. When I try to use it in another function, it is saying that it's an undeclared identifier.

    Do I need to reference the function or make it public where it was created or somewhere else?

    Code:
    public: System::Void menuFile_Click(...)
    {
       Original Code
    }
    
    bool OtherFunction()
    {
       if(picBlocks[0] = xxxx)
       {
          Blah Blah Blah
       }
    }
    Last edited by tineras; 05-06-2006 at 04:51 PM.

  9. #24
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    If that were C++ I would suggest to put the declaration
    Code:
    PictureBox* picBlocks[];
    as a private member into the class and initialize it in the constructor
    Code:
    picBlocks = new PictureBox*[5];
    Then I would create the PictureBoxes as you do now.
    BTW: I still think that you should try to find a way to access the PictureBoxes via pnlGameField->Controls. That seems to be some kind of container (it has a Add method ) and it should have some Get or Find method as well. Normally it is not a good Idea to store objects in more then one place. It can become quite difficult to keep the containers synchronous.
    Kurt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating A Thread W/ PThreads within an Object
    By HalNineThousand in forum Linux Programming
    Replies: 12
    Last Post: 03-28-2008, 02:57 PM
  2. Object destroy itself?
    By cminusminus in forum C++ Programming
    Replies: 28
    Last Post: 03-27-2008, 01:08 AM
  3. Deleting Dynamic array's of an object
    By j0hnb in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2004, 03:25 PM
  4. Replies: 3
    Last Post: 12-03-2001, 01:45 PM
  5. Creating an array of object pointers
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-08-2001, 10:01 PM