none
what does "value" variable really represents inside classes RRS feed

  • Question

  • I am reading this article about creating binary trees in C# https://msdn.microsoft.com/en-us/library/ms379572(v=vs.80).aspx

    and inside one class i find that they are referring to a variable named "value" inside this statement "data = value;" as follow:-

    public class Node<T>
    {
            // Private member-variables
            private T data;
            private NodeList<T> neighbors = null;
    
            public Node() {}
            public Node(T data) : this(data, null) {}
            public Node(T data, NodeList<T> neighbors)
            {
                this.data = data;
                this.neighbors = neighbors;
            }
    
            public T Value
            {
                get
                {
                    return data;
                }
                set
                {
                    data = value;
                }
            }
    
            protected NodeList<T> Neighbors
            {
                get
                {
                    return neighbors;
                }
                set
                {
                    neighbors = value;
                }
            }
        }
    }
    so what does the "value" variable really represents? as i can not find any definition for it inside the class?
    Friday, August 17, 2018 2:10 PM

All replies

  • That example is a bit confusing because they used Value (with a capital V) as the property name. But this is nothing to with value (lower-case v) inside the 'set' method.

    This is a special keyword as documented here.

    It only exists inside the 'set' method of a property and simply represents the value that is being set.

    Example:

    public class Test
    {
    	private int _someNumber;
    
            public int SomeNumber
            {
                get
                {
                    return _someNumber;;
                }
                set
                {
                    _someNumber = value;
                }
            }
    
    }
    
    ////////////////////
    
    Test t = new Test();
    t.SomeNumber = 6;	// At this point the set method will be called and value will be 6

    Friday, August 17, 2018 2:38 PM
  • Looks like pseudo-code
    Friday, August 17, 2018 2:41 PM
  • Value is T where T is common for a single generic object.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, August 17, 2018 2:41 PM
    Moderator
  • Value is T where T is common for a single generic object.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    With respect, not what the OP is asking!

    He is specifically asking about the 'value' variable (lower-case v) inside the set method. And possibly getting confused because they happened to call the property Value (capital V), which is just unfortunate coincidence.

    :)

    Friday, August 17, 2018 2:52 PM