The return value of the function may be undefined

Ok, I have the following function:

function TfPackagedItemEdit.GetRTFDescription: TStringList; begin Result.Text := richDescription.Lines.Text; //stringlist end; 

The compiler generates the following warning about this line:

[DCC Warning] W1035 The return value of the GetRTFDescription function can be undefined

Any ideas on how I can resolve this warning? (except how to disable it in the project settings)

I tried:

 function TfPackagedItemEdit.GetRTFDescription: TStringList; begin Result.Text := ''; Result.Text := richDescription.Lines.Text; end; 

But that doesn't work either.

+4
source share
3 answers

By default, the Result variable Result not initialized. It does not automatically refer to some instance of TStringList generated by the compiler. You need to set the value to Result . This means that there is such a line in your code:

 Result := ...; 

An expression of type Result.X reads the value of Result to get a reference to its member X , so you need to give the value of Result already. Larry's answer shows how to do this. It generates a new instance of TStringList , so the caller of this function needs to call Free on this object sometime.

But in the comment, you mention that you use this function as a means of accessing properties. It is inconvenient for callers to have free objects every time they read the property, so your whole plan may not be appropriate. Since it looks like you are trying to expose the description text, you might think about this:

 function TfPackagedItemEdit.GetRTFDescription: TStrings; begin Result := richDescription.Lines; end; 

First of all, note that I changed the return type to TStrings , which is essentially an abstract base class of all types of string lists in VCL. TStringList is one descendant, but TRichEdit.Lines does not use TStringList . Instead, it uses a specialized descendant of TStrings , which knows how to interact with the basic editing control.

Then note that I did not create any new objects. Instead, I returned the link directly to the Lines control. Users of your RTFDescription property no longer need to worry about freeing the object they receive.

+21
source

The compiler is correct. The result is not initialized by default. Try

 function TfPackagedItemEdit.GetRTFDescription: TStringList; begin Result = TStringList.Create(); Result.Text := richDescription.Lines.Text; end; 

Update: After reviewing the comments, I believe the original poster really wants something like this.

 function TfPackagedItemEdit.GetRTFDescription: String; begin Result := richDescription.Lines.Text; end; 
+14
source

I agree that returning a simple String is likely to be a better solution, or at least convenient to use.

Alternatively, you can return a link to TStringList , which is the fastest solution if you have a lot of text.

 function TfPackagedItemEdit.GetRTFDescription: TStringList; begin Result := richDescription.Lines; end; 
+2
source

Source: https://habr.com/ru/post/1286031/


All Articles