Click here to Skip to main content
15,063,556 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I'm struggling with my programming assignment. We have to use animation and graphics in C# to demonstrate a scientific principle. I was hoping to do either conservation of momentum or Brownian motion, and either would need 2d 'balls' moving, bouncing off the 'walls' and colliding with each other...so similar code.
I decided to create a Particle class, and use a for loop to instantiate an array of n Particles, each with random x and y starting values.
This part seems to have worked. At least it compiles.
However, I'm not able to access parameters of my Particles array in the way in which I thought I would be able to:
e.g.
//particlesArray is 8 by 1000 array of doubles
//numParticles is previously declined public int and equals 100;
int i;
int xValue;
int yValue;
xValue=Convert.ToInt32(particlesArray[i].x); //this part doesn't work
yValue=Convert.ToInt32(particlesArray[i].y); //this part doesn't work either
// I get the error "The name particles Array does not exist in the current context."

:sigh:
Could anybody help me please?
See full code below:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace ParticleBox
{
    public partial class ParticleBoxForm : Form
    {
        public ParticleBoxForm()
        {
            InitializeComponent();
        }
        int numParticles = 100;
        private Thread trd;
        Graphics g = null;
        Brush myBlackBrush = new SolidBrush(Color.Black);
        int count;

        public void initializeParticlesArray()
        {
            int i;
            Random xRandom = new Random();
            Random yRandom = new Random();
            Random dXRandom = new Random();
            Random dYRandom = new Random();
            int G; // used for testing only, see below.
            Particle[] particlesArray = new Particle[numParticles];
            for (i = 0; i < numParticles; i++)
            {
                particlesArray[i] = new Particle(1.0, 1.0, xRandom.Next(0, particlePictureBox.Width), yRandom.Next(0, particlePictureBox.Height), dXRandom.Next(-1, 1), dYRandom.Next(-1, 1), 0.0, 0.0);
                G = Convert.ToInt32(particlesArray[0].x); // this is here as a test only. It appears to work (no error or red underlines etc.)
            }
        }

        private void ThreadTask()
        {
            int i;
            initializeParticlesArray();
            while (true)
            {
                g = particlePictureBox.CreateGraphics();
                g.Clear(Color.White);
                for (i = 0; i < numParticles; i++)
                {
                    DrawCircle(Convert.ToInt32(particlesArray[i].X), Convert.ToInt32(particlesArray[i].Y), Convert.ToInt32(particlesArray[i].size));
                    ///////////////////////////////////////////
                    //Why can't my array of objects be seen by DrawCircle?

                    Thread.Sleep(10);
                }
            }
        }
        public void DrawCircle(int x, int y, int size)
        {
            Rectangle myRectangle = new Rectangle(x, y, size, size);
            g.FillPie(myBlackBrush, myRectangle, 0, 360);
        }
        private void startButton1_Click(object sender, EventArgs e)
        {
            startButton1.Text = "Start";
            trd = new Thread(new ThreadStart(this.ThreadTask));
            trd.IsBackground = true;
            trd.Start();
        }
        private void particlePictureBox_Paint(object sender, PaintEventArgs e)
        {
        }
        private void stopButton1_Click(object sender, EventArgs e)
        {
            if (trd.IsAlive)
            {
                trd.Abort();
            }
            else
                return;
        }
        private void pauseButton1_Click(object sender, EventArgs e)
        {
            if (trd.IsAlive)
            {
                trd.Suspend();
                startButton1.Text = "Resume";
            }
            else
                return;
        }
        private void numParticlesComboBox1_TextUpdate(object sender, EventArgs e)
        {
            numParticles = Convert.ToInt32(numParticlesComboBox1.Text);
        }
        public class Particle
        {
            public double size = 1.0, mass = 1.0, x = 0.0, y = 0.0, dX = 1.0, dY = 0.0, dPrevX = 0.0, dPrevY = 0.0;
            Random myRandom = new Random();
            bool moving = true;
            private Random xRandom;
            private Random yRandom;
            //bool collide = false; //use for colision handling
            public Particle(double pSize, double pMass, double pX, double pY, double dPX, double dPY, double dPrevPX, double dPrevPY)
            {
                mass = pMass;
                size = pSize;
                x = pX;
                y = pY;
                dX = dPX;
                dY = dPY;
                dPrevX = dPrevPX;
                dPrevY = dPrevPY;
            }
        }
        /*public double xMove()
        {
            if (moving == true)
            {
                x += dX;
                if (x < 0 || x > 600)
                {
                    x *= -1;
                }
                return x;
            }
            else
                return x;
        }
        public double yMove()
        {
            if (moving == true)
            {
                y += dY;
                if (y < 0 || y > 270)
                {
                    y *= -1;
                }
                return y;
            }
            else
                return y;
        }
         */
    }
}


Thanks in advance :thumbsup:,
Pete
Posted
Updated 11-Dec-10 21:53pm
v2
Comments
Abdul Quader Mamun 12-Dec-10 3:54am
   
Spelling check.

This is a scope issue. You can only see particlesArray inside initializeParticlesArray because that is where it is declared. You are attempting to use it inside ThreadTask. Either change where it is declared, or pass it around to the method that needs to use it.
   
Comments
AspDotNetDev 10-Dec-10 19:38pm
   
Also, your code should not compile. You should be seeing a compile-time error message.
Thanks for that. I'm still getting the same NullReferenceException not handled error when I run my thread. Sorry if I haven't quite understood what you meant to change.
My code as it stands now:

MSIL
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace ParticleBox
{
    public partial class ParticleBoxForm : Form
    {
        Particle[] particlesArray;
        int numParticles = 100;
        public ParticleBoxForm()
        {
            InitializeComponent();
            particlesArray = new Particle[numParticles];
        }
        private Thread trd;
        Graphics g = null;
        Brush myBlackBrush = new SolidBrush(Color.Black);
        Random xRandom = new Random();
        Random yRandom = new Random();
        Random dXRandom = new Random();
        Random dYRandom = new Random();
        public void initializeParticlesArray() //this is supposed to be making changes to the object "particlesArray" itself
        {
            int i;
            //define all eight parameters of each object
            for (i = 0; i < numParticles; i++)
            {
                particlesArray[i].mass = 1.0; //this line generates a NullException when initializeParticlesArray(ref particlesArray) is called
                particlesArray[i].size = 10.0;
                particlesArray[i].x = xRandom.Next(0, particlePictureBox.Width);
                particlesArray[i].y = xRandom.Next(0, particlePictureBox.Height);
                particlesArray[i].dX = dXRandom.Next(-1, 1);
                particlesArray[i].dY = dYRandom.Next(-1, 1);
                particlesArray[i].prevDX = 0.0;
                particlesArray[i].prevDX = 0.0;
            }
            return;
        }
        private void ThreadTask()
        {
            initializeParticlesArray(); // is this correct usage?
            /* //I commented this section out until I fix initialization problems
            while (true)
            {
                g = particlePictureBox.CreateGraphics();
                g.Clear(Color.White);
                for (i = 0; i < numParticles; i++)
                {
                    DrawCircle(Convert.ToInt32(particlesArray[i].x), Convert.ToInt32(particlesArray[i].y), Convert.ToInt32(particlesArray[i].size));
                    Thread.Sleep(10);
                }
            }
             */
        }
        public void DrawCircle(int x, int y, int size)
        {
            Rectangle myRectangle = new Rectangle(x, y, size, size);
            g.FillPie(myBlackBrush, myRectangle, 0, 360);
        }
        private void startButton1_Click(object sender, EventArgs e)
        {
            startButton1.Text = "Start";
            trd = new Thread(new ThreadStart(this.ThreadTask));
            trd.IsBackground = true;
            trd.Start();
        }
        private void particlePictureBox_Paint(object sender, PaintEventArgs e)
        {
        }
        private void stopButton1_Click(object sender, EventArgs e)
        {
            if (trd.IsAlive)
            {
                trd.Abort();
            }
            else
                return;
        }
        private void pauseButton1_Click(object sender, EventArgs e)
        {
            if (trd.IsAlive)
            {
                trd.Suspend();
                startButton1.Text = "Resume";
            }
            else
                return;
        }
        private void numParticlesComboBox1_TextUpdate(object sender, EventArgs e)
        {
            numParticles = Convert.ToInt32(numParticlesComboBox1.Text);
        }
        public class Particle
        {
            public double size = 1.0, mass = 1.0, x = 0.0, y = 0.0, dX = 1.0, dY = 0.0, prevDX = 0.0, prevDY = 0.0;
            Random myRandom = new Random();
            //bool moving = true;
            //bool collide = false; //use for colision handling
            public Particle(double pSize, double pMass, double pX, double pY, double dPX, double dPY, double prevDPX, double prevDPY)
            {
                mass = pMass;
                size = pSize;
                x = pX;
                y = pY;
                dX = dPX;
                dY = dPY;
                prevDX = prevDPX;
                prevDY = prevDPY;
            }
        }
        private void applyButton1_Click(object sender, EventArgs e)
        {
        }
    }
}
   
I have it working! Thanks to MSCoder610 over at Anandtech Forums - Programming http://forums.anandtech.com/showthread.php?p=30905770#post30905770[^]


"If you're still hitting the NullReferenceException:
When you declare an array of objects (rather than primitives like int), you still need to create the object for each individual element too. So:"
particlesArray = new Particle[numParticles];
for (int i=0; i<numparticles;>{
  particlesArray[i] = new Particle(...);
}
// use particlesArray


I now have an app that successfully generates n particles at random x and y positions, each with different random dX and dY starting values. On to animation! :laugh:
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900