Setting the Property Value
Go Up to Editing the Property as Text
The property editor's SetValue method takes a string typed by the user in the Object Inspector, converts it into the appropriate type, and sets the value of the property. If the string does not represent a proper value for the property, SetValue should throw an exception and not use the improper value.
To read string values into properties, override the property editor's SetValue method.
SetValue should convert the string and validate the value before calling one of the Set methods.
Example
The following example demonstrates using the GetValue and SetValue methods for the Integer type of TIntegerProperty. Because integer is an ordinal type:
- GetValue calls GetOrdValue and converts the result to a string.
- SetValue converts the string to an integer, performs some range checking, and calls SetOrdValue.
function TIntegerProperty.GetValue: string; begin with GetTypeData(GetPropType)^ do if OrdType = otULong then // unsigned Result := IntToStr(Cardinal(GetOrdValue)) else Result := IntToStr(GetOrdValue); end; procedure TIntegerProperty.SetValue(const Value: string); procedure Error(const Args: array of const); begin raise EPropertyError.CreateResFmt(@SOutOfRange, Args); end; var L: Int64; begin L := StrToInt64(Value); with GetTypeData(GetPropType)^ do if OrdType = otULong then begin // unsigned compare and reporting needed if (L < Cardinal(MinValue)) or (L > Cardinal(MaxValue)) then // bump up to Int64 to get past the %d in the format string Error([Int64(Cardinal(MinValue)), Int64(Cardinal(MaxValue))]); end else if (L < MinValue) or (L > MaxValue) then Error([MinValue, MaxValue]); SetOrdValue(L); end;
The specifics of the particular example here are less important than the principle:
- GetValue converts the value to a string.
- SetValue converts the string and validates the value before calling one of the "Set" methods.