none
Static class CS0414 Error RRS feed

  • Question

  • I am getting a CS0414 warning "the field b0Hour is assigned but it is never used error".  I would like to have static subroutine use static variables.  From searches online I can set it up to ignore these errors but don't want to in case some where in the program I make a mistake.  Is there a way to properly setup to use these variables?   I tried using properties, but that doesn't seem to help.

      // =======================================================================================================
        internal static class Class_Common
        // =======================================================================================================
        {
            internal static ConcurrentQueue<string> cqExceptions = new ConcurrentQueue<string>();
            #region "TimeCheck"     
            internal static bool b0Hour { get; set; }
            internal static bool b1Hour = false;
            internal static bool b2Hour = false;
            internal static bool b3Hour = false;
            internal static bool b4Hour = false;
            internal static bool b5Hour = false;
            internal static bool b6Hour = false;
            internal static bool b7Hour = false;
            internal static bool b8Hour = false;
            internal static bool b9Hour = false;
            internal static bool b10Hour = false;
            internal static bool b11Hour = false;
            internal static bool b12Hour = false;
            internal static bool b13Hour = false;
            internal static bool b14Hour = false;
            internal static bool b15Hour = false;
            internal static bool b16Hour = false;
            internal static bool b17Hour = false;
            internal static bool b18Hour = false;
            internal static bool b19Hour = false;
            internal static bool b20Hour = false;
            internal static bool b21Hour = false;
            internal static bool b22Hour = false;
            internal static bool b23Hour = false;
            internal static int iHour = 0;
            internal static int iMinute = 0;
            #endregion
            // =======================================================================================================
            // =======================================================================================================
            #region "Common"
            // ======================================================================================================
            internal static void myTimeCheck()
            {
                try
                {
                    DateTime currentTime = DateTime.Now;
                    int iHour = currentTime.Hour;
                    int iMinute = currentTime.Minute;
                    switch (iHour)
                    {
                        case 0:
                            {
                                b23Hour = false;
                                b0Hour = true;
                                break;
                            }
                        case 1:
                            {
                                b0Hour = false;
                                b1Hour = true;
                                break;
                            }
                        case 2:
                            {
                                b1Hour = false;
                                b2Hour = true;
                                break;
                            }
                        case 3:
                            {
                                b2Hour = false;
                                b3Hour = true;
                                break;
                            }
                        case 4:
                            {
                                b3Hour = false;
                                b4Hour = true;
                                break;
                            }
                        case 5:
                            {
                                if (b5Hour == false)
                                {
                                    DeleteOldFiles(7, "*.txt");
                                }
                                // If iMinute = 30 Or iMinute = 31 Or iMinute = 32 Then
                                // End If
                                b4Hour = false;
                                b5Hour = true;
                                break;
                            }

    Saturday, November 16, 2019 6:55 PM

All replies

  • Yeah the message is correct becuase b0Hour, which is  unlike the other variables that are initially set outside of conditional 'case' statements and used, is never used outside of a 'case' statement logic.

    Any conditional logic like case and if statements where  logic is conditionally executed is going to produce the compiler message as the compiler ignores any variable or object being used  within the conditional statement in determining if the object or variable is ever used.

    How  does the compiler know that a condition is going to be met to execute the logic within the conditional statement? The compiler can't know when or if conditional logic will ever be met and code executed, right? 

    The below line should stop the compiler message.

    internal static bool b0Hour { get; set; } = false; //it is setting and using the property outside of any conditional statement.

    Sunday, November 17, 2019 12:52 AM
  • "the field b0Hour is assigned but it is never used error". 

    The previous answer explained why you might get an error saying that "The field is used but not assigned". This would be an error, not a warning.

    But your case is different. What is happening is that the variable is assigned values (b0Hour=true or b0Hour=false) but then there is nothing in the code that reads the value of b0Hour. So the compiler warns you: why do you bother assigning a value for this variable if you then never use the value?

    The compiler knows that you never use the variable because it is scoped as "internal", so it cannot be used outside the assembly. And the compiler is compiling the whole of the assembly, so it's able to know that it's never used.

    Sunday, November 17, 2019 11:49 AM
    Moderator
  • Thank for the answer.  I closed the program yesterday and when I opened it today the 30+ warnings were all gone.  I am baffled.   
    Sunday, November 17, 2019 3:24 PM
  • The compiler wouldn't know, that in the main program, the static sub "myTimeCheck" is being called?  "myTiimeCheck" is called once a minute and instead of creating new variables for each hour I wanted the static class to use those static variables.  After closing my program last night and opening it back up this morning, all of those 30+ warnings disappeared.  
    Sunday, November 17, 2019 3:29 PM
  • Perhaps the reason is that you added "myTimeCheck" later to your program. At the time that the compiler was issuing the warnings, you had still never compiled a version that contained the sub that used the variables, so the compiler considered them "unused". Then you added the second part that used the variables and, once you compiled all of it so the compiler got that part into its tables then the warning went away.
    Sunday, November 17, 2019 5:00 PM
    Moderator