getter

Good or bad practice? Initializing objects in getter

只谈情不闲聊 提交于 2020-01-09 12:09:51
问题 I have a strange habit it seems... according to my co-worker at least. We've been working on a small project together. The way I wrote the classes is (simplified example): [Serializable()] public class Foo { public Foo() { } private Bar _bar; public Bar Bar { get { if (_bar == null) _bar = new Bar(); return _bar; } set { _bar = value; } } } So, basically, I only initialize any field when a getter is called and the field is still null. I figured this would reduce overload by not initializing

Good or bad practice? Initializing objects in getter

℡╲_俬逩灬. 提交于 2020-01-09 12:08:23
问题 I have a strange habit it seems... according to my co-worker at least. We've been working on a small project together. The way I wrote the classes is (simplified example): [Serializable()] public class Foo { public Foo() { } private Bar _bar; public Bar Bar { get { if (_bar == null) _bar = new Bar(); return _bar; } set { _bar = value; } } } So, basically, I only initialize any field when a getter is called and the field is still null. I figured this would reduce overload by not initializing

Combination of getter and list modification in Java

对着背影说爱祢 提交于 2020-01-05 07:25:42
问题 today i dealt with a Java problem that really confused me. I have the following code: List<ObjectXY> someList = obj.getListOfObjectsXY(); // getter returns 2 elements someList.add(new ObjectXY()); obj.getListOfObjectsXY(); // getter now returns 3 elements When i add an element to a list, the getter gets some kind of overwritten. Is this because someList acts like a reference on the result of the getter in this case? Or what else causes this effect? I solved the problem with the following code

How do input field methods (text_area, text_field, etc.) get attribute values from a record within a form_for block?

霸气de小男生 提交于 2020-01-05 03:33:06
问题 I have a standard Rails 2.3.5 app with a model called Post. Post has an attribute called url, and the following getter is defined: def url p = 'http://' u = self[:url] u.starts_with?(p) ? u : "#{p}#{u}" end If I load up script/console , I can do Post.first.url and get the desired result (e.g. it returns http://foo.com if the attribute's true value is foo.com) However, if I have a form_for block, and do something like form.text_field :url , it will not return the url with http:// prefixed;

How to remove the setter from a JavaScript object?

你说的曾经没有我的故事 提交于 2020-01-04 03:32:26
问题 Consider the following code: var x = 0; var o = {}; function getter() { return x; } Object.defineProperty(o, "y", { get: getter, set: function (y) { x = y; Object.defineProperty(o, "y", { get: getter }); }, configurable: true }); My objective is to remove the setter and make the property o.y non-configurable after the setter has been called once. However it doesn't work as expected: > x // 0 > o.y // 0 > o.y = 1 // 1 > x // 1 > o.y // 1 > o.y = 2 // 2 > x // 2 > o.y // 2 So my code did not

Are const references to members safe

六月ゝ 毕业季﹏ 提交于 2020-01-03 13:32:11
问题 If I use a const reference to another member, is it possible that this reference gets invalidated? class Class { public: const int &x{y}; private: int y; }; For example when I use instances of this class in a vector which increases its capacity after a push_back . According to the standard all iterators and references are invalidated if a vector has to increase its capacity. Is the reference still valid after that? 回答1: This is currently not safe, as when you copy an instance of Class , x

Templates for setters and getters

南楼画角 提交于 2020-01-02 02:21:33
问题 I am not familiar with templates, but I wonder, if it is possible to use them for setter and getter methods. For example in this situation: double exmlClass::getA(void) const { return a_; } void exmlClass::setA(const double& a) { a_ = a; } double exmlClass::getB(void) const { return b_; } As you can see, methods are almost the same, except they refer to another private variables (a_, b_, c_). Is there a more elegant way to write those functions or it is common practice to do like above in

How to avoid getters/setters in classes with many instance variables

限于喜欢 提交于 2020-01-01 05:51:06
问题 I'll try to keep it short. I have classes with many instance variables (30+) and hence many getters/setters. The classes by themselves are simple, but because of the getters/setters the LOC just exploded (and there also was way too much code duplicity). So I removed the attributes and stored them in a map, like this public class MyTechnicalToolClassX { //...constructor private Map<String, Object> data; public Object getAttributeByKey(AttributeKey key) { // ...doStuff, check data associated

Referencing getter/setter functions in actionscript 3

为君一笑 提交于 2019-12-31 07:23:12
问题 How does one get a reference the the getter and setter functions in actionscript 3? if a method is defined on the calls, e.g. public function blah():String { ...} I can get a reference to it by just saying blah or this.blah How do get a reference to public function get blah2():String {} public function set blah2(b:String):void {} Thanks! 回答1: Original response: Unfortunately, you will not be able to store references to those as functions. The getter and setter methods are actually built

Setting List items in C# without automatic setter/getter

六月ゝ 毕业季﹏ 提交于 2019-12-30 11:33:10
问题 I'm trying to make a manual setter/getter method in C#, but i'm getting the following error from the "set"-line: Error: The best overloaded method match for 'System.Collections.Generic.ListPackage.Add(Package)' has some invalid arguments private List<Package> packages = new List<Package>(); public List<Package> Packages { set { packages.Add(value); } get { return packages; } } 回答1: Your code should look like this. private var packages = new List<Package>(); public List<Package> Packages { set