locked
What wrong with my code,it always give warning RRS feed

  • Question

  • hi all.Just want to know what wrong with my code below,it always give me two lines of warning,
    1.the variable 'ukstr' is assign but it never used
    2.the variable 'ukint' is assign but it never used

    //-----------------------------------------

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace ConsoleApplication1
    {
        class Program

        {
           
            static void Main()
            {
                object ukobj = table5.Rows[i].ItemArray[2];   // gives result ukobj=null;
                string ukstr = "";
                int ukint = 0;

                if (ukobj == null)
                {
                    ukint = 1;
                    ukstr = " ";
                }
             
            }
        }
    }

    //----------------

    thank you
    hans
    • Moved by CoolDadTxModerator Thursday, April 30, 2009 1:41 PM C# related (From:Visual C# IDE)
    Thursday, April 30, 2009 1:40 AM

Answers

  • Hi,

    The warning speaks for itself. The two variables are not used .
     
    If we set the value of ukint to another int i, the warning doesn't show up.
     int i = ukint;

    Harry

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Harry Zhu Thursday, May 7, 2009 3:44 AM
    Wednesday, May 6, 2009 6:27 AM

All replies

  • Hi,
    The warning is very appropriate.
    You have assigned variables - ukstr = "" & ukint = 0.

    But in code, it has not been used in all cases.
    This means that- if ukobj != null, i.e. if the If condition fails- the variables are not used.
    The variables are only used if it goes into the True if block.

    What you can do is- also have an else block with the If statement.
    Then, the variables would be used in both the cases True or False. And, you would not get a warning.

    Something like:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
    
        {
            
            static void Main()
            {
                object ukobj = table5.Rows[i].ItemArray[2];   // gives result ukobj=null;
                string ukstr = "";
                int ukint = 0;
    
                if (ukobj == null)
                {
                    ukint = 1;
                    ukstr = " ";
                }
                else
                {
                    ukint = 0;
                    ukstr = "";
                }
              
            }
        }
    }
    Additionally, it is always better to have an If-Else statement, rather than a solitary If statement to avoid probablity of errors/ exception flows.
    Regards, Lakra :) - If the post is helpful or answers your question, please mark it as such.
    • Proposed as answer by Abhijeet Lakra Thursday, April 30, 2009 6:20 AM
    Thursday, April 30, 2009 2:40 AM
  • Hi,

    The warning speaks for itself. The two variables are not used .
     
    If we set the value of ukint to another int i, the warning doesn't show up.
     int i = ukint;

    Harry

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Harry Zhu Thursday, May 7, 2009 3:44 AM
    Wednesday, May 6, 2009 6:27 AM
  • Hi,
    The warning is very appropriate.
    You have assigned variables - ukstr = ""  & ukint = 0 .

    But in code, it has not been used in all cases.
    This means that- if ukobj != null, i.e. if the If condition fails- the variables are not used.
    The variables are only used if it goes into the True if block.

    What you can do is- also have an else block with the If statement.
    Then, the variables would be used in both the cases True or False . And, you would not get a warning.

    Something like:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
    
        {
            
            static void Main()
            {
                object ukobj = table5.Rows[i].ItemArray[2];   // gives result ukobj=null;
                string ukstr = "";
                int ukint = 0;
    
                if (ukobj == null)
                {
                    ukint = 1;
                    ukstr = " ";
                }
                else
                {
                    ukint = 0;
                    ukstr = "";
                }
              
            }
        }
    }
    Additionally, it is always better to have an If-Else statement , rather than a solitary If statement to avoid probablity of errors/ exception flows.
    Regards, Lakra :) - If the post is helpful or answers your question, please mark it as such.

    Those values are STILL not used! They're overwritten, but not used.

    Wednesday, May 6, 2009 8:55 AM