1

I need to make a program that will input and display the info of a product(brand name, contents, mass and how much of it is in stock).

Problem: The input boxes in this piece of code does not completely display in order even though the code is written to display it in order. The input boxes are in this order:"Contents", "Mass", "Stock", "Brand name" when it should actually be "Brand name", "Contents", "Mass", "Stock".(These are the captions of the input boxes).

-I think that this might be the source(Scroll down for the rest of the program):

 StrObj := TProducts.Create(Inputbox('Brand name', 'Input the brand name', ''),Inputbox('Contents', 'Input the contents', ''), StrToInt(Inputbox('Mass', 'Input the mass', '')), StrToInt(Inputbox('Stock', 'Input the number in stock', '')));

Note:

-All the data goes where it is intended to go, therefore the order of the input boxes don't affect the program at all. I would just like to know if there is a way to make the input boxes display in order.

-This is an assignment, but the order of the input boxes won't count marks.

Whole code:

Application:

unit TProducts_U;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, TProducts_class;

type
  TForm1 = class(TForm)
    btnResult: TButton;
    redOut: TRichEdit;
    procedure btnResultClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnResultClick(Sender: TObject);
var StrObj : TProducts;
begin
  StrObj := TProducts.Create(Inputbox('Brand name', 'Input the brand name', ''),Inputbox('Contents', 'Input the contents', ''), StrToInt(Inputbox('Mass', 'Input the mass', '')), StrToInt(Inputbox('Stock', 'Input the number in stock', '')));
  redOut.Lines.Add(StrObj.toString);
end;

end.

My class:

unit TProducts_class;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Math;

type
  TProducts = class
  private
    fBname, fContents : string;
    fMass, fNum : integer;
  public
    Constructor Create; overload;
    Constructor Create(Bname, contents : string; mass, num : integer); overload;
    Function toString : string;
  end;

implementation

{ TProducts }

constructor TProducts.Create;
begin
   fBname := '';
   fContents := '';
   fMass := 0;
   fNum := 0;
end;

constructor TProducts.Create(Bname, contents: string; mass, num: integer);
begin
   fBname := Bname;
   fContents := contents;
   fMass := mass;
   fNum := num;
end;

function TProducts.toString: string;
begin

  result := 'Brand Name is : ' + fBname + #13 +
            'Contents is : ' +  fContents + #13 +
            'Mass is : ' +  IntToStr(fMass) + #13 +
            'We have ' + IntToStr(fNum) + ' in stock';

end;

end.
Amber
  • 171
  • 1
  • 10

1 Answers1

5

Parameter evaluation order is not defined in Delphi.

You need to write separate instructions for each InputBox() call and then concatenate them afterward.

For example:

procedure TForm1.btnResultClick(Sender: TObject);
var
  StrObj: TProducts;
  sBrandName, sContents, sMass, sStock: string;
begin
  sBrandName := Inputbox('Brand name', 'Input the brand name', '');
  sContents := Inputbox('Contents', 'Input the contents', '');
  sMass := StrToInt(Inputbox('Mass', 'Input the mass', ''));
  sStock := StrToInt(Inputbox('Stock', 'Input the number in stock', ''))
  StrObj := TProducts.Create(sBrandName, sContents, sMass, sStock);
  // Added try .. finally, otherwise you will leak StrObj
  try
    redOut.Lines.Add(StrObj.toString);
  finally
    StrObj.Free;
  end;
end;
Tom Brunberg
  • 20,312
  • 8
  • 37
  • 54