TStream warning in delphi

I have the following code snippet

 Procedure TFrm.Retrieve(mystring : string);
  var 
   bs : TStream;
   ...
  begin
    ...
    bs:=nil;
    //bs:= TStream.create; 
    try
     bs := CreateBlobStream(FieldByName('Picture'), bmRead);
    finally
     bs.Free;
    end;
  ... 
  end;   

I have a problem understanding the initialisation of bs variable.

If I dont initialize it , a but obvious warning i get.

 Variable 'bs' might not have been initialized.

Now if I do it as the commented part i.e.

 bs:= TStream.create;

I get the following warning.

Constructing instance of 'TStream' containing abstract method 'TStream.Read'
Constructing instance of 'TStream' containing abstract method 'TStream.Write'

and finally it works totally fine if I use

 bs:=nil;

Am I Doing it correct by assigning it to Nil?

Any views appreciated.

Answers


TStream is abstract so you shouldn't instantiate it (calling an abstract method causes a runtime error). Instead, you should instantiate a non-abstract descendant. When you're done you should Free the instance.

For example:

var
  Stream: TStream;
begin
  try
    Stream := CreateBlobStream(FieldByName('Picture'), bmRead);
    try
      // ...
    finally
      Stream.Free;
    end;
  except 
    // handle exceptions
  end;
end;

Need Your Help

android drawable recognise resources

android drawable

I drag and drop an image into my "drawable" folder,

Flex+MXML: Getting parent form from textfield “enter” event

flex forms mxml parent hierarchy

I'm not sure what the best practice is when attempting to "do something" with a form as a result of a user interaction - I'm certainly open to an alternative if it's the best practice.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.