Recent

Author Topic: Assigning values to multiple variables in an object in one command  (Read 3353 times)

fatmonk

  • Sr. Member
  • ****
  • Posts: 252
I have an object defined as follows:
Code: Pascal  [Select][+][-]
  1. type
  2.   TcolourScheme = Object
  3.     fg: TColor;
  4.     bg: TColor;

And I'm creating instances of the object as follows:

Code: Pascal  [Select][+][-]
  1. var
  2.   blackOnWhite, whiteOnBlack: colourScheme;
  3.  

All working good so far, but I'm initialising these objects as follows:

Code: Pascal  [Select][+][-]
  1. blackOnWhite.fg:=clBlack;
  2. blackOnWhite.bg:=clWhite;
  3.  

Is there any way to initialise these in a single command, I cannot figure out the syntax to do this at all?

I thought I maybe needed to use a 'setter' procedure, but couldn't figure out the syntax for that either as I was getting errors that I'd defined the setter procedure twice.

Can the object be initialised in a single command? And are 'setters' and 'getters' relevant in pascal?

Thanks,

FM

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: Assigning values to multiple variables in an object in one command
« Reply #1 on: March 21, 2018, 04:26:36 pm »
Turbo Pascal object  : https://www.freepascal.org/docs-html/ref/refse31.html#x63-810005.4

Code: Pascal  [Select][+][-]
  1. type
  2.   TcolourScheme = Object
  3.     fg: TColor;
  4.     bg: TColor;
  5.     constructor Init;
  6.   end;
  7.  
  8. constructor TcolourScheme.Init;
  9. begin
  10.    fg := clBlack,
  11.    bg := clWhite;
  12. end;
  13.  
  14. var
  15.   blackOnWhite, whiteOnBlack: TcolourScheme;
  16. begin
  17.    blackOnWhite.Init;
  18.    whiteOnBlack.Init;
  19. end;
  20.  

Note that classic Turbo Pascal object doesn't support setters or getters. You need Delphi/Free Pascal class for that.

Info on classes : https://www.freepascal.org/docs-html/ref/refch6.html#x68-900006

fatmonk

  • Sr. Member
  • ****
  • Posts: 252
Re: Assigning values to multiple variables in an object in one command
« Reply #2 on: March 21, 2018, 04:54:28 pm »
Surely that code always initialises to black foreground and white background.

That's not what I need.

I need to create various instances of the object with various different values for fg and bg.

Right now I am setting fg and bg separately with two commands.

I'd like to do something like the following (I know this is not the correct syntax, but is just to illustrate what I want to achieve)

Code: Pascal  [Select][+][-]
  1. var
  2.   blackOnWhite, whiteOnBlac, greenOnBlack: TcolorScheme;
  3.  
  4. blackOnWhite:=(clBlack, clWhite);
  5. whiteOnBlack:=(clWhite,clBlack);
  6. greening lack:=(clLime,clBlack);
  7.  

-FM

Thaddy

  • Hero Member
  • *****
  • Posts: 14203
  • Probably until I exterminate Putin.
Re: Assigning values to multiple variables in an object in one command
« Reply #3 on: March 21, 2018, 05:16:45 pm »
Note that classic Turbo Pascal object doesn't support setters or getters. You need Delphi/Free Pascal class for that.
Yes, but Delphi, ObjFpc mode and I think - not sure - even Fpc mode supports it. So you would have to explicitly define {$mode tp} which is rare.

In the case of FM I would simply write a constructor for the object that takes FG and BG as parameters. (And that needs not to be called "init" except in TP mode).
« Last Edit: March 21, 2018, 05:18:55 pm by Thaddy »
Specialize a type, not a var.

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: Assigning values to multiple variables in an object in one command
« Reply #4 on: March 21, 2018, 06:02:43 pm »
I need to create various instances of the object with various different values for fg and bg.
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$MODE FPC}
  3.  
  4. uses Graphics;
  5.  
  6. type
  7.   TColorScheme = object
  8.   strict private
  9.     FBackground: TColor;
  10.     FForeground: TColor;
  11.   public
  12.     procedure SetFB(AForeground, ABackground: TColor);
  13.     property Foreground: TColor read FForeground;
  14.     property Background: TColor read FBackground;
  15.   end;
  16.  
  17. procedure TColorScheme.SetFB(AForeground, ABackground: TColor);
  18. begin
  19.   FForeground := AForeground;
  20.   FBackground := ABackground;
  21. end;
  22.  
  23. var
  24.   BlackOnWhite, WhiteOnBlack, GreenOnBlack: TColorScheme;
  25. begin
  26.   BlackOnWhite.SetFB(clBlack, clWhite);
  27.   WhiteOnBlack.SetFB(clWhite, clBlack);
  28.   GreenOnBlack.SetFB(clLime, clBlack);
  29. end.

fatmonk

  • Sr. Member
  • ****
  • Posts: 252
Re: Assigning values to multiple variables in an object in one command
« Reply #5 on: March 22, 2018, 10:58:48 am »
That looks like exactly the kind of thing I am looking for an was trying.

However, I get the same error with this code as I was when trying myself:

Code: Pascal  [Select][+][-]
  1. mainunit.pas(26,24) Error: Function is already declared Public/Forward "SetFB(TGraphicsColor;TGraphicsColor);"

(Note that in my code line 26 is equivalent to line 17 in your example).

Thanks,

FM

fatmonk

  • Sr. Member
  • ****
  • Posts: 252
Re: Assigning values to multiple variables in an object in one command
« Reply #6 on: March 22, 2018, 11:29:36 am »
OK, that error was a silly one.. I needed to move the procedure specification into the implementation section...

I've also moved the FForeground and FBackground properties to into the public space so that they can also be set individually if needed.

-FM

fatmonk

  • Sr. Member
  • ****
  • Posts: 252
Re: Assigning values to multiple variables in an object in one command
« Reply #7 on: March 22, 2018, 11:56:10 am »
In fact, as I want access to the values both individually and via the setting function I've managed to simplify things even further.
Code: Pascal  [Select][+][-]
  1. type
  2.  
  3. TColourScheme = object
  4.   public
  5.     Foreground: TColor;
  6.     BackGround: TColor;
  7.     procedure setColours(AForeground, ABackground: TColor);
  8.   end;

Under implementation section:

Code: Pascal  [Select][+][-]
  1. procedure TColourScheme.setColours(AForeground, ABackground: TColor);
  2. begin
  3.   Foreground := AForeground;
  4.   BackGround := ABackground;
  5. end;
  6.  

I can now define colour schemes two ways:

Code: Pascal  [Select][+][-]
  1.   greenOnBlack.setColours(clLime,clBlack);
  2.  
  3.   whiteOnBlack.Foreground:=clWhite;
  4.   whiteOnBlack.BackGround:=clBlack;
  5.  

(Note, I also swapped some of the variable names a bit to make the exposed variables more friendly.)

So all looking good now.

-FM

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Re: Assigning values to multiple variables in an object in one command
« Reply #8 on: March 22, 2018, 03:10:33 pm »
In fact, as I want access to the values both individually and via the setting function I've managed to simplify things even further.
It is common practice for an object to control the change of its fields. Therefore, usually, fields are placed in the private section, and in the public section only properties with access procedures are left.

 

TinyPortal © 2005-2018