We help IT Professionals succeed at work.
Get Started

debug a @asClass Error //  Access Violation

584 Views
Last Modified: 2013-11-23
[i] trying to  to the following


TClassElement   = class
  ........  
  ...
 end;



aClassElement    : TClassElement

aObjList  :   TObjectList;


aClassElement    := TClassElement.Create;



aClassElement := AComplexClassDefintion.aObjList.Items as TObjectList

 


and get a fatal error with the  line aClassElement := AComplexClassDefintion.aObjList.Items[i] as TObjectList  as

the  Delphi RUNTIME says that the conversion @asClass is not correct. any tip to debug that sitatuation ???

A Test program to show that issue works perfect !!!

unit Unit_demo_objectList;
{*******************
 *
 *    simple demo for TObjectList with a class Tresolution
 *    demo works fine, but real app with this contruction gives me an error
 *
 *
 *
 *    TSimpleListClass  is a simple version of my class, a lot of stuff is missing
 *    the access
 *        aRes :=  aSimpleListClass.aRList.Items[i] as TResolution ;
 *    makes trouble in my real app !!!
 *
 ******************}
interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, SSCFileTypeUnit, Contnrs , StdCtrls;

type


      TResolution = Class
                   ppLx            :   Real  ;               ///   x direction
                   ppLy            :   Real  ;               ///   y direction
                   NAme            :   String;
                   end;



      TResolutionList= Class(TObjectList)
                      end;



  TSimpleListClass  = class

             constructor create;
             destructor  free;

             public

             aRList  :  TResolutionList;

  end;

  TForm1 = class(TForm)
    CreateAResButton: TButton;
    CreateListButton: TButton;
    FreeListButton: TButton;
    Memo1: TMemo;
    ShowObjectListButton: TButton;
    ShowObjectListButton2: TButton;
    versionCreateLISTButton: TButton;
    Addelement: TButton;
    Button2: TButton;
    FreeButton2: TButton;
    procedure CreateAResButtonClick(Sender: TObject);
    procedure CreateListButtonClick(Sender: TObject);
    procedure FreeListButtonClick(Sender: TObject);
    procedure ShowObjectListButtonClick(Sender: TObject);
    procedure ShowObjectListButton2Click(Sender: TObject);
    procedure versionCreateLISTButtonClick(Sender: TObject);
    procedure AddelementClick(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FreeButton2Click(Sender: TObject);

  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
    aRList              :  TResolutionList;
    aSimpleListClass    :  TSimpleListClass;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.CreateAResButtonClick(Sender: TObject);
var   aRes  :  TResolution;
begin
     aRes  :=TResolution.Create;

     aRes.ppLx := Random(100);


     aRList.Add(aRes);

     memo1.Lines.Add(IntToStr(aRList.Count));

end;

procedure TForm1.CreateListButtonClick(Sender: TObject);
begin
    aRList :=TResolutionList.Create;
end;

procedure TForm1.FreeListButtonClick(Sender: TObject);
begin
    aRList.Free;
end;

procedure TForm1.ShowObjectListButtonClick(Sender: TObject);
var    i     : Integer;
       aRes  :  TResolution;
begin
     for i:= 0 to aRList.Count-1 do
        begin

        aRes := aRList.Items[i] as TResolution ;

        memo1.Lines.Add('ppx ' + FloatToStr(aRes.ppLx));

        end;
end;

procedure TForm1.ShowObjectListButton2Click(Sender: TObject);
var    i     : Integer;
       aRes  :  TResolution;
begin

     aRes := TResolution.Create;

     for i:= 0 to aRList.Count-1 do
        begin

        aRes := aRList.Items[i] as TResolution ;


        memo1.Lines.Add('ppx ' + FloatToStr(aRes.ppLx));

        end;

     aRes.Free;
end;

{ TSimpleListClass }

constructor TSimpleListClass.create;
begin
    aRList  := TResolutionList.Create;

    inherited;
end;

destructor TSimpleListClass.free;
begin

   aRList.Free

end;

procedure TForm1.versionCreateLISTButtonClick(Sender: TObject);
begin
    aSimpleListClass  :=TSimpleListClass.create;
end;

procedure TForm1.AddelementClick(Sender: TObject);
var   aRes  :  TResolution;
begin
     aRes  :=TResolution.Create;

     aRes.ppLx := Random(100);
     aRes.Name := 'any person name' + IntToStr(Random(100));

     aSimpleListClass.aRList.Add(aRes);

     memo1.Lines.Add(IntToStr(aSimpleListClass.aRList.Count));

end;

procedure TForm1.Button2Click(Sender: TObject);
var    i     : Integer;
       aRes  :  TResolution;
begin




     for i:= 0 to  aSimpleListClass.aRList.Count-1 do
        begin

        aRes :=  aSimpleListClass.aRList.Items[i] as TResolution ;

        memo1.Lines.Add('ppx ' + FloatToStr(aRes.ppLx));

        memo1.Lines.Add('text ' + aRes.NAme);

        end;

end;

procedure TForm1.FreeButton2Click(Sender: TObject);
begin
    aSimpleListClass.free;
end;

end.

Open in new window

ee-error.jpg
Comment
Watch Question
Freelance Project Manager
CERTIFIED EXPERT
Top Expert 2010
Commented:
This problem has been solved!
Unlock 1 Answer and 7 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE