5
Answers

Property in C#

Photo of Arun Kurmi

Arun Kurmi

12y
988
1
hi friends,
(1) Is Possible use a messageBox to display a message in get{} or set{} Block ?I want to display a messege on the basis of value of set{} block.

My (2)Question are Commented in below code:

class Student
{
  private string name;
 
 public string Property
   {
      get
       {
            return name;
       }
       set
       {
            name=value;
       }
   }
}
------------------------------------------------
On button

Student stu=new Student();
stu.Property="arun";
string x=stu.Property;
//What is the meaning of below line.
stu.name="some one not arun"; //error

Answers (5)

1
Photo of Vulpes
NA 98.3k 1.5m 12y
If it's a Windows Forms application, you'll need this 'using' directive at the top of the class file:

   using System.Windows.Forms;

If it's a console application, you'll need to add a reference to System.Windows.Forms.dll as well as adding the above 'using' directive.
Accepted
5
Photo of Jignesh Trivedi
NA 61.3k 14.2m 12y
hi,

agree with Vuples

public int dummy
{
    get
    {
        return 0;
    }
    set
    {
        System.Windows.Forms.MessageBox.Show("set Property");
    }
}

hope this will help you.
0
Photo of Arun Kurmi
NA 106 72.8k 12y
But what is syntax for this.Because MessageBox.show give error during writing it in set block.
0
Photo of Satyapriya Nayak
NA 53k 8m 12y
http://www.c-sharpcorner.com/UploadFile/rajeshvs/PropertiesInCS11122005001040AM/PropertiesInCS.aspx
0
Photo of Jignesh Trivedi
NA 61.3k 14.2m 12y
hi,

1) yes, you can put message box in set block.
2) you are getting error on code stu.name="some one not arun";  right?
 this is due to name is private member of class. .NET does not allow you to access private member out side of scope. so you got the error.

hope this will help you.