C # set set body without declaring class property variable

Do I need to declare a class level variable to store the property, or can I just call self.{propertyname} in getter / setter?

In other words, can I do this? (where I did not define mongoFormId anywhere):

 public string mongoFormId { get { return this.mongoFormId; } set { this.mongoFormId = value; revalidateTransformation(); } } 
+7
source share
8 answers

You can use automatic means of access or implement your own. If you use automatic accessors, the C # compiler will create a support field for you, but if you implement it yourself, you must manually provide a support field (or handle the value in some other way).

 private string _mongoFormId; public string mongoFormId { get { return this._mongoFormId; } set { this._mongoFormId = value; revalidateTransformation(); } } 

UPDATE:. After this question was asked, C # 6.0 was released. However, even with the new syntax options , there is still no way to provide a custom installer body without having to explicitly declare a support field.

+12
source

You need to set the field variable and save the value there if you are going to use a custom getter and setter.

With the code that you have now, you will run into a stack overflow. When you assign something to mongoFormId , you will run the line this.MongoFormId = value; . This is the purpose of mongoFormId , which results in the string this.MongoFormId = value; etc. It will never stop.

The correct way is this field:

  private string _mongoFormId; public string mongoFormId { get { return this._mongoFormId; } set { this._mongoFormId = value; revalidateTransformation(); } } 
+3
source

You must have a backup variable. Take a closer look:

 get { return this.mongoFormId; } 

Going to call getter on mongoFormId , which will call this code again, and again and again! Defining a support variable avoids an endless recursive call.

+2
source

Check MSDN Properties Overview

Although a property definition usually includes a private data element, this is not required. An access recipient can return a value without access to a private data item. One example is a property that receives a method that returns the system time. Properties allow you to hide data, access methods hide the implementation of the property.

0
source

This will not work, as you will get a recursive call to the property. If I am not mistaken, the result will be a StackOverflowException . You must use a variable.

  private string mongoFormId; public string MongoFormId { get { return this.mongoFormId; } set { this.mongoFormId = value; revalidateTransformation(); } } 

If you do not need to perform revalidateTransformation , you can use the auto-property. This will create a backup for you behind the scenes.

 public string MongoFormId { get; set; } 
0
source

With the code you wrote, you create a recursive infinite loop for both get and set. This keyword refers to the current class, not to the property you are in.

So you need to declare a private field. To avoid confusion, create properties by following the MSDN Naming Guide (Use the Pascal property for properties, the camel case for private fields). And please do the same for your methods, this should be RevalidateTransformation instead of RevalidateTransformation if you follow the C # convention instead of java.

 private string mongoFormId; public string MongoFormId { get { return mongoFormId; } set { mongoFormId = value; RevalidateTransformation(); } } 
0
source

You can do this both ways.

If you want to have a class member variable, do it like this -

 public class sampleClass { private string _mongoFormId; public string mongoFormId { get { return _mongoFormId; } set { _mongoFormId = value; revalidateTransformation(); } } } 

Or do it just in the class if you don't need revalidateTransformation () to make the call there

 public class sampleClass { public string mongoFormId {get; set;} } 
0
source
 public string mongoFormId { get { return this.mongoFormId; } set { this.mongoFormId = value; revalidateTransformation(); } } 

So you have a recursive function on all paths. The only way I can see is to use a private data member. As the other boys say.

0
source

All Articles