Thread: Inheritence

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428

    Inheritence

    Well my google skills have failed me this time as I can't seem to figure out what I'm missing when it comes to inheritence in C#. I have class called Particle Engine. It initiates all its variables to a default then the user can call a series of Methods to define behaviors, colors, textures, ect for the particles, or you can leave it to its default initiated parameters.

    Next step I wanted to create a class called explosion that really should just be a subclass of the particle engine. I realized that all I really needed to do with this subclass was override it's MakeNewParticle() call and add a loop that created its max number of particles if there were no particles on the screen that it owned.

    First problem was when I realized that constructors weren't inherited. My compiler (VS 2008) complained that I was calling the PartEngine class with 0 arguments. So I read an article that said I needed to declare the particular constructor as a base.

    Code:
    public PartEngine(int num, Vector2 Pos, Texture2D Tex, Direction allowedDir) : base()
    {
                AllowedDirection = allowedDir;
                ThisAccelRate.AccelRate = 0.0f;
                ThisAccelRate.BeingUsed = false;
                ThisDefaultColor.BeingUsed = false;
                ThisDefaultColor.ColorToUse = Color.White;
                EmitterPos = Pos;
                NumOfParticles = num;
                TheseParticles = new Particle[NumOfParticles];
                random = new Random();
                for (int loopcontrol = 0; loopcontrol < NumOfParticles; loopcontrol++)
                {
                    TheseParticles[loopcontrol] = new Particle(Tex, Vector2.Zero, Color.White, Vector2.Zero, 0.0f, 0.0f, 0.0f, 0);
                }
            }
    Then the subclass was supposed to contain a constructor like

    Code:
    public Explosion(int num, Vector2 Pos, Texture2D Tex, PartEngine.Direction allowedDir)
                : base(num, Pos, Tex, allowedDir)
            {
            }
    From what I understand this should initiate the Subclass in the exact same mannar as the class would by this call. This is where I know my understanding is breaking down because things fall apart here. All the sudden I get compiler errors complaining that I'm accessing indices in arrays that aren't valid the original class. My suspicions lead me to think that my initiation of the subclass might be the cause.
    Code:
    ThisExplosion = new Explosion(1000, new Vector2(0, 0), PartTex, PartEngine.Direction.All);
    Somehow I think this isn't passing 1000 to the value MaxNumOfParticles inside the subclass before the update call accesses it's particles in the update. It might be important to note that MaxNumOfParticles is a static variable used to define the size of the array Particles[MaxNumOfParticles];
    Code:
    // this is the actual subclass code
       class Explosion : PartEngine
        {
            public Explosion(int num, Vector2 Pos, Texture2D Tex, PartEngine.Direction allowedDir)
                : base(num, Pos, Tex, allowedDir)
            {
            }
            public override void MakeNewParticle(Texture2D parTex)
            {
                for(int X = 0; X < base.MaxNumberOfParticles(); X++)
                    base.MakeNewParticle(parTex);
            }
        }
    The compiler points to code in the original class that updates all the particles that the ParticleEngine owns. It contains that loop that runs on logic like
    Code:
    for(int Loopcontrol = 0; Loopcontrol < MaxNumberOfParticles;   Loopcontrol++)
    {
      if(Particle[Loopcontrol].TimeToLive > 0) // <-- this is the line that is highlighted as accessing an incorrect indice
        Particle.Update();
    }
    The error says that the program is trying to access an indice outside of the particle array, but all controls are based on the MaxNumberOfParticles integer that is SUPPOSED to be defined in the constructor as int num. I am sure I am just missing something simple about initiation orders and how C# is handling this subclass.

    Thanks in advance for your time in reading this and helping me to solve this issue. I appreciate your time.
    Last edited by Lesshardtofind; 11-30-2012 at 04:45 AM.
    Virtual reality hello world http://www.rodneybrothers.com/vr/vrh...rld/index.html in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with Inheritence
    By sprankles in forum C++ Programming
    Replies: 2
    Last Post: 04-23-2011, 01:01 PM
  2. inheritence problem
    By mouse666666 in forum C++ Programming
    Replies: 3
    Last Post: 11-27-2010, 02:44 PM
  3. Abstract inheritence
    By Chris87 in forum C++ Programming
    Replies: 6
    Last Post: 06-18-2009, 10:47 AM
  4. Question about Inheritence
    By chadmandoo in forum C++ Programming
    Replies: 22
    Last Post: 11-30-2008, 12:50 PM
  5. Inheritence and operator[]
    By nempo in forum C++ Programming
    Replies: 6
    Last Post: 11-06-2007, 04:58 PM