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;
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.
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.)
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.
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.
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.
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? 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; }.
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With