locked
Constant Member Function and Modification Member Function

    Question

  • Hi everyone,
      I just started to study classes and I have difficulties understanding the difference between constant member function and modification member functions. Can anyone explain it to me in a simple and clear way?
         Thank you,
             Julia
    • Moved by Martin Xie - MSFT Friday, August 22, 2008 6:20 AM Move (Moved from Visual Studio Class Designer to Visual Basic Language)
    Monday, August 18, 2008 12:38 PM

Answers

  • JuliaSandor said:
     I just started to study classes and I have difficulties understanding the difference between constant member function and modification member functions. Can anyone explain it to me in a simple and clear way?
            

    Dear Julia,

    Welcome to MSDN forums!

    A constant member value would not be changed once it's assigned.
    The constant members are useful even required under certain scenarios.

    There are two approaches (Const keyword and Readonly keyword) to create a constant in .NET.

    A quick synopsis on the differences between 'Const' and 'Readonly' in .NET
    http://dathan-tech.spaces.live.com/blog/cns!147BFB11775C9091!224.entry?wa=wsignin1.0

    'Const':

    • Can't be static.
    • Value is evaluated at compile time.
    • Initiailized at declaration only.

    'Readonly':

    • Can be either instance-level or static.
    • Value is evaluated at run time.
    • Can be initialized in declaration or by code in the constructor.

    Understanding C# Constants
    http://www.aspnetpro.com/NewsletterArticle/2004/05/asp200405bw_l/asp200405bw_l.asp

    C# code:

    //The first creates a compile-time constant  
    private const int _Millenium = 2000;  
       
    //The second creates a run-time class constant  
    private static readonly DateTime _classCreation = DateTime.Now;   
     
    //The third creates a run-time object constant  
    private readonly DateTime _InstanceTime = DateTime.Now; 

    VB.NET code:

    Private Const _Millenium As Integer = 2000  
     
    Private Shared ReadOnly _classCreation As DateTime = DateTime.Now  
     
    Private ReadOnly _InstanceTime As DateTime = DateTime.Now 

    C# and VB.Net are essentially identical. Here are two helpful conversion tools:
    1. Instant VB: C# to VB converter

    The tool can convert entire C# project to corresponding VB.NET project, also can convert code snippet.
    2. Convert C# code to VB.NET code by means of this Code Translator tool.


    Other references:

    Visual Basic Express Edition is free-to-download, free-to-register, easy-to-use, easy-to-learn and funny.

    Download Visual Basic 2008 Express Edition
    http://www.microsoft.com/express/download/

    In the following thread, a teacher Solitaire has ever recommended some good books to his students, such as "Beginning Visual Basic 2005 Express Edition", "Microsoft Visual Basic 2005 Step by Step".
    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2099704&SiteID=1

    There are many VB.NET code samples on Microsoft website for you to start with VB.NET.
    101 Visual Basic and C# Code Samples

    Visual Basic .NET Code Samples: Master Set of 101 Samples
     

    Beginner Developer Learning Center
    http://msdn.microsoft.com/en-us/beginner/default.aspx

    Windows Forms FAQ
    http://www.syncfusion.com/FAQ/WindowsForms/Default.aspx

     

    Best regards,
    Martin Xie

    Friday, August 22, 2008 7:01 AM

All replies

  • The Visual Studio Class Designer is a project structure visualization tool in Visual Studio. Ask computer language questions in a computer language forum.

    MSMVP VC++
    Monday, August 18, 2008 4:03 PM
  • JuliaSandor said:
     I just started to study classes and I have difficulties understanding the difference between constant member function and modification member functions. Can anyone explain it to me in a simple and clear way?
            

    Dear Julia,

    Welcome to MSDN forums!

    A constant member value would not be changed once it's assigned.
    The constant members are useful even required under certain scenarios.

    There are two approaches (Const keyword and Readonly keyword) to create a constant in .NET.

    A quick synopsis on the differences between 'Const' and 'Readonly' in .NET
    http://dathan-tech.spaces.live.com/blog/cns!147BFB11775C9091!224.entry?wa=wsignin1.0

    'Const':

    • Can't be static.
    • Value is evaluated at compile time.
    • Initiailized at declaration only.

    'Readonly':

    • Can be either instance-level or static.
    • Value is evaluated at run time.
    • Can be initialized in declaration or by code in the constructor.

    Understanding C# Constants
    http://www.aspnetpro.com/NewsletterArticle/2004/05/asp200405bw_l/asp200405bw_l.asp

    C# code:

    //The first creates a compile-time constant  
    private const int _Millenium = 2000;  
       
    //The second creates a run-time class constant  
    private static readonly DateTime _classCreation = DateTime.Now;   
     
    //The third creates a run-time object constant  
    private readonly DateTime _InstanceTime = DateTime.Now; 

    VB.NET code:

    Private Const _Millenium As Integer = 2000  
     
    Private Shared ReadOnly _classCreation As DateTime = DateTime.Now  
     
    Private ReadOnly _InstanceTime As DateTime = DateTime.Now 

    C# and VB.Net are essentially identical. Here are two helpful conversion tools:
    1. Instant VB: C# to VB converter

    The tool can convert entire C# project to corresponding VB.NET project, also can convert code snippet.
    2. Convert C# code to VB.NET code by means of this Code Translator tool.


    Other references:

    Visual Basic Express Edition is free-to-download, free-to-register, easy-to-use, easy-to-learn and funny.

    Download Visual Basic 2008 Express Edition
    http://www.microsoft.com/express/download/

    In the following thread, a teacher Solitaire has ever recommended some good books to his students, such as "Beginning Visual Basic 2005 Express Edition", "Microsoft Visual Basic 2005 Step by Step".
    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2099704&SiteID=1

    There are many VB.NET code samples on Microsoft website for you to start with VB.NET.
    101 Visual Basic and C# Code Samples

    Visual Basic .NET Code Samples: Master Set of 101 Samples
     

    Beginner Developer Learning Center
    http://msdn.microsoft.com/en-us/beginner/default.aspx

    Windows Forms FAQ
    http://www.syncfusion.com/FAQ/WindowsForms/Default.aspx

     

    Best regards,
    Martin Xie

    Friday, August 22, 2008 7:01 AM