0

In my program I am creating PictureBoxes which move across the screen. I want to make it so that the user can create as many of these as they want. To do this I created a class is assigned to a picturebox after it is created and controls it's movement. This works for the picturebox until I create another one, when it is no longer controlled. I assume this is because c# does not allow me to create multiple objects of a class, and therefore ends the previous ones to make a new one. Here's how I've done it:

static ArrayList cps = new ArrayList();

public void ShootCannon() {
    Image cubeImage= Image.FromFile("C:\\Users\\Stefan\\Documents\\Visual Studio 2010\\Projects\\Game1\\Game1\\Resources\\CannonCube.png");
    PictureBox cannonCube = new PictureBox();
    ScreenPanel.Controls.Add(cannonCube);
    cannonCube.Image = cubeImage;
    cannonCube.SetBounds(cannonCubeInst.X, cannonCubeInst.Y, cubeImage.Width, cubeImage.Height);
    cannonCube.BringToFront();

    cps.Add(new CubeProjectile(cannonCube));
}

And the CubeProjectile class is:

public class CubeProjectile
{

    static PictureBox box;

    public CubeProjectile(PictureBox box1)
    {
        box = box1;
        Timer Update = new Timer();
        Update.Interval = 1;
        Update.Tick += new EventHandler(Timer_Tick);
        Update.Start();
    }

    void Timer_Tick(object sender, EventArgs e)
    {
        Point loc = new Point(box.Location.X, box.Location.Y);
        box.SetBounds(loc.X + 1, loc.Y, box.Width, box.Height);
    }

}
1
  • 2
    'c# does not allow me to create multiple objects of a class' What are you talking about? PictureBox is not singleton. Fix your private static PictureBox box. Commented Feb 8, 2014 at 13:07

2 Answers 2

2

You can create how many instances of a class you want.

The problem is that you are using a static variable inside the class. That only exists once, no matter how many instances you create. When you create the second instance it will overwrite the value in the static variable with the new picture box.

You need an instance variable to hold one picture bx per instance of the class.

Change this:

static PictureBox box;

to:

PictureBox box;

Side note: A good practice is to specify the access level for members of the class, and only make those public that you want to access from outside the class. Making your member variable private makes sure that it's only accessed from that instance:

private PictureBox box;
Sign up to request clarification or add additional context in comments.

Comments

0

A static variable declared in a class is common to all instances of that class. So your PictureBox box is overwritten with the latest picturebox that you pass to the constructor of your class

So if you use your code above and call

 CubeProjectile cb1 = new CubeProjectile(pic1);
 CubeProjectile cb2 = new CubeProjectile(pic2);

both instances of CubeProjectile share the latest picturebox passed to the constructor pic2

Instead if you declare the variable without the static modifier, every instance of the class has its own PictureBox

public class CubeProjectile
{

    private PictureBox box;

    public CubeProjectile(PictureBox box1)
    {
        box = box1;
        Timer Update = new Timer();
        Update.Interval = 1;
        Update.Tick += new EventHandler(Timer_Tick);
        Update.Start();
    }

    void Timer_Tick(object sender, EventArgs e)
    {
        Point loc = new Point(box.Location.X, box.Location.Y);
        box.SetBounds(loc.X + 1, loc.Y, box.Width, box.Height);
    }

}

 CubeProjectile cb1 = new CubeProjectile(pic1);
 CubeProjectile cb2 = new CubeProjectile(pic2);

Now cb1 has the reference to pic1 stored in the internal variable box and cb2 references the pic2

However, keep in mind that a PictureBox is the host of a possible expensive resource (The Image property) and you should be sure to dispose these instances of CubeProjectile when no more needed

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.