automatic-properties

Learning about Auto-Implemented Properties

懵懂的女人 提交于 2019-12-21 20:23:39
问题 I have the simple class using auto-implemented properies: Public Class foo { public foo() { } public string BarName {get; set;} } I obviously use the variable BarName throughout my class and now need to add logic when the property value is set (it must be all upper case, go figure). Does this mean that I need to now create a private variable for BarName , e.g. _BarName, and change the current BarName variable used throughout my class to _BarName? Public Class foo { public foo() {} private

Generate custom setter using attributes

烂漫一生 提交于 2019-12-21 15:33:31
问题 In classes whose instances I persist using an object database, I keep having to do this: private string _name; public string Name { get { return this._name; } set { _name = value; this.Save(); } } whereas I would much rather type this: [PersistedProperty(Name)] private string _name; where the PersistedProperty attributes generates a Getter and Setter just like the default [Property()] attribute, except I want to add a line of code to the generated Setter. Is there a way I can create an

Generate custom setter using attributes

安稳与你 提交于 2019-12-21 15:32:27
问题 In classes whose instances I persist using an object database, I keep having to do this: private string _name; public string Name { get { return this._name; } set { _name = value; this.Save(); } } whereas I would much rather type this: [PersistedProperty(Name)] private string _name; where the PersistedProperty attributes generates a Getter and Setter just like the default [Property()] attribute, except I want to add a line of code to the generated Setter. Is there a way I can create an

What's the best way to call INotifyPropertyChanged's PropertyChanged event? [duplicate]

泪湿孤枕 提交于 2019-12-18 12:27:01
问题 This question already has answers here : Implementing INotifyPropertyChanged - does a better way exist? (34 answers) Closed 4 months ago . When you implement the INotifyPropertyChanged interface, you're responsible for calling the PropertyChanged event each and everytime a property is updated in the class. This typically leads to the following code : public class MyClass: INotifyPropertyChanged private bool myfield; public bool MyField { get { return myfield; } set { if (myfield == value)

Auto-properties and structs

南楼画角 提交于 2019-12-17 20:35:18
问题 I am wondering about the following C#-code: struct Structure { public Structure(int a, int b) { PropertyA = a; PropertyB = b; } public int PropertyA { get; set; } public int PropertyB { get; set; } } It is not compiling with an error "The 'this' object cannot be used before all of its fields are assigned to". For the analogous class it is compiling without any problems. It can be made working by refactoring to the following: struct Structure { private int _propertyA; private int _propertyB;

How to set value of property where there is no setter

落花浮王杯 提交于 2019-12-17 16:58:11
问题 I have seen various questions raised and answered where we can invoke a private setter using reflection such as this one: Is it possible to get a property's private setter through reflection? However I have some code which has a property i need to set but cant because there is no setter, I cant add a setter as this isn't my code. Is there a way to somehow set the value using reflection in this scenario? 回答1: I do not suggest doing this on your application but for testing purpose it may be

C# Automatic Properties

天涯浪子 提交于 2019-12-17 15:38:14
问题 I'm a bit confused on the point of Automatic properties in C# e.g public string Forename{ get; set; } I get that you are saving code by not having to declare a private variable, but what's the point of a property when you are not using any get or set logic? Why not just use public string Forename; I'm not sure what the difference between these 2 statements is, I always thought you used properties if you wanted additional get/set logic? 回答1: Properties can have code put into them without

Public Fields versus Automatic Properties

大憨熊 提交于 2019-12-16 19:03:11
问题 We're often told we should protect encapsulation by making getter and setter methods (properties in C#) for class fields, instead of exposing the fields to the outside world. But there are many times when a field is just there to hold a value and doesn't require any computation to get or set. For these we would all do this number: public class Book { private string _title; public string Title { get{ return _title; } set{ _title = value; } } } Well, I have a confession, I couldn't bear writing

How to create an auto-property faster in Delphi IDE?

百般思念 提交于 2019-12-13 11:35:43
问题 I need to create and manage many simple published properties. I call them auto-properties if they look like that: private FTitle: string; published property Title: string read FTitle write FTitle; Usually I create them next way: Adding property name and type: property Title: string Selecting and copying property name Title to clipboard. Appending the line with read F Ctrl+V write F Ctrl+V ; Pressing Ctrl+Shift+C and this will generate the private field Is there any way to exclude steps 2 and

Update a class property based on another Property of a calss properties value in the Setter

妖精的绣舞 提交于 2019-12-13 03:49:21
问题 I'm having two model Classes public class Person { public int PersonId { get; set; } public string Name { get; set; } public int AddressId { get; set; } public Address AddressInfo { get; set; } } public class Address { public int AddressId { get; set; } public string streetName { get; set; } public string City { get; set; } public string State { get; set; } } If any value gets update in Person.AddressInfo.AddressId, update the Person.AddressId automatically. Kindly assist me how to update