fbpx

Use of unassigned vars

More
1 year 1 month ago #1 by Karl Faller
Use of unassigned vars was created by Karl Faller
Folks,
TRY
Response := (HttpWebResponse)Request:GetResponse()
 SR := StreamReader{Response:GetResponseStream()}
	ResponseData := SR:ReadToEnd()
CATCH Wex AS System.Net.WebException
 SR := StreamReader{Wex.Response:GetResponseStream()}
	ResponseData := SR:ReadToEnd()
THROW Exception{ResponseData}
FINALLY
 SR:Close() // line 61
END TRY

gives me a
warning XS0165: Use of unassigned local variable 'SR' 61,3 EasyHttp.prg
Why?


Karl

Please Log in or Create an account to join the conversation.

More
1 year 1 month ago - 1 year 1 month ago #2 by Wolfgang Riedmann
Replied by Wolfgang Riedmann on topic Use of unassigned vars
Hi Karl,

you are in a Try/Catch block, and if your code fails with an exception other than the one you are catching, before SR is initialized, you call the :Close() method on a not initialized object.
This sort of compiler warning helps me a lot!

Wolfgang
Last edit: 1 year 1 month ago by Wolfgang Riedmann.

Please Log in or Create an account to join the conversation.

More
1 year 1 month ago - 1 year 1 month ago #3 by Karl Faller
Replied by Karl Faller on topic Use of unassigned vars
Wolfgang,
yep, i hear you.
But what to do? As far as i saw, i have to provide the input stream at instantiation of the object, so when moving instantiation out of the block i'm unsafe, in case the stream call fails. OTOH, reminds me why i don't like the concept of writing constructors which do more than producing an object..
Somewhat contradictory to the "assign vals to property in the constructor call in the other thread.
BTW, i tried the new conditional syntax oSR?:Close() to no avail, not sure what to make of that..

Finally, in my defense: this is not my code, but the transposition of VB source ;)

Thx
Karl
Last edit: 1 year 1 month ago by Karl Faller. Reason: typo

Please Log in or Create an account to join the conversation.

More
1 year 1 month ago #4 by Wolfgang Riedmann
Replied by Wolfgang Riedmann on topic Use of unassigned vars
Hi Karl,

I would set the SR to null before the Try statement, so the error goes away. And in the Finally block I would add a "if SR != null" statement.

It seems X# makes better warnings that VB.NET - IMHO the warning is 100% correct.

Wolfgang

Please Log in or Create an account to join the conversation.

More
1 year 1 month ago #5 by Karl Faller
Replied by Karl Faller on topic Use of unassigned vars
Hi Wolfgang,
not sure what use a := Null has? If both instantiation paths in try and catch fail, the final :close would crash, nevertheless.
I think the ?: syntax prevents a crash...

But i wonder: i get no warning in the ResponseData := SR:ReadToEnd() lines, probably because the instantiation is "inside" the same block. What means, the real warning occurs because the compiler can't feather that SR exists as either the try or the catch path WILL be executed.

In a certain way the inverse situation of "unreachable code" ;)

Not sure if this qualifies as a bug or not...
Anyway, interesting discussion,
thx for you time!
Karl

Please Log in or Create an account to join the conversation.

More
1 year 1 month ago #6 by Wolfgang Riedmann
Replied by Wolfgang Riedmann on topic Use of unassigned vars
Hi Karl,

personally I use statements like
SR := null
often also in my VO applications to keep the compiler calm, and to say explicetely that I know that this variable can be null.

Wolfgang

Please Log in or Create an account to join the conversation.

More
1 year 1 month ago #7 by Frank Maraite
Replied by Frank Maraite on topic Use of unassigned vars
Hi Karl,
in your first line after try you have
Response := (HttpWebResponse)Request:GetResponse()

This may fail to another then a System.Net.WebException. This (other) exception is not catched. So the second line
SR := StreamReader{Response:GetResponseStream()}

is not executed and not in any catch block.

Solution:
LOCAL SR := NULL AS StreamReader
..
FINALLY
IF SR != NULL
   SR:Close()
ENDIF

I experience that the compiler is always right with that warning. It reminds you that SR may be uninitialized without intend and you have to check out this case.

Frank

Please Log in or Create an account to join the conversation.