Cannot Start Process Error -- VB.Net

Can somebody help resolve the error that is caused by the code shown below? I get a "cannot start process because a file name has not been provided" error.

The following warnings were received: "Warning 1 Variable 'test' is used before it has been assigned a value. A null reference exception could result at runtime."
"Warning 2 Variable 'fileName' is used before it has been assigned a value. A null reference exception could result at runtime."

The screenshots show the errors and warnings.
    Private Sub OpenFile(ByVal AFileName As String)
        'EE Version 1 without error code
        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
        'ee
        If RadioButton1.Checked = True Then
            Dim fileName As String
            Dim test As String

            Select Case test
                Case 1
                    fileName = "C:\test_3\johnson.pdf"
                Case 2
                    fileName = "C:\test_3\Ross.pdf"
            End Select

            OpenFile(fileName)
        End If
    End Sub

Open in new window

ErrorWarnings
Mark01Asked:
Who is Participating?
 
AndyAinscowFreelance programmer / ConsultantCommented:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'ee
        If RadioButton1.Checked = True Then
            Dim fileName As String
           Dim test As Integer
test = 1
           Select Case test
                Case 1
                    fileName = "C:\test_3\johnson.pdf"
                Case 2
                    fileName = "C:\test_3\Ross.pdf"
            End Select
'  MessageBox.Show("File name is: " + fileName)
            OpenFile(fileName)
        End If
    End Sub
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
I bet you do not a file name in the message box.  If you do not then the variable 'test' contains some other value than either 1 or 2

 
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'ee
        If RadioButton1.Checked = True Then
            Dim fileName As String
            Dim test As String

            Select Case test
                Case 1
                    fileName = "C:\test_3\johnson.pdf"
                Case 2
                    fileName = "C:\test_3\Ross.pdf"
            End Select
MessageBox.Show("File name is: " + fileName)
            OpenFile(fileName)
        End If
    End Sub
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
The solution is simple - assign the value you want to the variable test
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
Mark01Author Commented:
Yes, the message box does not contain a file name. Can you provide some sample code to assign the value you want to the variable test? I am a beginner and will use this app to learn.
0
 
Mark01Author Commented:
The error is gone but the only the johnson.pdf will open when RadioButton 1 is clicked. RadioButton 2 does not work. Doesn't the sub OpenFile need to be called in order for both radio buttons to function?

Full code:
 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

        If RadioButton1.Checked = True Then
            Dim fileName As String
            ' Dim test As String
            Dim test As Integer
            test = 1
            Select Case test
                Case 1
                    fileName = "C:\test_3\johnson.pdf"
                Case 2
                    fileName = "C:\test_3\Ross.pdf"
            End Select
            MessageBox.Show("File name is: " + fileName)
            OpenFile(fileName)
        End If
    End Sub

Open in new window

The form
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
Experts exchange really likes a question - answer set.  Your original question was answered.

Concerning your follow up with radio buttons you probably want to replace
            test = 1
with something like
            test = GroupBox1.Value

If it doesn't work then please close this question and open a new question about linking radio buttons to variables.  (I'm not going to get back until tomorrow at the earliest now).
0
 
Mark01Author Commented:
Thank you, Andy.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.