1 Write a program with 3 fields that are departmentName (read only), StudentName (read and write), StudentRolNumber (read and write). Implement encapsulation using get and set accessors with the check only on roll number and student name that roll number should not be negative and name should not be null. In this case user will set the initial value of roll number and name from main function.
using System;
class SE
{
private int RN;
private string name;
public string address { get; set; }
public int studentRN // this is read /write property
{
get //get means read
{
return RN; // returning it to main
}
set //set means write
{
do
{
Console.WriteLine("Enter stduent Roll Number=");
value = Convert.ToInt32(Console.ReadLine());
if (value <= 0)
Console.WriteLine("\n Roll-Number must be greater than ZERO , try again");
else
Console.WriteLine("\n Roll Number saved\n\n");
} while (value <= 0); // check to aviod entry of -ve number
this.RN = value;
}
}
public string studentName // this is read /write property
{
get //get means read
{
return name; // returning it to main
}
set //set means write
{
do
{
Console.WriteLine("Enter stduent Name (null value is not allowed)= ");
value = Console.ReadLine();
if (string.IsNullOrEmpty(value))
Console.WriteLine("\n Student Name cannot be NULL or EMPTY");
else
Console.WriteLine("\n Roll Number saved\n\n");
} while (string.IsNullOrEmpty(value)); // check to aviod entry of -ve number
this.name = value;
}
}
class program
{
static void Main(string[] arg)
{
SE obj = new SE();
obj.studentRN = 100; // initlizing of valye
obj.studentName = "Ali";
Console.WriteLine("\n The value of RN=" + obj.studentRN); //dispaly of value
Console.WriteLine("\n The Student Name=" + obj.studentName); //dispaly of value
obj.address = "\n House No 9, B4, Mirpur city";
Console.WriteLine("\n The Student address=" + obj.address); //dispaly of value
Console.Read();
}
}
}
0 Comments