views:

225

answers:

3

so with this class i have

public class Options
    {
        public bool show { get; set; }
        public int lineWidth { get; set; }
        public bool fill { get; set; }
        public Color fillColour { get; set; }
        public string options { get; set; }

        public virtual void createOptions()
        {
            options += "show: " + show.ToString().ToLower();
            options += ", lineWidth: " + lineWidth;
            options += ", fill: " + fill.ToString().ToLower();
            options += ", fillColor: " + (fillColour != Color.Empty ? ColourToHex(fillColour) : "null");
        }

        public Options(bool _show, int _lineWidth, bool _fill, Color _fillColour)
        {
            show = _show;
            lineWidth = _lineWidth;
            fill = _fill;
            fillColour = _fillColour;

            createOptions();
        }
}

and another class that inherits it

public class Line : Options
    {
        public static bool steps { get; set; }

        public override void createOptions()
        {
            options += ", lines: {";
            options += " steps: " + steps.ToString().ToLower() + ",";
            base.createOptions();
            options += "}";
        }

        public Line(bool _show, int _lineWidth, bool _fill, Color _fillColour, bool _steps)
            : base(_show, _lineWidth, _fill, _fillColour) { steps = _steps; }
    }

When calling the object Line(true, 1, true, Color.Gray, true) it does the override of the inherited class function first, then sets steps to true.

I want steps to be included in the override so steps will now be true instead of false(its default).

If its possible please gimme some pointers and tips on how to fix this and explain to me why my setup will not allow for the override to happen after the constructor init.

A: 

I suppose you forgot to show us the Options class constructor, but I guess it calls the createOptions() method.

In C# you just can't call a base constructor wherever you want. The base constructor is always called before the given constructor starts. You may refer to this url to know more about constructor chaining:

http://www.yoda.arachsys.com/csharp/constructors.html

But you can get what you want by changing your architecture a little. As I see this, your options property is a ToString() of the Options class. You do not need this property to be ready at constructor time.

Some options:

  1. Convert the Options class to string in the options get accessor.
  2. Eliminate the options property and, instead, implement a toString method that would call createOptions or just it's content
Ciwee
awesome link really helped me i changed the public static bool to not be static it didnt need to be. i pretty much called the override function inside my inherited class constructor and that fixed my issue i noticed that it was being called in my base class i forgot to post that part of it in my above example.
Ayo
A: 

I don't see the constructor of the Options class in the code, but I guess that it first copies the four given parameters to the corresponding fields and then calls createOptions to initialize the options field. This is actually a very good example of why it's bad practice to call virtual methods in constructor. There is no way to set any field of the derived class until the base class's constructor is done, so the overridden method will use the default value of the steps field.

There are many ways you can fix this, but the first thing you should do is avoid calling virtual methods in constructor. The way I would do this is make options property read-only and make its getter virtual, so that it calculates the value when it's needed.

Edit. I didn't notice that steps is static. Then I don't understand why do you set it in instance constructor.

A: 

So here are some tips when working with static class members like public static bool steps:

  1. Static members are initialized to their default values (false in this case) when the class is loaded for the first time. Remember that static members belong to the class and not the instance so they cannot be overridden, but can be hidden in derived classes by using the new keyword.

  2. You can use a special constructor called a static constructor to explicitly initialize static class members... so you shouldn't try to initialize them in an instance constructor like your code snippet illustrates.

A quick Google search on C# static members might be helpful: Link to C# Help on Static Members

Matt