인터넷에서 다음과 같이 일부 코드가 발견되었습니다 (약간 수정 됨).시도에서 값이 할당되지 않은 변수에 대한 경고를 방지합니다.
단순히 웹 페이지의 콘텐츠를 요청합니다. 나는 단순히 Finally
잊고 try 블록에 코드를 추가 할 수 있습니다 알고
Warning 1 Variable 'srRead' is used before it has been assigned a value. A null reference exception could result at runtime.
Warning 2 Variable 'Str' is used before it has been assigned a value. A null reference exception could result at runtime.
:
Private Sub readWebpage(ByVal url As String)
Dim Str As System.IO.Stream
Dim srRead As System.IO.StreamReader
Try
' make a Web request
Dim req As System.Net.WebRequest = System.Net.WebRequest.Create(url)
Dim resp As System.Net.WebResponse = req.GetResponse
Str = resp.GetResponseStream
srRead = New System.IO.StreamReader(Str)
' read all the text
textContent.text = srRead.ReadToEnd
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Unable to download content from: " & url)
Finally
srRead.Close()
Str.Close()
End Try
End Sub
그러나 나는이 경고를 얻을.
다른 방법을 사용하여 경고 메시지를 표시 할 수 있습니까?
미리 감사드립니다. :)
'Using'을 사용할 때 연결을 닫을 필요가 없습니까? – PeeHaa
아니요 using 문을 사용하면 도움이됩니다. (닫기는 Dispose와 동일합니다.) –
좋습니다. 나는 이것이 최선의 접근이라고 생각한다. 모든 정보 주셔서 감사합니다! – PeeHaa