automatic-properties

Objective C multiple declarations of instance variables / properties

喜欢而已 提交于 2019-12-06 05:17:25
I'm still pretty new to ObjC. I noticed that it's pretty standard everywhere to create your @interface myObj : NSObject { id delegate; NSDictionary *dict; } and then @property (nonatomic,retain) NSDictionary *dict; @property (retain) id delegate; --for example. I know how useful the auto code generation + clearer definition of @property is thanks to the Declared Properties page over at Apple. What I do not understand, however, is why it's standard for people to do both -- declare their properties and then have them again in the {curly brackets}. I mean, if I had a class where I wanted some of

iOS — “add” methods appearing for autosynthesized properties in codeSense

不羁的心 提交于 2019-12-05 19:48:11
问题 I just created an iOS class with the following properties: @property (nonatomic, strong) NSString* foo; @property (nonatomic, strong) NSObject* bar; @property (nonatomic) CGRect fubar; I did not put in any @synthesize or explicit ivars for these properties. I then went into the implementation file and started to create a method as follows: -(void) add I left the cursor at the end of the word "add". The following method names then popped up in code sense: addBar: (NSSet*) objects addBarObject:

How often do you see abuse of C# shorthand getters/setters?

做~自己de王妃 提交于 2019-12-05 05:31:15
In C# you can create getter/setters in a simpler way than other languages: public int FooBar { get; set; } This creates an internal private variable which you can't address directly, with the external property 'FooBar' to access it directly. My question is - how often do you see this abused? It seems like it has a high potential to violate encapsulation best-practices often. Don't get me wrong, I use it as appropriate, and partial variations of it for read-only write-only types of properties, but what are your unpleasant experiences with it from other authors in your code base? Clarification:

Auto-property initializer Singleton implementation

偶尔善良 提交于 2019-12-05 04:07:30
So, with the brand new C# 6 we got those neat auto-property initializers. I thought I might as well take advantage of these to make more concise singletons than ever. Apparently someone else got that idea, too. public sealed class Singleton { public static Singleton Instance { get; } = new Singleton(); private Singleton() { /* some initialization code */ } } My questions are: How thread-safe it is? How lazy it is, or when the instance is actually created? (not a priority, but it would be good for future reference) Is it a good idea overall? (it might look similar to this question , but it's

Code contracts on auto-implemented properties

孤街醉人 提交于 2019-12-04 23:46:25
Is there any way to put contracts on automatically implemented properties in .NET? (And how if the answer is 'Yes')? (I assume using .NET code contracts from DevLabs) Yes, this is possible - all that is needed is to add your contract condition to the [ContractInvariantMethod] method in your class, which then adds the equivalent Requires precondition to the automatic set ter, and a post condition Ensures is added to the get . From section 2.3.1 of the Reference As the example illustrates, invariants on auto-properties turn into: A precondition for the setter A postcondition for the getter An

Learning about Auto-Implemented Properties

风流意气都作罢 提交于 2019-12-04 13:56:38
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 string _BarName = ""; public string BarName { get {return _BarName;} set {_BarName = Value.ToString()

Generate custom setter using attributes

北战南征 提交于 2019-12-04 08:12:45
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 attribute which does this? Hopefully , which works with Intellisense. How does the default [Property()]

Encapsulate Collection in C#

一曲冷凌霜 提交于 2019-12-04 07:58:24
问题 Since 3.0 C# has great syntax sugar like auto-properties which a lot simplify implementation of encapsulation principle. This is good if you use it with atomic values, so you can replace encapsulation pattern like this: private string _name; public string Name { get { return _name; } set { _name = value; } } with just one line: public string FirstName { get; set; } I very like this great feature as it saves a lot of developers time. But things are not so great when you create property that

Object fingerprinting: serialization + untouchable legacy code + Getter-only auto-properties = cornered?

杀马特。学长 韩版系。学妹 提交于 2019-12-04 07:14:32
问题 I have found myself cornered, so here we go. Context I need to produce a fingerprint hash code for object diffing. Comparing the hashes of two sets of objects will need to tell me if there are identical objects with the same hash. The fingerprint hash must be platform-independent. So I went for MD5 hashing . I am working with a large Object model code base that is out of my control. All types that I will be passed for this fingerprinting can not be modified by me . I cannot add attribute or

MATLAB how to automatically read a number of files

て烟熏妆下的殇ゞ 提交于 2019-12-04 06:54:16
问题 I would like to plot a number of 3D graphs from different data files. For example I am using fid = fopen('SS 1.dat','r'); to read the first file and then plot a graph. How to set the program to change the name to 'SS 2.dat' automatically? Also for the tenth file the name becomes 'SS 10.dat' which has one space less (i.e.only two space between SS and 10) then the first to ninth files. How to set the program to adjust for that? Thank you. 回答1: The following code displays a lazy way to print the