What is Delphi's equivalent to "+=" for adding elements to a set?
Asked Answered
F

4

13

In other languages like C++, there are operators to do a plus-equals or or-equals type of operation to add additional styles/flags. Is there an equivalent in Delphi?

Right now I have some code like:

label1.Font.Style := label1.Font.Style + [fsBold];
label2.Font.Style := label2.Font.Style + [fsBold];

But I would love, if it is possible, to get that simplified a bit to something more concise without duplicating the label name on both sides of the assignment operator, something along the lines of: label1.Font.Style += [fsBold]; or label1.Font.Style := self + [fsBold];

Can this be done? Or not so much?

Fundy answered 21/7, 2012 at 18:18 Comment(4)
use the 'inc' function - docwiki.embarcadero.com/Libraries/en/System.Inc. Anyway you can not use it on properties.Marshy
For sets, it wouldn't be Inc, it would be the Include procedure with the same problem that it requires a variable.Choragus
For strings, use Concat(S1, S2, S3, S4...)Thimbu
No, @Jerry, that function doesn't work that way. It doesn't modify any of its arguments, unlike Inc and Include. Furthermore, Concat is no different from the more idiomatic use of repeated + operators. (The function used to be more efficient for four or more operands, but that changed around version 5, maybe earlier.)Gibby
C
14

This is one of the few cases where I can recommend the with statement:

with label1.Font do Style := Style + [fsBold];
with label2.Font do Style := Style + [fsBold];

Normally, the problem with with is that it is unclear what's a member access and what isn't, but if the scope is a single statement in which a single property is used, it's clear enough.

Choragus answered 21/7, 2012 at 18:28 Comment(2)
What happens when with label2.Font do Style := Style + [fsBold]; is executed repeatedly?Telegraphic
what you gain is that label.Font isn't resolved twice thereCraft
O
14

Include is what you're looking for. Unfortunately you run into the problem that Label.Font.Style is a property and must be assigned to and not passed by var. You can do this however:

var
  fontStyle: TFontStyles;
begin
  fontStyle := Label1.Font.Style;
  Include(fontStyle, fsBold);
  Label1.Font.Style := fontStyle;
Osculum answered 21/7, 2012 at 18:33 Comment(0)
V
11

Delphi has the Inc function, but it does not work on properties:

Inc(a);     // Increment a with 1
Inc(a, 5);  // Increment a with 5

If you need to add style to a font often, you can write a procedure:

procedure AddStyle(const AFont: TFont; const AStyle: TFontStyles);
begin
  AFont.Style := AFont.Style + AStyle;
end;

(With later versions of Delphi you can use class helpers).

Valency answered 21/7, 2012 at 18:21 Comment(1)
+1, And I just fix your AddStyle method :), also you can add a sanple of use like so AddStyle(Label1.Font, [fsBold]);Fleck
P
0

Subclass TLabel type and create a method like this:

procedure TMyLabel.AddStyle(const AStyle: TFontStyles);
begin
 Self.Font.Style := Self.Font.Style + AStyle;
end;

Then you can put the class into a bpl package and install it into editor. After that, you can replace all your labels to your customized labels. For large project i would recommend Notepad++ to replace all your declarations in .pas and (text!) .dfm files. This may sound complicated but it's less than an hour of work. Good luck!

Pedate answered 21/7, 2012 at 18:31 Comment(2)
dont do this, the complexy you introduce to the code is far then worth the extra work. do it in a visitor pattern, (create an external function that accept TLabel and TFontStyles and do the work in a function).Lamontlamontagne
This is probably one of the worst possible reasons to create a subclass. A simple utility function does the job quite nicely, without interfering with your class hierarchy.St

© 2022 - 2024 — McMap. All rights reserved.