警告:在分配之前使用变量“文件” [英] Warning: Variable 'File' Is Used Before It Has Been Assigned
问题描述
我收到了警告。我不知道为什么会得到警告消息,因为变量'文件'在被赋值之前就被使用。在运行时可能会产生空引用异常。
Private Sub HL_Click(发送者为对象,e为EventArgs)处理HL.Click
如果vld(TxtProcode)= False那么
退出Sub
结束如果
Dim文件As System.IO.StreamWriter
Dim savefile As New FolderBrowserDialog
Dim fileDateTime如String = DateTime.Now.ToString(yyyyMMdd)& DateTime.Now.ToString(HHmmss)& .HL7
Dim ts As String = DateTime.Now.ToString(yyyyMMdd)& DateTime.Now.ToString(HHmmss)
'file = My.Computer.FileSystem.OpenTextFileWriter(C:\ psave \New folder \ & fileDateTime,True)
如果RadioBtndefault.Checked那么
file = My.Computer.FileSystem.OpenTextFileWriter(C:\ pdata \New folder \ & fileDateTime,True)
结束如果
TxtDob.Format = DateTimePickerFormat.Custom
TxtDob.CustomFormat =yyyyMMdd
TxtExamtime.Format = DateTimePickerFormat.Custom
TxtExamtime.CustomFormat =hhMMss
TxtExamdate.Format = DateTimePickerFormat.Custom
TxtExamdate.CustomFormat =yyyyMMdd
'我在下面的行收到警告
file.WriteLine( MSH | ^〜\& |||||& TxtExamdate.Text&& TxtExamtime.Text&|| ORM ^ O01 || P | 2.3.1)
file.Write行(PID |||& TxtId.Text& || &安培; TxtFamilyname.Text& ^& TxtGivenname.Text& || &安培; TxtDob.Text& || &安培; TxtGender.Text& ||| &安培; TxtStreet.Text& & TxtHouse.Text& ^^& TxtCity.Text& ^^& TxtPostcode.Text)
file.WriteLine(PV1 || O ||||||||||||||||||& TxtId.Text&|||| ||||||||||||||||||||||& ts)
file.WriteLine(ORC | NW |& ts& ||||| ^^^ S ||& TxtExamdate.Text)
file.WriteLine(OBR ||& ts&^& ts& ||& TxtProcode.Text&|||& TxtExamdate.Text&& TxtExamtime.Text&|& TxtExamdate.Text&& TxtExamtime.Text)
file.WriteLine()
file.Close()
MessageBox.Show(HL7消息已保存到& fileDateTime,HL7)
TxtDob.Format = DateTimePickerFormat.Short
TxtExamtime.CustomFormat =hh:MM tt
TxtExamdate.CustomFormat =dd-MMM-yy
结束子
结束类
警告的原因是你只在a中为'file'赋值如果分支...如果执行将跳过该怎么办?如果?
'文件'将为空(未分配)...
你必须提供一个未选中复选框的情况下的解决方案...
1.将所有文件处理放在if
中2.在else分支上退出方法而不使用'file
Am getting a warning. I dont know why am getting tha Warning message as " Variable 'file' is used before it has been assigned a value. A null reference exception could result at runtime."
Private Sub HL_Click(sender As Object, e As EventArgs) Handles HL.Click
If vld(TxtProcode) = False Then
Exit Sub
End If
Dim file As System.IO.StreamWriter
Dim savefile As New FolderBrowserDialog
Dim fileDateTime As String = DateTime.Now.ToString("yyyyMMdd") & DateTime.Now.ToString("HHmmss") & ".HL7"
Dim ts As String = DateTime.Now.ToString("yyyyMMdd") & DateTime.Now.ToString("HHmmss")
'file = My.Computer.FileSystem.OpenTextFileWriter("C:\psave\New folder\" & fileDateTime, True)
If RadioBtndefault.Checked Then
file = My.Computer.FileSystem.OpenTextFileWriter("C:\pdata\New folder\" & fileDateTime, True)
End If
TxtDob.Format = DateTimePickerFormat.Custom
TxtDob.CustomFormat = "yyyyMMdd"
TxtExamtime.Format = DateTimePickerFormat.Custom
TxtExamtime.CustomFormat = "hhMMss"
TxtExamdate.Format = DateTimePickerFormat.Custom
TxtExamdate.CustomFormat = "yyyyMMdd"
'Am getting the warning at the below line
file.WriteLine("MSH|^~\&|||||" & TxtExamdate.Text & "" & TxtExamtime.Text & "||ORM^O01||P|2.3.1")
file.WriteLine("PID|||" & TxtId.Text & "||" & TxtFamilyname.Text & "^" & TxtGivenname.Text & "||" & TxtDob.Text & "||" & TxtGender.Text & "|||" & TxtStreet.Text & " " & TxtHouse.Text & "^^" & TxtCity.Text & "^^" & TxtPostcode.Text)
file.WriteLine("PV1||O|||||||||||||||||" & TxtId.Text & "|||||||||||||||||||||||||" & ts)
file.WriteLine("ORC|NW|" & ts & "|||||^^^S||" & TxtExamdate.Text)
file.WriteLine("OBR||" & ts & "^" & ts & "||" & TxtProcode.Text & "|||" & TxtExamdate.Text & "" & TxtExamtime.Text & "|" & TxtExamdate.Text & "" & TxtExamtime.Text)
file.WriteLine()
file.Close()
MessageBox.Show("HL7 Message Saved to " & fileDateTime, "HL7")
TxtDob.Format = DateTimePickerFormat.Short
TxtExamtime.CustomFormat = "hh:MM tt"
TxtExamdate.CustomFormat = "dd-MMM-yy"
End Sub
End Class
The reason for the warning is that you assign value to 'file' only in an if branch...What will happen if the execution will skip that if?
'file' will be null (unassigned)...
You have to provide a solution for the case when checkbox is not checked...
1. Put all the file handling inside the if
2. Exit method on the else branch without using 'file'
这篇关于警告:在分配之前使用变量“文件”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!