Questions tagged [getter-setter]

Public mutator methods in object-oriented programming, responsible for changing and returning the value of private class members, thus keeping with the principle of encapsulation.

Filter by
Sorted by
Tagged with
0 votes
0 answers
18 views

How to mark a Scala var as partially overriding a parent

Consider the following simple trait trait Parent { def x: Int = 0 } Now consider an implementing class that wishes to define x to be mutable. class Child extends Parent { var x: Int = 1 } This ...
Silvio Mayolo's user avatar
-1 votes
0 answers
33 views

Java - Should I use getter in method? [migrated]

I have two working solutions, but which one should I use? Which solution is correct? Solution 1: List<Client> myClient = new ArrayList<>(); public class Client { private final String ...
Qmails's user avatar
  • 73
-1 votes
1 answer
61 views

Automatic updates for class attributes when modifying a list-like attribute in Python using __set__ and __setitem__

I have a class with an attribute ax_mask. The attribute is list like. When I change the whole attribute or just an element of it, I need some other attribute (window) in the class to be updated (i.e. ...
fuhrmanj's user avatar
0 votes
0 answers
15 views

Efficiently writing setters for property objects with similar attributes but different validations in Python

My Python class has an extensive list of properties. The properties share a similar structure bu have different validation conditions, which necessitates I write separate property setting functions ...
Achintya Agarwal's user avatar
0 votes
2 answers
70 views

I want to use get set method in field of data class kotlin

data class Thermostat( // ... other properties @SerializedName("units") private var _units: String?, // Fahrenheit, // ... other properties ) : Serializable { var units:...
Mohammad Taqi's user avatar
-1 votes
0 answers
49 views

Why does Python require a setter for an attribute, for which I don't even try to set a new value?

I was experimenting with json in python and got an Error because of @property. I wrote some code that reads in a txt file into a dict object, changes the structure of the json object and then writes ...
Begüm's user avatar
  • 1
0 votes
1 answer
26 views

How can I determine if a class member is a getter or setter?

I am trying to use ts-morph to validate code in a transpiler I am writing. No it is not a TypeScript transpiler, but I need to support both JS and TS code within my original file. I have the following ...
Intervalia's user avatar
  • 10.5k
-2 votes
4 answers
169 views

Is there a way to keep track of an object-property's value-history, like memorizing and recalling any of its recent values?

Say I have a method that changes a value based on what the current value is. I will just make it very simple here: changeValue(){ if(!this.value){ this.value = 'value1' } else if(...
Scandidi's user avatar
1 vote
2 answers
81 views

Check C++ reference setter value

I have a question about the much-commented-about C++ reference getter-setter in C++. This is the base code: class Foo { X x_; public: X & x() { return x_; } const X & ...
eje211's user avatar
  • 2,407
0 votes
0 answers
18 views

Uncaught (in promise) TypeError: getLonAndLat is not a function [duplicate]

Running JavaScript on VS-Code. I have the following code as my main file: d3.json("./data/trueData.json").then(function (data) { let regionNameVar2; let regionLatVar2; let ...
Toyu_Frey's user avatar
  • 101
0 votes
3 answers
129 views

How to properly implement getter/setter functionality, either for both a property's `get` and `set`, or for two methods and a property's `get`?

I have a logic that need to reactively run some function if a this.variableName is changed inside or outside the class the problem is with this logic, getter doesn't work, and I get undefined. not ...
user avatar
0 votes
0 answers
26 views

Transmit object with getter/setter property via HttpClient

I have the following class: export class FileData { get name(): string { return this.#name || (this.data ? `Unnamed.${this.data.match(/[^:/]\w+(?=;|,|\+)/)?.[0]}` : '') } set name(name: string) { ...
Otto Abnormalverbraucher's user avatar
0 votes
0 answers
59 views

ObjectMapper maps Boolean field with "is" prefix to same name field without "is" prefix after some time

I have a Boolean field in the request body of an API, named "isSafe". This field is mapped to my Pojo class using ObjectMapper. If I send "safe" in the request then it fails and I ...
Amit Kumar's user avatar
0 votes
1 answer
37 views

Why is it that even after a stack overflow error, in getter and setter methods, the rest of my code still works in JavaScript?

I'm currently learning about setters and getters in Javascript but I ran into an issue. I've named a property in my class the same name as my get and set methods which I understand can lead to ...
Ana's user avatar
  • 3
1 vote
1 answer
34 views

Swift automatically set value to variable A by setting variable B

I've a question on whether or not it is possible to automatically set value to variable A when variable B gets updated. I've 2 variables set up as following: enum CurrentState: Hashable { case ...
Bob's user avatar
  • 182
-5 votes
1 answer
78 views

Fix code to create a getter/setter property backed by an underlying field [duplicate]

From https://github.com/freeCodeCamp/freeCodeCamp/blob/139eecad25760621d42e482e34b6b57c980b6094/curriculum/challenges/english/02-javascript-algorithms-and-data-structures/es6/use-getters-and-setters-...
theonerishi's user avatar
0 votes
0 answers
23 views

Sending more than one parameter in url in symfony

i want to send some parameters in array when redirecting and i want the url to keep only the first value in the array. i tried using cookies but i can't send array so i tried to json_encode & ...
helpcall's user avatar
1 vote
2 answers
116 views

How to correctly implement an own property getter/setter via Object.defineProperty and utilizing class syntax?

it's my code "use strict"; class Employee { constructor(firstName, lastName, age, salary, gender) { this._firstName = firstName; this._lastName = lastName; this._age = age; ...
Delfon's user avatar
  • 39
-1 votes
1 answer
49 views

In Python ,why does it end up in recursion while accessing private attribute from getter() ,without using double underscore in getter()

class Person(): def __init__(self,name:str,height :float) -> None: #initialize self.__name=name self.height=height @property def name(self) -> str: ...
Armaan Sahoo's user avatar
0 votes
1 answer
32 views

Accessor magic method not working in PHP 8 [duplicate]

I recently upgraded Ubuntu from 20.04 LTS to 22.04 LTS. I noticed that PHP was upgraded to the following version: PHP 8.1.2-1ubuntu2.13 (cli) (built: Jun 28 2023 14:01:49) (NTS) Copyright (c) The PHP ...
Jaime Montoya's user avatar
0 votes
0 answers
54 views

SqlAlchemy setter, getter

I was interested in one question. Usually, in order to get / put data from the database and decode / encode the logic is written in routers or in separate functions that are called anyway in routers. ...
Aram's user avatar
  • 1
2 votes
1 answer
36 views

How to get values from one module to another in gradle java project?

I created two modules in my project. I want to get value from one module to another module. my project's github link In my first module : package org.example; public class Main { static String c;...
pradeep's user avatar
  • 21
0 votes
1 answer
97 views

Lombok gives java: unreported exception java.lang.Exception; must be caught or declared to be thrown

If I am not wrong, Lombok's @Data gives the getter, setter and other util methods. Developer can customize its generated methods. And I did that before. But recently I have come across a scenario ...
Md. Saiful Islam's user avatar
0 votes
3 answers
138 views

Which UML class diagram is more accurate for Kotlin class attributes?

I want to create a UML class diagram to represent a Kotlin class. I have come across a few options and came up with two alternatives. However, I'm unsure which of the two is the most appropriate and ...
Ali Dehkhodaei's user avatar
2 votes
1 answer
60 views

Should I explicitly implement getName(), getAddress(), and setAddress() in my Kotlin code based on the UML?

I have implemented the following UML in Kotlin, and I am now considering whether I should explicitly implement the methods getName(), getAddress(), and setAddress() in my code, as they are mentioned ...
Ali Dehkhodaei's user avatar
0 votes
2 answers
60 views

Why do getters work differently in this two examples?

Why when we have this function Car() { const fuel = 50 return { fuel, } } const car = Car() console.log(car.fuel) // 50 car.fuel = 3000 console.log(car.fuel) // 3000 And if we add a getter,...
user3303019's user avatar
3 votes
2 answers
115 views

What are the mechanics of Python decorator for property setter and deleter?

The subject of Python properties is covered extensively here, and the Python documentation provides a pure Python implementation here. However, I am still not fully clear on the mechanics of the ...
Vahan's user avatar
  • 51
0 votes
0 answers
48 views

How to decrement an item Quantity from an array in java if there are multiple classes?

Why does when a user purchase an item, the quantity of the item is not deducting or updating and still it is the same? what should i do to correct the implementation of this? public ...
Dustin Minrod's user avatar
0 votes
0 answers
31 views

Strange behavior with += and getters and setters of numpy arrays [duplicate]

Getters and setters seem to behave weirdly when performing += with numpy arrays due to in-place mutation: a += b behaves differently from a = b + a when b is a numpy array. A minimal example is the ...
abeta201's user avatar
  • 233
1 vote
1 answer
46 views

Having trouble in the setCoordinates function

I am a newbie in React JS and I am following the below tutorial (application 1): https://www.youtube.com/watch?v=GDa8kZLNhJ4&list=PL6QREj8te1P6wX9m5KnicnDVEucbOPsqR&index=10 Specifically from ...
angad gosain's user avatar
0 votes
2 answers
88 views

Default Property value for attribute in c#

using Newtonsoft.Json; using System; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; namespace Logistics.ShippingRoutes.Domain.Models { /// <summary&...
Pavan K's user avatar
0 votes
0 answers
32 views

Why does my Velocity Template conditional assignment not work properly?

I am trying to create custom Getters using Velocity Template that are annotated with a @Generated annotation. My current template looks like this: #if($field.recordComponent) @Generated public ${field....
MrMyth's user avatar
  • 67
0 votes
1 answer
46 views

Setter not running as part of constructor

void main() { CarCarCar car1 = CarCarCar('Toyota', 'Corolla', 1983); CarCarCar car2 = CarCarCar('', '', 2014); List<CarCarCar> cars = [car1, car2]; for (var car in cars) { print(...
UF7K's user avatar
  • 3
0 votes
0 answers
55 views

How to get getter value in functions other than JSX

I'm using watermelondb for local storage in react js. there are multiple columns which i can access directly but one colum role_id i don't want to fetch it as role_id i want to convert it into role. ...
web pakistan's user avatar
0 votes
0 answers
18 views

How to change color of getters and setters in the android studio?

I have recently discovered Lombok which, among other things, makes getters and setters generate automatically and invisibly. Previously I would use fields which have a nice color pink in the IDE. Even ...
Michael Efraimidis's user avatar
0 votes
1 answer
126 views

Java: NoSuchMethodError when using getters and setters to access private variables from another class [closed]

-I am understanding working of getters and setters in java. I am trying to access private variables(id, name) of other class through -methods like getName(),setName(). but my main method is not able ...
Anurag Shirvatkar's user avatar
0 votes
2 answers
214 views

Build getters and setters lambdas to avoid using reflection in c#

I am validating a lot of complex objects in my applications. For example: public class School{ public string Name {get;set;} public DateTime DateCreated {get;set;} public List<Person > ...
Tono Nam's user avatar
  • 34.7k
0 votes
2 answers
37 views

Python: Overload compare method of property variable

I want to overload a class method(s) (__eq__, __lt__, ...) of a property value. Has anybody an idea? Here is my propery value for version: class Element(): def __init__(self): self....
Georg Bauer's user avatar
0 votes
0 answers
18 views

Create a generic class

Write a generic class Pair which has two type parameters—F and S—each representing the type of the first and second element of the pair, respectively. Add set and get methods for the first and second ...
Noah Riley's user avatar
0 votes
1 answer
61 views

A getter in a non-virtual interface with a chained method

I have the following interface that can return a map with a pair of int and string for key and string as a value. class Foo { public: virtual ~Foo() = default; Foo& operator=( ...
Namal's user avatar
  • 281
0 votes
0 answers
28 views

Angular getFoo() vs get foo. Is there a benefit to getter/setter syntax, or is it just preference?

My background is primarily in Java, so I'm very used to using getter/setter methods, but in Angular I know there is a different getter/setter syntax. Is this purely a preference, or are there ...
SpecialK711's user avatar
-1 votes
1 answer
39 views

How to activate a setter method without input in the main program code?

I'm working on a program that needs to check if a value is an integer before applying it to an attribute of a class. This must be done in a setter ("set") method. internal class Dummy { ...
somebody1122's user avatar
0 votes
1 answer
440 views

Visual Studio Format getter and setter on one line

I just scaffolded a project using Template Studio WinUI v5.3 and when I press Ctrl K D to format document, the getter and setter is placed on new lines. current.cs public class MyClass { public ...
Mihai Socaciu's user avatar
0 votes
0 answers
43 views

simple question on Getters/Setters (accessors and mutators) in android

Been stuck on this for two days embarrassingly. Getters and setters not working. Just causing a fatal exception. Please see code below. SearchNumber.java is the getter/setter method. All the app does ...
Tim W Newark's user avatar
1 vote
1 answer
54 views

Why does the getter/setter methods get inherited as a property in child object?

I was exploring the Object.create() method and came across something interesting. When I passed an Object with a getter method in it (made using get keyword), the getter method was implicitly present ...
Jay Mehta's user avatar
0 votes
3 answers
514 views

Kotlin - Extension Boolean property that returns true and then false (and so on)

I want an object to have a boolean property that returns true when I first get it, and the false, in a second time I get it. This must repeat every time I call it (if it is an even call, returns true; ...
Leonardo Sibela's user avatar
0 votes
2 answers
100 views

Flutter class not initialized

I am using a login on my app and I am trying to use getter and setter for my User class. I can't get the values from the class because the app says not initialised. First I sign up from my profile ...
Robban13's user avatar
1 vote
1 answer
61 views

What is causing the second program to return a "cannot find symbol" error (using Getter-Setter methods)? [duplicate]

I'm working on a college exercise. It requires me to use "getter and setter" methods in the program "Sandwich.java" so that the references in "TestSandwich.java" may use ...
Takeshi DBXV's user avatar
-3 votes
1 answer
69 views

Where does a local variable get stored, and how can I define a getter/setter for one? [duplicate]

So in JavaScript, I know that when you define a variable in the global scope, like this, let a = 0; console.log(a); // > 0 it's (more or less) the same as defining a property on the window object: ...
Magnogen's user avatar
0 votes
1 answer
48 views

Different getter methods for returning instance vs. instance variable based on position in dot notation?

Is there a way to implement two different getter methods in Python, i.e. one that returns an instance and one that returns the instance attribute (depending on where the getter is called in dot ...
asti205's user avatar
  • 164

1
2 3 4 5
44