Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Writing Getter setter methods of class using C# in .net 2.0

Author: Zunnair
Download Source Code : 825_ClsGetSet.zip

In this simple article you will learn that how you can write getter and setter methods of a class

 Getter Methods:

Getter method in a class are methods which are use to get value of private variable of the class, so that user should not be able to access them directly.

 

Setter Methods:

Setter method in a class are methods which are use to set value of private variable of the class, so that user should not be able to set them directly.

 

 

Create new c# project and add new class with name math.cs.

 

Write following code in class:

 

C#

class Math

    {

        int _number1;

        int _number2;

        int _number3;

 

        public int number1

        {

            get

            {

                return this._number1;

            }

            set

            {

                _number1 = value;

            }

        }

       

        public int number2

        {

            get

            {

                return this._number2;

            }

            set

            {

                _number2 = value;

            }

        }

       

        public int number3

        {

            get

            {

                return this._number3;

            }

            set

            {

                _number3 = value;

            }

        }

    }

VB

 

Class Math

        Private _number1 As Integer

        Private _number2 As Integer

        Private _number3 As Integer

 

        Public Property number1() As Integer

            Get

                Return Me._number1

            End Get

            Set(ByVal value As Integer)

                _number1 = value

            End Set

        End Property

 

        Public Property number2() As Integer

            Get

                Return Me._number2

            End Get

            Set(ByVal value As Integer)

                _number2 = value

            End Set

        End Property

 

        Public Property number3() As Integer

            Get

                Return Me._number3

            End Get

            Set(ByVal value As Integer)

                _number3 = value

            End Set

        End Property

    End Class

 

 

In this class you can see that user is unable to access _number1 variable directly, the only way is this that he can access it by its property methods called getter and setter.

Article Comments
using System;
using System.Collections.Generic;
using System.Text;

namespace setnget
{
class Program
{
private int aa;
private float bb;
private int cc;
private int a
{
get
{
return aa;
}
set
{
aa = value;
}
}

private float b
{
get
{
return bb;
}
set
{
bb = value;
}
}
private int c
{
get
{
return cc;
}
set
{
cc = value;
}
}


public void display()
{
Console.WriteLine("{0} {1} {2}", aa, bb, cc);
}

static void Main(string[] args)
{
Program p = new Program();
p.aa = 10;
p.aa--;
p.display();
Console.Read();
}
}
}

Posted on 5/19/2010 12:58:56 AM by Sriramana and Dhanunjaya

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Getting window form closing reason using C# in .net 2.0.

Disclaimer - Privacy
© 2002-2017 DevASP.net