Board index » delphi » Access Violation with TfileStream I hate access violations

Access Violation with TfileStream I hate access violations

unit uMainForm;

interface

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

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    ListBox2: TListBox;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    OpenDialog1: TOpenDialog;
    SaveDialog1: TSaveDialog;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);

Var
  MyBuffer : Array of byte;
  Stream1, Stream2 : TFileStream;
begin
      If OpenDialog1.Execute then
        begin
           Stream1.Create(OpenDialog1.FileName,fmOpenRead);
  {         Stream1.Read(MyBuffer,1);}
    {      Listbox1.Items.Add(InttoStr(Stream1.Read(MyBuffer,1));
           Listbox1.Items.Add( IntToStr(MyFileArray[1]));
           Listbox1.Items.Add( IntToStr(MyFileArray[2]));
           Listbox1.Items.Add( IntToStr(MyFileArray[3]));
    }
       {    For I := 0 to Midifile.Size do
             begin
               File
             end;}

        end;
end;

end.

This code is generating an access violation.  What the heck did I do?  What do
I need to de-reference or create an instance of or what?  Wish Delphi 4's error
message gave me some clue.  Thanks for the help.

 

Re:Access Violation with TfileStream I hate access violations


Quote
Shakepeare wrote:
> [...]
>   Stream1, Stream2 : TFileStream;
> begin
>       If OpenDialog1.Execute then
>         begin
>            Stream1.Create(OpenDialog1.FileName,fmOpenRead);
> [...]

> This code is generating an access violation.  What the heck did I do?  What do
> I need to de-reference or create an instance of or what?  Wish Delphi 4's error
> message gave me some clue.  Thanks for the help.

    Coming from someone who just said "I might also recommend The Delphi
Developer's Handbook" in another thread this seems like it must be some
sort of trollery. I hate that.

    On the off chance that you've never created any Delphi objects before:
Look at _any_ example _anywhere_:

    Stream1:= TFileStream.Create(OpenDialog1.FileName,fmOpenRead);

instead of Stream1.Create(OpenDialog1.FileName,fmOpenRead).

Re:Access Violation with TfileStream I hate access violations


On 17 Aug 1999 18:46:33 GMT, shakepe...@aol.com (Shakepeare) wrote:

Quote
>Var
>  MyBuffer : Array of byte;
>  Stream1, Stream2 : TFileStream;
>begin
>      If OpenDialog1.Execute then
>        begin
>           Stream1.Create(OpenDialog1.FileName,fmOpenRead);

Go to Project | Options.
Select the Compiler page.
Check the "Show hints" and "Show warnings" boxes.
Rebuild the application.
--
Ray Lischner (http://www.tempest-sw.com/)
author of Delphi in a Nutshell

Re:Access Violation with TfileStream I hate access violations


Im Artikel <19990817144633.08445.00000...@ng-cn1.aol.com>, shakepe...@aol.com
(Shakepeare) schreibt:

Quote
>         Stream1.Create(OpenDialog1.FileName,fmOpenRead);

I had the same idea in my first steps in Delphi, but this isn't the right way
:-(

Delphi doesn't use the type of the variable to call the appropriate
constructor, instead you must use <class>.Create(...), as outlined in other
answers to your question.

DoDi

Other Threads