Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use the same getter and setter for properties?

I have a class with multiple variables, which can be accessed by their own property:

TGame = class(TObject)
   strict private
      FValue1 : Integer;
      FValue2 : Integer;
   private
      procedure SetValue1(const Value : Integer);
      procedure SetValue2(const Value : Integer);
      function GetValue1() : Integer;
      function GetValue2() : Integer;
   public
      property Value1 : Integer read GetValue1 write SetValue1;
      property Value2 : Integer read GetValue2 write SetValue2;

I am wondering, if there is a way to use the same getter and setter for different properties, like this:

property Value1 : Integer read GetValue write SetValue;
property Value2 : Integer read GetValue write SetValue;
like image 961
Maik Hasler Avatar asked Aug 23 '21 08:08

Maik Hasler


People also ask

Are getters and setters properties?

What are Getters and Setters? Getters: These are the methods used in Object-Oriented Programming (OOPS) which helps to access the private attributes from a class. Setters: These are the methods used in OOPS feature which helps to set the value to private attributes in a class.

Can getters and setters have the same name?

You can only have one getter or setter per name, on an object. (So you can have both one value getter and one value setter, but not two 'value' getters.)

What is the benefit of using properties with getters and setters?

The getter and setter method gives you centralized control of how a certain field is initialized and provided to the client, which makes it much easier to verify and debug. To see which thread is accessing and what values are going out, you can easily place breakpoints or a print statement.

Are getters and setters bad practice?

Getter and setter methods (also known as accessors) are dangerous for the same reason that public fields are dangerous: They provide external access to implementation details. What if you need to change the accessed field's type? You also have to change the accessor's return type.

Can you put getters and setters in the same descriptor?

An important note: a property must either be an accessor property or a data property. Trying to put both in the same descriptor will lead to an error like this: Getters and setters can be usefully performed as wrappers over property values for gaining more control over operations.

What is getter and setter parameter in Java?

In case of property getters and setters, it is the property for which you want to add getter and/or setter method. For new property, the last parameter is for object with descriptors, such as enumerable, configurable, value and so on.

Why use getters and setters?

Why use getters and setters? Getters and setters allow you to control how important variables are accessed and updated in your code. For example, consider this setter method: public void setNumber(int number) { if (number < 1 || number > 10) { throw new IllegalArgumentException(); } this.number = num; }.

What is the difference between get and getter method in JavaScript?

The getter method looks like a regular object method. The difference is the get keyword. This get keyword is what tells JavaScript that you don’t want to create regular object method, but a getter method. The way to use this keyword is to put it as first, before the name of the getter method.


Video Answer


1 Answers

Yes, this can be achieved using index specifiers:

Index specifiers allow several properties to share the same access method while representing different values.

For example,

type
  TTest = class
  strict private
    FValues: array[0..1] of Integer;
    function GetValue(Index: Integer): Integer;
    procedure SetValue(Index: Integer; const Value: Integer);
  public
    property Value1: Integer index 0 read GetValue write SetValue;
    property Value2: Integer index 1 read GetValue write SetValue;
  end;

{ TTest }

function TTest.GetValue(Index: Integer): Integer;
begin
  Result := FValues[Index];
end;

procedure TTest.SetValue(Index: Integer; const Value: Integer);
begin
  FValues[Index] := Value;
end;

Of course, this also works with your original private fields:

type
  TTest = class
  strict private
    FValue1: Integer;
    FValue2: Integer;
    function GetValue(Index: Integer): Integer;
    procedure SetValue(Index: Integer; const Value: Integer);
  public
    property Value1: Integer index 1 read GetValue write SetValue;
    property Value2: Integer index 2 read GetValue write SetValue;
  end;

{ TTest }

function TTest.GetValue(Index: Integer): Integer;
begin
  case Index of
    1:
      Result := FValue1;
    2:
      Result := FValue2;
  else
    raise Exception.Create('Invalid index.');
  end;
end;

procedure TTest.SetValue(Index: Integer; const Value: Integer);
begin
  case Index of
    1:
      FValue1 := Value;
    2:
      FValue2 := Value;
  end;
end;

But it almost seems like you would rather need an array property:

type
  TTest = class
  strict private
    FValues: array[0..1] of Integer;
    function GetValue(Index: Integer): Integer;
    procedure SetValue(Index: Integer; const Value: Integer);
  public
    property Values[Index: Integer]: Integer read GetValue write SetValue;
  end;

{ TTest }

function TTest.GetValue(Index: Integer): Integer;
begin
  Result := FValues[Index];
end;

procedure TTest.SetValue(Index: Integer; const Value: Integer);
begin
  FValues[Index] := Value;
end;
like image 94
Andreas Rejbrand Avatar answered Oct 18 '22 19:10

Andreas Rejbrand