Like Tree1Likes
  • 1 Post By nvoigt

Inheritence

This is a discussion on Inheritence within the C# Programming forums, part of the General Programming Boards category; Well my google skills have failed me this time as I can't seem to figure out what I'm missing when ...

  1. #1
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    404

    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 03:45 AM.

  2. #2
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    404
    I spend two days on a problem and 10 minutes after I ask for help I finally figure out the answer. Irony at its best.
    I made all the variables in the original class public and then I added the initiation code of the original class into the subclass
    Code:
     class Explosion : PartEngine
        {
            public Explosion(int num, Vector2 Pos, Texture2D Tex, PartEngine.Direction allowedDir)
                : base(num, Pos, Tex, allowedDir)
            {
                base.AllowedDirection = allowedDir;
                base.ThisAccelRate.AccelRate = 0.0f;
                base.ThisAccelRate.BeingUsed = false;
                base.ThisDefaultColor.BeingUsed = false;
                base.ThisDefaultColor.ColorToUse = Color.White;
                base.EmitterPos = Pos;
                base.NumOfParticles = num;
                base.TheseParticles = new Particle[NumOfParticles];
                base.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);
                }
            }
            public override void MakeNewParticle(Texture2D parTex)
            {
                for(int X = 0; X < base.MaxNumberOfParticles(); X++)
                    base.MakeNewParticle(parTex);
            }
        }
    Is there a better solution than making all these variables public so you can save to them? I like to keep things on a need to know basis in my programs. If a class can handle its data without the parents knowing or accessing then I prefer that solution as it narrows down my possibilities for bugs. Secondly if I have to reinitiate this data in the subclass what is the point of calling the base constructor in the subclass constructor at all?

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    If you call base(), the base constructor will be executed. So your second example code should be working. However, you mentioned a static variable. Statics and inheritance is a train wreck waiting to happen. Get rid of the static.
    Lesshardtofind likes this.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Why are you re-initializing all of your variables? You can just do it once in the base constructor.

    Code:
    class BaseClass
    {
        public int SomeNumber { get; set; }
        public int SomeOtherNumber { get; set; }
    
        public BaseClass(int someNumber)
        {
            SomeNumber = someNumber;
            SomeOtherNumber = 5;  // Some default value
        }
    }
    
    class SubClass : BaseClass
    {
        public int MyOwnNumber { get; set; }
    
        public SubClass(int someNumber, int myOwnNumber)
            : base(someNumber)
        {
            MyOwnNumber = myOwnNumber;
        }
    }
    
    {
        SubClass obj = new SubClass(10, 7);
        Console.WriteLine(obj.SomeNumber);       // 10
        Console.WriteLine(obj.SomeOtherNumber);  // 5
        Console.WriteLine(obj.MyOwnNumber);      // 7
    }
    Last edited by itsme86; 11-30-2012 at 03:33 PM.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    404
    I explained above maybe not clearly, but I assumed what you are saying would work but it did not. There was a class array that was not being defined before it was used by using the base constructor, but then as he said the static variable + inheritance may have been the issue since the static variable was used to set the size of the particle class array inside the particle engine. I felt like re defining the variables twice was redundant so I am guessing the fact that I had to change from a static to a public in order to use the base.numofparticles = num was probably the actual fix. I will adjust the code in a few when I finally get home from work and post back if that static variable was the real root.

  6. #6
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    404
    So I went back into the program and commented out the code that you suggested was redundant and the error came back immediately. Any suggestions? It seems for the time being I am just going to have to double initiate these values. Anyone else ever ran into issues with class arrays in a base class that are given a size by an argument through the contructor of the subclass?

  7. #7
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    I'm not sure what to tell you... get rid of that static. You can find a very crude hack around it so it will work now and blow up in your face another time... but the solution would be to not initialize an array using a static variable. Why isn't it a const or a class variable?

    By the way... you can set breakpoints into both constructors and step through them to see where the code fails.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  8. #8
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    404
    I got rid of the static on the second post before you mentioned it, but it is good to know for the future it causes problems. As for this particular case I I know where the code fails but by all explanations it should not fail there. It says that the base class accesses an indice outside its array... Which it does not so tha means that the base constructor wasn't working properly which is why I doubled up the initiation code and now it works but it seems redundant.

  9. #9
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    Well, put a breakpoint there. It seems you are guessing a lot. Put a breakpoint in the line of the array initialization and find out.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  10. #10
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    404
    Well, put a breakpoint there.
    Thanks for that I am new to VS and had not yet learned that trouble shooting feature. That will be a big help in the future.

    It seems you are guessing a lot.
    Indeed I am. The ability to make an intelligent guess usually makes a difference. Just look at good vs great sudoku solvers.

    I knew the problem was on my end somewhere but I didn't know where so I started at the top and just went line by line through all my code cleaning anything un-neccesary and commenting through the lines so I could retrace my thoughts. I found a couple corners of the program that could be more intuitive and by the end of all the changes the problem dissapeared. I'm not sure where exactly the issue was but it was definitely my fault. Thank you both for your assistance. Knowing the breakpoint tool and that static + inheritance are a bad idea should definitely help me in the future.

  11. #11
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    404
    Kinda lame but you can see them in action here when the enemies get shot.

    Spaceship game prototype. - YouTube

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, 01: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, 11:50 AM
  5. Inheritence and operator[]
    By nempo in forum C++ Programming
    Replies: 6
    Last Post: 11-06-2007, 03:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21