Page 1 of 1
Use of unassigned vars
Posted: Thu Dec 14, 2017 3:35 pm
by FFF
Folks,
Code: Select all
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
Use of unassigned vars
Posted: Thu Dec 14, 2017 3:42 pm
by wriedmann
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
Use of unassigned vars
Posted: Thu Dec 14, 2017 4:11 pm
by FFF
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
Use of unassigned vars
Posted: Thu Dec 14, 2017 6:10 pm
by wriedmann
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
Use of unassigned vars
Posted: Thu Dec 14, 2017 8:28 pm
by FFF
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
Use of unassigned vars
Posted: Fri Dec 15, 2017 4:02 am
by wriedmann
Hi Karl,
personally I use statements like
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
Use of unassigned vars
Posted: Fri Dec 15, 2017 6:52 am
by Frank Maraite
Hi Karl,
in your first line after try you have
Code: Select all
Response := (HttpWebResponse)Request:GetResponse()
This may fail to another then a System.Net.WebException. This (other) exception is not catched. So the second line
Code: Select all
SR := StreamReader{Response:GetResponseStream()}
is not executed and not in any catch block.
Solution:
Code: Select all
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