Like Tree1Likes
  • 1 Post By Shakti

Is there a way to tell that the variable is still unused using logical operators?

This is a discussion on Is there a way to tell that the variable is still unused using logical operators? within the C# Programming forums, part of the General Programming Boards category; I am still new to this programming, my goal is to be able to tell whether the variable is already ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    18

    Question Is there a way to tell that the variable is still unused using logical operators?

    I am still new to this programming, my goal is to be able to tell whether the variable is already used using the logical operators. And if the variable is not yet used, then there will be some list of things to do after that.

    I would really be thankful if someone helps me...
    Last edited by cloudpuffballz; 10-09-2012 at 06:07 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,634
    There are tools (eg Resharper) with capabilities to detect unused symbols (including variables) in C# code.

    However, such tools can be fooled (eg by code which uses reflection) into incorrectly reporting a variable as unused, so always manually check what is reported.

    No operators in the language provide a means to detect if a variable is unused. By definition, when an operator acts on a variable (eg retrieves its value, converts the value, changes the value) the variable is in use.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,209
    You could create your own class that wraps the variable and only grants access to it through a property, then have a boolean to tell wether that property has been accessed:
    Code:
    class AccessTracked<T>
    {
        private T m_value;
    
        public T Value
        {
            get
            {
                Accessed = true;
                return m_value;
            }
            set
            {
                Accessed = true;
                m_value = value;
            }
        }
    
        public bool Accessed
        {
            get;
            private set;
        }
    
        public AccessTracked(T initialValue)
        {
            m_value = initialValue;
            Accessed = false;
        }
    }
    itsme86 likes this.

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    That works but it hides a major logic flaw. Why do you care what variables have been accessed in a statement? Referring back to grumpy's post, if you wrote the statement and manually accessed the variable by including it in the statement....then you already know if it was accessed or not b/c you put it in the statement.

    What exactly are you trying accomplish here? If we know more context we might be able to guide you in a better direction.
    Last edited by VirtualAce; 10-12-2012 at 04:34 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unused variable in a program?
    By password636 in forum C Programming
    Replies: 3
    Last Post: 09-29-2012, 01:42 AM
  2. Logical Operators
    By Geagle in forum C Programming
    Replies: 2
    Last Post: 08-30-2012, 11:02 PM
  3. Logical Operators in C++
    By Flecto in forum C++ Programming
    Replies: 4
    Last Post: 05-15-2009, 08:17 AM
  4. logical operators
    By Marlon in forum C++ Programming
    Replies: 5
    Last Post: 07-13-2005, 03:55 AM
  5. Logical Operators
    By E i F x 65 in forum C++ Programming
    Replies: 6
    Last Post: 01-24-2002, 08:45 AM

Tags for this Thread


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