0

I am having issues debugging the following code, I really need help as we have an exam tomorrow! Whenever I try to change the combo box the program stops responding. Please let me know if you spot the issue, I'm sure it's glaringly obvious...

Thanks in advance Chris

Edit: I'm really new here, I'm sorry that I wasn't more specific. I have added all of the code. The only reason that I didn't initially do that is because I thought that the issue was most likely with the TForm1.ComboBox1Change procedure, because the .exe crashes (windows says that it is not responding, I have attached an image) whenever I change the combobox. I am sorry if I wasted anyone's time. I was also incorrect when I said that I was having issues debugging it. What I mean is that I am struggling to find the issue that is causing this. I have also attached the task brief.

unit VehicleManager_u;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Car, Vcl.StdCtrls, Vcl.Samples.Spin;

type
  TForm1 = class(TForm)
    ComboBox1: TComboBox;
    Edit1: TEdit;
    SpinEdit1: TSpinEdit;
    Edit2: TEdit;
    Button1: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
  private
    { Private declarations }
    Car1 : TCar;
    Car2 : TCar;
    Car3 : TCar;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
  begin
    Car1 := TCar.Create('', 0, 0.0);
    Car2 := TCar.Create('', 0, 0.0);
    Car3 := TCar.Create('', 0, 0.0);
  end;

procedure TForm1.ComboBox1Change(Sender: TObject);
  begin

    If ComboBox1.Text = 'Car 1' then
      begin
        Edit1.Text := Car1.GetMake;
        SpinEdit1.Value := Car1.GetYearMade;
        Edit2.Text := FloatToStr(Car1.GetValue);
      end

    Else if ComboBox1.Text = 'Car 2' then
      begin
        SpinEdit1.Value := Car2.GetYearMade;
        Edit2.Text := FloatToStr(Car2.GetValue);
      end

    Else if ComboBox1.Text = 'Car 3' then
      begin
        Edit1.Text := Car3.GetMake;
        SpinEdit1.Value := Car3.GetYearMade;
        Edit2.Text := FloatToStr(Car3.GetValue);
      end;
  end;

procedure TForm1.Button1Click(Sender: TObject);
  begin

    If ComboBox1.Text = 'Car 1' then
      begin
        Car1.SetMake(Edit1.Text);
        Car1.SetYearMade(SpinEdit1.Value);
        Car1.SetValue(StrToFloat(Edit2.Text));
      end

    Else if ComboBox1.Text = 'Car 2' then
      begin
        Car2.SetMake(Edit1.Text);
        Car2.SetYearMade(SpinEdit1.Value);
        Car2.SetValue(StrToFloat(Edit2.Text));
      end

    Else if ComboBox1.Text = 'Car 3' then
      begin
        Car3.SetMake(Edit1.Text);
        Car3.SetYearMade(SpinEdit1.Value);
        Car3.SetValue(StrToFloat(Edit2.Text));
      end;
  end;

end.

Here is the code from the object:

unit Car;

interface
  type
    TCar = class(TObject)

    private
      Make : String;
      YearMade : Integer;
      Value : Double;

    public
      constructor Create(MakeIn : String; YearMadeIn : Integer; ValueIn : Double);
      procedure SetMake(MakeIn : String);
      function GetMake: String;
      procedure SetYearMade(YearMadeIn : Integer);
      function GetYearMade : Integer;
      procedure SetValue(ValueIn : Double);
      function GetValue : Double;
      function ToString : String;
    end;

implementation

  uses
    SysUtils;

  constructor TCar.Create(MakeIn : String; YearMadeIn : Integer; ValueIn : Double);
    begin
      Make := MakeIn;
      YearMade := YearMadeIn;
      Value := ValueIn;
    end;

  procedure TCar.SetMake(MakeIn: string);
    begin
      Make := MakeIn;
    end;

  function TCar.GetMake;
    begin
      Result := GetMake;
    end;

  procedure TCar.SetYearMade(YearMadeIn: Integer);
    begin
      YearMade := YearMadeIn;
    end;

  function TCar.GetYearMade;
    begin
      Result := GetYearMade;
    end;

  procedure TCar.SetValue(ValueIn: Double);
    begin
      Value := ValueIn;
    end;

  function TCar.GetValue;
    begin
      Result := GetValue;
    end;

  function TCar.ToString;
    begin
      Result := 'Make: ' + Make + ', Year Made: ' + IntToStr(YearMade) + ', Vale: R' + FloatToStr(Value);
    end;
end.

Here is a screenshot of the "error" message

Task brief

LU RD
  • 34,438
  • 5
  • 88
  • 296
Anon
  • 9
  • 4
  • 2
    You say you're having a problem with debugging. You've described the problem that the code has ("stops responding"), but you haven't described what problem you're having with debugging of that problem. Does the debugger's "pause" button not work? Are breakpoints not stopping where you set them? Can you not inspect the values of variables? What advice has your instructor given you? The only glaringly obvious problem *I* see is that there's no code for `TCar` present in the question, so I can't possibly comment on what's wrong with it. – Rob Kennedy Nov 08 '16 at 15:29
  • I wouldn't be surprised if you have an infinite loop : your event changing the text of a component, raising its Onchange event which changes again its text, ... ... ... You have to set a break point inside your events (I would set a breakpoint in the first line of all your events), and when the debugger stops there, execute it line by line, so you will be able to see your infinite loop. Otherwise you will see which exact line causes your application to hang. Come back and tell us this line and we may be able to help your further. – Marc Guillot Nov 08 '16 at 15:46
  • @MarcGuillot: "your event changing the text of a component, raising its Onchange event which changes again " If it were that, wouldn't it manifest itself, rather rapidly, as a stack overflow? Which wouldn't give the effect of a hang. But I agree the problem must involve code the OP has not shown. – MartynA Nov 08 '16 at 17:38
  • 5
    The problem is with those `Get....` methods of `TCar`. All three are calling themselves recursively instead of returning the value of the corresponding variable in the class. It would have bees simple to spot that in the debugger. – Tom Brunberg Nov 08 '16 at 19:19
  • Thanks so much, that solved it! I knew it was something stupid – Anon Nov 08 '16 at 19:27
  • 4
    @ChristopherAubin The solution to this particular problem may have been stupid, but the real lesson you should learn from this is that it is critically important to learn how to use your debugger. You could simply have added a breakpoint in your code and stepped through the execution to discover this problem for yourself. If you take **that** lesson away from this episode then you will find yourself better able to solve a much broader number of problems on your own in the future. – J... Nov 08 '16 at 20:59

1 Answers1

0

I asked this question while I was in high school and was extremely inexperienced with all things coding related. The problem was pointed out by Tom Brunberg in his comment. The Get.... methods of TCar were calling themselves recursively instead of returning the value of the corresponding variable in the class. However, as pointed out by both J... and Tom Brunberg (thanks to both), the real issue was that I didn't know how to use my debugger (or really what debugging was). If I had made use of breakpoints and stepped through my code this would have immediately become apparent.

Anon
  • 9
  • 4