fbpx
Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC: Use of unassigned vars

Use of unassigned vars 1 year 6 months ago #3403

  FFF's Avatar Topic Author FFF Offline Posts: 571
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.

Use of unassigned vars 1 year 6 months ago #3404

  wriedmann's Avatar wriedmann Away Posts: 1549
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
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

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

Last edit: by wriedmann.

Use of unassigned vars 1 year 6 months ago #3406

  FFF's Avatar Topic Author FFF Offline Posts: 571
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

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

Last edit: by FFF. Reason: typo

Use of unassigned vars 1 year 6 months ago #3407

  wriedmann's Avatar wriedmann Away Posts: 1549
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
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

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

Use of unassigned vars 1 year 6 months ago #3408

  FFF's Avatar Topic Author FFF Offline Posts: 571
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.

Use of unassigned vars 1 year 6 months ago #3409

  wriedmann's Avatar wriedmann Away Posts: 1549
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
Wolfgang Riedmann
Meran, South Tyrol, Italy
This email address is being protected from spambots. You need JavaScript enabled to view it.
www.riedmann.it - docs.xsharp.it

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

Use of unassigned vars 1 year 6 months ago #3412

  Frank Maraite's Avatar Frank Maraite Offline Posts: 172
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.

  • Page:
  • 1