none
What is the difference between Properties and functions

    Question

  • with a property in .Net you can have parameters just like functions. What's the point of having both?

    Apologies, I'm new to OO in .Net.

    Friday, April 21, 2006 12:54 PM

Answers

  • In the most of class, there are two kinds of concepts: data and action. For example, if I have a class to represent a robot. The data portion of the class will be the robots legs, arms, heads, etc. the action portion will be move arms, move legs, etc. The main purpose of the property is to allow class to expose its private variables and representing the data portion of the class, function representing the actions. In the following example, RotbotName() is a property, it representing a conception thing, a data member. VerifyName() is a function. It is an action.

     

    Class Robot

        Private name As String

     

     

        Public Property RobotName() As String

            Get

                Return name

            End Get

            Set(ByVal value As String)

                name = value

            End Set

        End Property

     

        Public Function VerifyName(ByVal yourGuess As String) As Boolean

            If yourName.Equals(yourGuess) Then

                Return True

            Else

                Return False

            End If

        End Function

     

     

    End Class

     

    Friday, April 21, 2006 9:35 PM

All replies

  • In the most of class, there are two kinds of concepts: data and action. For example, if I have a class to represent a robot. The data portion of the class will be the robots legs, arms, heads, etc. the action portion will be move arms, move legs, etc. The main purpose of the property is to allow class to expose its private variables and representing the data portion of the class, function representing the actions. In the following example, RotbotName() is a property, it representing a conception thing, a data member. VerifyName() is a function. It is an action.

     

    Class Robot

        Private name As String

     

     

        Public Property RobotName() As String

            Get

                Return name

            End Get

            Set(ByVal value As String)

                name = value

            End Set

        End Property

     

        Public Function VerifyName(ByVal yourGuess As String) As Boolean

            If yourName.Equals(yourGuess) Then

                Return True

            Else

                Return False

            End If

        End Function

     

     

    End Class

     

    Friday, April 21, 2006 9:35 PM
  • Property

    Use a property when the member is a logical data member. In the following member declarations, Name is a property because it is a logical member of the class.
    public string Name
       get
       {
          return name;
       }
       set
       {
          name = value;
       }

    Use a function when:

    1.The operation is a conversion, such as Object.ToString.
    2.The operation is expensive enough that you want to communicate to the user that they should consider caching the result.
    3.Obtaining a property value using the get accessor would have an observable side effect.
    4.Calling the member twice in succession produces different results.
    5.The order of execution is important. Note that a type's properties should be able to be set and retrieved in any order.
    6.The member is static but returns a value that can be changed.
    7.The member returns an array. Properties that return arrays can be very misleading. Usually it is necessary to return a copy of the internal array so that the user cannot change internal state. This, coupled with the fact that a user can easily assume it is an indexed property, leads to inefficient code. In the following code example, each call to the Methods property creates a copy of the array. As a result, 2n+1 copies of the array will be created in the following loop.


    Friday, December 27, 2013 6:08 AM
  • Basically Properties are used for Design of your class. You can get it is when you are design a page of your class like design a web page there u have properties of the page, controls etc. So the properties are used for design. And function are for executing some action.

    One more basic thing, function can have parameters but the properties can't so a property can't be overloaded while a function can be.

    Thursday, January 02, 2014 7:22 AM
  • Hi

    Properties can be used at the design time and can be modified at the runtime.

    Function is a specific unit written to do a specific work like

    void PrintPage(). This functioin is used to print a page and it has a void return type.

    But in properties it either have a return type or a assignment value.

    Like

    Must return some value.

    get{return x;}

    Assign value

    set{a = value;}

    We can use function inside a property like

    set

    {

    a = value;

    CallPrintPage();

    }

    Other thing is that property can be readonly or writeonly. It means that if property is readonly then the value can not be modified from the outside. similarly for writeonly.

    We can not say that this function is readonly or writeonly.

    class Person
    {
        private string name;  // the name field 
        public string Name    // the Name property
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }
    }

    Person person = new Person();
    person.Name = "Joe" // the set accessor is invoked here         if the property is readonly this will give an error      

    System.Console.Write(person.Name);  // the get accessor is invoked here if the property is write only this line will give a error

    Thursday, January 02, 2014 8:50 AM
  • Are you aware you abusing this forum by putting C# code in a VB forum?

    Success
    Cor

    Thursday, January 02, 2014 2:57 PM
  • Are you aware you abusing this forum by putting C# code in a VB forum?

    Success
    Cor

    Thursday, January 02, 2014 2:58 PM
  • Do you think somebody is waiting for your reply, this thread is almost a decenia old. 

    Beside that is your reply wrong. 

    A property is a so called accessor for a data part, a function is an old VB name and also keyword for a method which returns a result.

    Sometimes both are misused but this thread is to old to go to deep into it.


    Success
    Cor

    Thursday, January 02, 2014 3:02 PM