Mark01
asked on
VB.Net FileName is Used Before Warning
I am trying to work with two radio buttons in a simple VB.net app that allows a user to select and open a PDF using the Process object. One PDF will be assigned to each radio button. The code is shown below. I get the following warning: "Variable ‘fileName’ is used before it has been assigned a value. A null reference exception could result at runtime."
Please help resolve the error.
This is not homework for a class. It is self-study using books.
Code:
The warning occurs on the following line:
Please help resolve the error.
This is not homework for a class. It is self-study using books.
Code:
Public Class Form1
Private Sub OpenFile(ByVal AFileName As String)
Dim psi As New ProcessStartInfo()
psi.UseShellExecute = True
psi.FileName = AFileName
Process.Start(psi)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim fileName As String
If RadioButton1.Checked = True Then
fileName = "c:\test_3\johnson.pdf"
End If
If RadioButton2.Checked = True Then
fileName = "c:\test_3\ross.pdf"
End If
MessageBox.Show("File name is: " + fileName)
OpenFile(fileName)
End Sub
End Class
The warning occurs on the following line:
MessageBox.Show("File name is: " + fileName)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you Eric, myboss and Ryan.
ASKER
Code:
Open in new window
The error is caused by:Open in new window
@Ryan: Your code does not resolve the warning.@mybosssucks: Your code worked!