Recent

Author Topic: Adding integers?  (Read 10072 times)

A

  • Guest
Adding integers?
« on: December 26, 2017, 06:15:33 pm »
Hello everyone :)

I've decided to learn Object Pascal(?) and have just read a basic tutorial online.

I'm up to the stage of adding numbers together. So, I've created my application, I have a button, and a text-field. This is the code that I have so far:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     BtnCalcSum: TButton;
  16.     BtnCalcAvg: TButton;
  17.     Edit1: TEdit;
  18.     procedure BtnCalcSumClick(Sender: TObject);
  19.     procedure FormCreate(Sender: TObject);
  20.   private
  21.     { private declarations }
  22.   public
  23.     { public declarations }
  24.   end;
  25.  
  26. const
  27.   Number1 = 45;
  28.   Number2 = 7;
  29.   Number3 = 68;
  30.   Number4 = 2;
  31.   Number5 = 34;
  32.  
  33. var
  34.   Form1: TForm1;
  35.  
  36. implementation
  37.  
  38. {$R *.lfm}
  39.  
  40. { TForm1 }
  41.  
  42. procedure TForm1.FormCreate(Sender: TObject);
  43. begin
  44.   Edit1.ReadOnly := True
  45. end;
  46.  
  47. procedure TForm1.BtnCalcSumClick(Sender: TObject);
  48. begin
  49.   Edit1.Text := (Number1 + Number2 + Number3 + Number4 + Number5);
  50. end;
  51.  
  52. end.

Please don't give me the code, could you just tell me what is wrong with the code, so that I could try figuring it out?

Also, am I learning Object Pascal, Pascal, or is it FreePascal?

Thanks a lot for your help, and happy boxing day :)

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Adding integers?
« Reply #1 on: December 26, 2017, 06:21:19 pm »
you need to convert your number to string in order to assign it to the text property. There are two basic ways to handle this
1) the direct approach use the function IntToStr(Number1 + Number2 + Number3 + Number4 + Number5)
2) Use a component that support numbers spinedit, floatspinedit etc and assign the number to the value property.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

A

  • Guest
Re: Adding integers?
« Reply #2 on: December 26, 2017, 06:37:17 pm »
Works perfectly now, thanks for your help!

Zoran

  • Hero Member
  • *****
  • Posts: 1977
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Adding integers?
« Reply #3 on: December 26, 2017, 06:47:29 pm »
Free Pascal is a compiler, which compiles a dialect of Object Pascal. As none of popular compilers implements some Object Pascal standard (unlike C++ for example, where there is a standard which all popular C++ compilers follow), you can say that Free Pascal is a language in a sense... So... saying that you learn Free Pascal is just more precise than saying that you learn Object Pascal, which is also true.

And before you start learning object oriented concepts, you should learn plain Pascal concepts and one of those is strong typing. See fundamental Pascal types here: https://www.freepascal.org/docs-html/current/ref/refch3.html
Strong typing means that you cannot mix types -- for example, if you declare a String variable, you cannot assign an Integer to this variable -- you first need to convert this integer somehow to string.

So, this is what you should do to be able to assign your sum to edit's text property -- convert it to string first. The easiest way to do it is, as tazz suggests, IntToStr function.
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

sash

  • Sr. Member
  • ****
  • Posts: 366
Re: Adding integers?
« Reply #4 on: December 26, 2017, 06:49:24 pm »
what is wrong with the code...?

Incompatible types in assignment.
You're assigning right value of type integer to the left variable (property) of type string.
Lazarus 2.0.10 FPC 3.2.0 x86_64-linux-gtk2 @ Ubuntu 20.04 XFCE

Handoko

  • Hero Member
  • *****
  • Posts: 5508
  • My goal: build my own game engine using Lazarus
Re: Adding integers?
« Reply #5 on: December 26, 2017, 06:50:17 pm »
Hello A,
Welcome to the forum.

Please don't give me the code, could you just tell me what is wrong with the code, so that I could try figuring it out?

Usually people will ask for the code but you ask not to get the code.
~ :) ~ Respect ~ :) ~

In the link below has a list of useful tutorials, docs, videos that should serve a good start for you:
http://forum.lazarus.freepascal.org/index.php/topic,39286.msg269090.html#msg269090

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Adding integers?
« Reply #6 on: December 26, 2017, 06:55:32 pm »
Free Pascal is a compiler, which compiles a dialect of Object Pascal.

Well, it can compile several different Pascal dialects.

Also, am I learning Object Pascal, Pascal, or is it FreePascal?

You're learning Object Pascal, but most people call it Pascal for short.
The original Pascal language published by N. Wirth in the early 1970s used a procedural syntax, and did not provide for classes or object-orientation (which is particularly valuable for GUI programming).
Borland and others extended Wirth's language with object-oriented constructs such as inheritable classes and an event model, later used in their Delphi product and VCL framework.
Free Pascal is an open source compiler (not a language) that is Delphi-compatible. It does provide its own Pascal dialect (specified via the compiler directive {$mode objfpc} in your code) which differs in a few minor ways from classic Delphi syntax; differences that need not trouble a beginner.

A

  • Guest
Re: Adding integers?
« Reply #7 on: December 26, 2017, 07:18:37 pm »
Thanks for the clarification regarding Free Pascal and Object Pascal.

@Zoran & Handoko; thanks a lot for the links, I'll check them out.

It's great to see such an active forum for a language that people claim to be "dead". I'm looking forward to learning Pascal as my first language!

If I'm missing an 'upvote' or 'mark as answer' feature on this forum, please let me know, otherwise, thank you for your help :)

simone

  • Hero Member
  • *****
  • Posts: 681
Re: Adding integers?
« Reply #8 on: December 26, 2017, 07:45:20 pm »
Pascal will never die... Good and popular programming languages are subject to Lindy effect...
Microsoft Windows 10/11 64 bit - Lazarus 3.8/4.0 FPC 3.2.2 x86_64-win64-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 18529
  • Here stood a man who saw the Elbe and jumped it.
Re: Adding integers?
« Reply #9 on: December 26, 2017, 08:06:41 pm »
@A

I don't necessarily want to give you code, but what I want to do is make you aware of advanced Pascal features that make life easier for newbie's:
You can write:
Code: Pascal  [Select][+][-]
  1.   Edit1.Text := (Number1 + Number2 + Number3 + Number4 + Number5).ToString; // simple types like integer have methods when sysutils is included. Like: integer.ToString

This is because this solution is rather new and is overlooked by many of us on this forum.
« Last Edit: December 26, 2017, 08:11:32 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Adding integers?
« Reply #10 on: December 26, 2017, 08:11:28 pm »
please post the minimum lazarus version of features recently released (like the tostring helper).
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 18529
  • Here stood a man who saw the Elbe and jumped it.
Re: Adding integers?
« Reply #11 on: December 26, 2017, 08:12:30 pm »
please post the minimum lazarus version of features recently released (like the tostring helper).
As usual: current FPC release, this is not Lazarus, but it works in Lazarus. sysutils is FPC rtl...
.. older versions are not supported by me. Current FPC release is 3.0.4.
If you have Laz 1.8, FPC 3.0.4, this code works. You don't need trunk.
This code is explicitly not for A!!!: 8-) 8-) >:D
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9. const
  10.   Number1 = 45;
  11.   Number2 = 7;
  12.   Number3 = 68;
  13.   Number4 = 2;
  14.   Number5 = 34;
  15.  
  16.  
  17. type
  18.  
  19.   { TForm1 }
  20.  
  21.   TForm1 = class(TForm)
  22.     Button1: TButton;
  23.     Edit1: TEdit;
  24.     procedure Button1Click(Sender: TObject);
  25.   private
  26.  
  27.   public
  28.  
  29.   end;
  30.  
  31. var
  32.   Form1: TForm1;
  33.  
  34. implementation
  35.  
  36. {$R *.lfm}
  37.  
  38. { TForm1 }
  39.  
  40. procedure TForm1.Button1Click(Sender: TObject);
  41. begin
  42.     Edit1.Text := (Number1 + Number2 + Number3 + Number4 + Number5).ToString;
  43. end;
  44.  
  45. end.
:D :D :D :D :) :) ;D ;D

Note the typehelper compiler feature  for simple types was backported to 3.0.2. introduced in 3.0.0 release. The typehelpers itself(implementation) were added to sysutils in 3.0.0 fixes? and are in the official 3.0.4. release (look at syshelph.inc under /rtl/sysutils). Which is what you should use.
@A: this is not of any interest to you... just use the current release versions.
« Last Edit: December 26, 2017, 09:43:56 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

A

  • Guest
Re: Adding integers?
« Reply #12 on: December 26, 2017, 08:43:03 pm »
Thank you Thaddy. :) Next time I need to do an Int -> String conversion, I'll use this - very simple.

Thaddy

  • Hero Member
  • *****
  • Posts: 18529
  • Here stood a man who saw the Elbe and jumped it.
Re: Adding integers?
« Reply #13 on: December 26, 2017, 08:58:53 pm »
It is a feature I requested myself and the actual request is 0023817 in Mantis. Sven Barth was so kind to implement it.
Actually the compiler support was already in 3.0.0 release, the sysutils helper implementations in 3.0.2.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

bytebites

  • Hero Member
  • *****
  • Posts: 767
Re: Adding integers?
« Reply #14 on: December 26, 2017, 09:07:29 pm »
Code: Pascal  [Select][+][-]
  1. Number.ToString
is Japlhi way to do conversion.  :D

 

TinyPortal © 2005-2018