Resolved static field in abstract class not intializing

aronmatthew

Well-known member
Joined
Aug 5, 2019
Messages
76
Programming Experience
Beginner
it appears that declaring and initializing a static reference type compiles ok is not initializing at run time. a silent but deadly bug. im not sure to initialize these fields in the class constructor or hard code the initial values into property accessor. since in this case is need hard code the value.
 
Last edited by a moderator:
Please show us your code in code tags, not as screenshots.

If possible post some minimal code that demonstrates the problem.
 
The following code works just fine. It correctly prints out "Color [Green]" due to line 11. If you uncomment line 1, then line 9 will be use instead of line 11, and the output is "Color [Empty]".

C#:
// #define DO_NOT_INIT

using System;
using System.Drawing;

abstract class Vehicle
{
#if DO_NOT_INIT
    protected static Color s_color;
#else
    protected static Color s_color = Color.Green;
#endif
}

class Boat : Vehicle
{
    public Boat()
    {
        Console.WriteLine(s_color);
    }
}
                   
public class Program
{
    public static void Main()
    {
        var boat = new Boat();
    }
}

 
Last edited:
You might be in the situation where there is bug where you overwrite the value of the static field like in the case of the F117 class here.
C#:
using System;
using System.Drawing;

abstract class Vehicle
{
	protected static Color s_color = Color.Green;
}

class Boat : Vehicle
{
	public Boat()
	{
		Console.WriteLine(s_color);
	}
}

class F117 : Vehicle
{
	public F117()
	{
		s_color = Color.Black;
		Console.WriteLine(s_color);
	}
}
					
public class Program
{
	public static void Main()
	{
		var boat = new Boat();
		var f117 = new F117();
		var boat2 = new Boat();
	}
}
Which results in the output of:
Code:
Color [Green]
Color [Black]
Color [Black]

 
And the way to protect yourself from that kind of evil is to mark the field as readonly. Make line 6:
C#:
protected static readonly Color s_color = Color.Green;
so that you'll get a compilation error for line 21:
Code:
Compilation error (line 21, col 3): A static readonly field cannot be assigned to (except in a static constructor or a variable initializer)
 
@aronmatthew : And now you see now with your original post why we want you to post your code here as text, rather than as screenshots or links to external: when that external source goes away, people who come to the thread later cannot see what you are talking about.
 
Last edited:
I believe the case is as stated initially where simply declaring such a static value for type such as color results in some type of default value I will post example
 
here in an abstract class used for static fields required properties in order for values to be initialized with types such as Color. I created these properties only because there was a problem. Although in an example I just created there does not appear to be a problem with a static color initialization but in this case obviously there was a problem.
C#:
using debug1.lines.lines;

namespace debug1.forms.main.Fmain
{
    public abstract class AFmain
    {
        //text settings
        private static float keyboardFontScalar;
        private static float operatorFontScalar;
        private static float subscriptFontScalar;
        private static Color textColor;
        private static Color textBackColor;
        private static Font operatorFont;
        private static Font subscriptFont;
        private static Font keyboardFont;


        //lineBlock settings
        private static float rectangleScalar;
        //line settings
        private static float lineHeight;
        private static Color currentLineColor;
        //cursor settings
        private static float cursorScalar;
        private static float cursorWidth;
        private static int cursorBlinkInterval;
        private static SizeF cursorSize;

        //panel settings
        private static float pad;
        private static Color panelColor;

        public static float KeyboardFontScalar
        {
            get { return .56F; }

        }
        public static float SubscriptFontScalar
        {
            get { return .36F; }
          
        }
        public static float OperatorFontScalar
        {
            get { return .6F; }
            // get { return .36F; }
            // set { fontScalar = value; }
        }
      
        public static Color TextColor
        {

            get { return Color.Black; }
           // set { textColor = value; }
        }
        public static Color TextBackColor
        {

            get { return Color.Gray; }
           // set { textBackColor = value; }
        }
        public static Font OperatorFont
        {

            get {
                return new Font(FontFamily.GenericSansSerif,
LineHeight* OperatorFontScalar, FontStyle.Regular);
            }
           // set { operatorFont = value; }
        }
        public static Font SubscriptFont
        {
            get {
                return new Font(FontFamily.GenericSansSerif,
OperatorFont.Size*SubscriptFontScalar, FontStyle.Regular);
            }
            //set { subscriptFont = value; }
        }
        public static Font KeyboardFont
        {
            get
            {
                return new Font(FontFamily.GenericSansSerif,
OperatorFont.Size * KeyboardFontScalar, FontStyle.Regular);
            }
            //set { subscriptFont = value; }
        }
        public static float RectangleScalar
        {

            get { return .40F; }
           // set { rectangleScalar = value; }
        }
        public static float LineHeight
        {

            get { return 70F; }
            //set { lineHeight = value; }
        }

        public static Color CurrentLineColor
        {

            get { return Color.LimeGreen; }
            //set { currentLineColor = value; }
        }
        public static float CursorScalar
        {

            get { return .90F; }
            //set { cursorScalar = value; }
        }
        public static float CursorWidth
        {

            get { return 3F; }
            //set { cursorWidth = value; }
        }
        public static int CursorBlinkInterval
        {

            get { return 300; }
            //set {  cursorBlinkInterval= value; }
        }
        public static SizeF CursorSize
        {

            get { return new SizeF(CursorWidth, LineHeight ); }
            //set { cursorSize = value; }
        }
        public static Color CursorColor
        {

            get { return Color.Black; }
            //set { cursorColor = value; }
        }
        public static float Pad
        {
            get { return 5F; }
            //set { pad = value; }
        }
        public static Color PanelColor
        {

            get { return Color.White; }
           // set { panelColor = value; }
        }
    }
}
 
since installing the latest free version of visual studion it says "preview" on it and have been having a few problems such as needing to clean and build before running for the current code to be compiled
 
There's way too much code in post #9. Post #2 said:
post some minimal code that demonstrates the problem
It just makes it harder for us for no good reason if we have to wade through irrelevant code to find what actually matters. You seem to always make the minimum of effort and then expect us to make up the slack. If you'd like us to help you, make the effort to help us do so.
 
There's way too much code in post #9. Post #2 said:

It just makes it harder for us for no good reason if we have to wade through irrelevant code to find what actually matters. You seem to always make the minimum of effort and then expect us to make up the slack. If you'd like us to help you, make the effort to help us do so.

Visual Studio "Preview" must mean its lacking even more than the previous community edition. Since installed, many times it has ran code that does not build. A problem that I never had with any previous version. Which I suppose could have resulted in this misunderstanding. But in fact it is a social problem of sort to solve. Although it may be similar to asking someone what happened at the dentist or in court. Rather than reality. I apologize and will do my best to not panic next time they get away with it.
 
The preview versions aren't lacking anything that earlier versions have, other than deprecated features that have been removed from all future versions. They are basically a beta of the next version, so they may still contain bugs and incomplete new features. Unless you specifically want to live on the edge and experience new features ASAP, don't install any preview versions. Stick to the latest stable version.
 
Back
Top Bottom