FAQ Database Discussion Community


Overriding properties to make them readonly - what about the setter?

c#,inheritance,properties,polymorphism,setter
I have the following design goal in a class hierarchy: There is a BaseClass defining some properties, which would usually be read/write: public class Media { public virtual object Content { get; set; } public virtual double recordingLength { get; set; } } The intention is to have some subclasses...

Passing to Setter and Getter methods

java,methods,inner-classes,setter,getter
I have a Bread class and a Filling class which set the bread type and calories per slice as well as a filling class which sets the filling type and calories per serving... I can't seem to figure out how to pass them into the sandwich class correctly. My total...

How to set a property statically with changing variable

c#,.net,properties,setter
This is probably an easy question, I'm pretty new to OOP in general. If I have a simple class with one int array property (and constructor) public class MyClass { public int[] MyProperty {get; set;} public MyClass(){} } How do I set the property value to the value of an...

getters() and setters() simplification for a class

java,encapsulation,setter,getter
Why do we exactly need to use the set and get methods in our class that use private attributes? When it is really used during our program process? Can we still make it work without it (without changing the private attributes to public)? This is an example of a...

Guarantee not optional value, while allowing optional value as setter

swift,setter,getter,optional
class MyClass { private var _image: UIImage var image: UIImage { set { if newValue == nil { _image = UIImage(named: "some_image")! } } get { return _image } } } My goal is to guarantee non-optional value when image is accessed Can i achieve this without additional function? Even...

Check if class property has been set

c#,equality,setter,getter
Lets assume I have a User class public Class User { public string Name { get; set; } public string Surname { get; set; } public int Level {get;set;} } User user1 = new User(); user1.Name = "name"; user1.Surname = "Surname"; user1.Level = 0; User user2 = new User(); user2.Name...

php - setter of objects is not called

php,oop,object,setter,getter-setter
I have the following issue: The current code of an application I'm working on contains a very large number of definitions like this: $obj = new stdClass(); $obj->a->b = "something"; This results in: PHP Strict Standards: Creating default object from empty value in [somewhere]. The correct form would be: $obj...

Setter conventions in Java (return void or this)

java,setter,code-conventions
I have been writing Java for almost a year now, and I have seen 2 different conventions for how people implement their setters. To Illustrate this, here are examples of both conventions. (I would love also to know concise names of these two patters) Classes using first convention, return nothing...

Why must a setter be empty, if the property uses annotation attributes?

c#,annotations,setter
i want to use the setter to auto-correct the value and want to use the RequiredAttribute too. But in this case the RequiredAttribute do not work, because the setter is not empty. So, why have the setter to be empty? [Required(AllowEmptyStrings = false, ErrorMessage = "The Name cannot be empty....

Better practice for heap object getters / setters in C++

c++,setter,getter
I'm currently having Type1 &GetType1() const { return *this->type1; } void SetType1(const Type1 &type1) { *this->type1 = type1; } and in the class definition class Type2 { public: Type2(); virtual ~Type2(); Type1 &GetType1() const; void SetType1(const Type1 &type1); private: Type1 *type1 = nullptr; } And in main int main() {...

How setter works inside Spring Framework?

java,spring,spring-mvc,setter,getter-setter
I'm new in spring Framework. And actually i was doing an experiment with spring actually. Look at this HelloWorld.java: public class HelloWorld { private String messageee; public void setMessage(String messageee){ this.messageee=messageee; } public void show(){ System.out.println("message: "+messageee); } } You see in this program, I've one variable which is outside...

How to use setters and getters in a Javascript object (Class)?

javascript,class,setter,getter,getter-setter
The following script DOES NOT WORK. What would be the proper way? function AnObject () { get this.apple = () { return this.apple + "GET"; } set this.apple = ( newValue ) { return newValue + "SET"; } } var obj = new AnObject(); obj.apple = "Lemon"; console.log( obj.apple );...

Using Getters and Setters: a Beginners guide

java,setter,getter
Okay so I'm totally a beginner and just learning this in class. Can anyone tell me what I'm doing wrong? When I run this code it's supposed to tell me that myMonkey and myFavoriteMonkey are the same color... but I keep getting a null value for the output... I've got...

Java Reflection - get the name of a field from a set method

java,reflection,field,setter
i started to work with java reflection I started to create my own personal working methods, including a method that takes all the setter of a class. Now I find myself in the bizarre situation of wanting to take the name of the field of methods set, i try to...

Rails 4 Override belongs_to setter

ruby-on-rails,override,setter,belongs-to
I am having an issue overriding a setter on a belongs_to attribute. I have the following: class Task < ActiveRecord::Base belongs_to :parent_task, :class_name => 'Task', :foreign_key => 'parent_task_id' def parent_task=(value) write_attribute(:parent_task, value) unless value == nil #remove all groups_belonging_to if this has been made into a child task -i.e. if...

“More functional” “accessor” for collections in C#

c#,collections,setter,getter,accessor
Now I have a collection: Dictionary<string, List<string>> dictionary. For some reason, now I want to "project/map" a part of it to other collections, sort of like using it as backing field, and create different accessors for this collection. Like List<string> keys corresponds to the keys of this dictionary, or Dictionary<string,...

Can I have just getters and not setter?

groovy,default,setter,getter
I am quite new to groovy, and I have found out that by making a field public, groovy provides getters and setters by default. Is there a way to have just the getters but not the setters by default? The reason behind this is that I have a Builder and...

LPTSTR Losing Scope, work around not working (C++)

c++,scope,setter,lptstr
EDIT: Solution was to replace LPTSTR to wstring. When Windows API required LPTSTR the wstring is casted with .c_str() My problem is, a variable (LPTSTR) is going out of scope and I can't fix it. I'll explain... In my code I am trying to save the name of an Image...

Sometimes-readonly property

python,python-2.7,design-patterns,setter,programming-paradigms
I have a class, CameraInterface, with two properties: recording (a boolean) recording_quality (an enumeration with three possible values) The user can push hardware buttons (this is a Raspberry Pi project) to select the recording_quality, but if the camera is already recording, the hardware will blink/buzz/otherwise indicate that you can’t change...

Are getter and setters ok in android if using it in another class?

java,android,android-intent,setter,getter
I read that getter and setters are not good for android programming when using the method in the same class it was created. But would there be any negative effects if using it in another class? I was told not to use many static variables so i'm looking for an...

assigning setter / getter for js object

javascript,setter
I tried to assign a setter for my js "class": function testing(){ this.value = "content"; set a(b){ this.value = b; }; } var test1 = new testing(); test1.a = "nope"; But it throws out SyntaxError: missing ; before statement on line set a(b){ could some one tell me how the...

Getter and Setter Function Help and How To

java,function,setter,getter
I am currently writing a program in which I need to incorporate get functions to produce and output for the program. The program needs to have Number of SeatsA = 30 and the pricePerSeatA = 120.99. I thought the way my program was set up all i would need to...

Create multiple properties with the same getters and setters

javascript,setter,getter,getter-setter
The problem Recently I've been writing some JavaScript program which involves the use of getters and setters. I read the MDN documentation for both methods, but I got confused trying to use them. In a nutshell, I just want to create a series of similar properties, which will have the...

Java Get and then remove from a list

java,list,setter,getter,instances
In my code i have a list of instances of a class. And i want to get a attribute of 1 instance which is ArrayList. In this class i have implement getters and setters. So I call listofinstances.get(i).getArrayList().remove(0); in order to remove the 1st item of this list. Is this...

Delphi Classes: Property vs Get/Set methods

class,delphi,properties,setter,getter
So I'm kinda new to OO programming. Delphi has properties which are intended to be a "more elegant" way to acess class data than getters/setters (read it here usage of property vs getters/setters in business classes). When should I use the fields directly and when should I use getters/setters inside...

Getter and Setter in C# [on hold]

c#,setter,getter
I'm just playing around with C# and I'm aksing myself which the proper method is for Getter and Setter. I found something like this with google: class MyClass { Button btnMyButton; // some code... public Button getBtnMyButton { get { return btnMyButton; } } } is there a 'proper' way?...