Reputation: 881
I am having an issue where I am receiving a "Variable 'strMap' is used before it has been assigned a value. A null reference exception could result at runtime" error for the string strMap.
I understand that it is saying that I am using the string BEFORE I am applying a value, however, as you can see from the code I am applying a value in the last set of If statements (these were previously If/Else but I assumed this was what was messing with it.
As a result, It cannot find the file specified in the shell command because the strMap string is acting as empty.
Thanks in advance.
Damon
Private Sub btnCreateServerSimple_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateServerSimple.Click
'Working code for server creation using pre-existing batch files
'Declarations
Dim strGameType As String
Dim strMap As String
Dim strServer As String
'Dim strBatchFile As String
Dim strBatchLoc As String
'Checking for selected server and setting strServer string accordingly
If cmbServerSimple.Text = "Server 1" Then
strServer = "server1"
strBatchLoc = frmInstallUpdateSettings.txtCSGOServer1BatchFilesLocation.Text
Else : cmbServerSimple.Text = "Server 2"
strServer = "server2"
strBatchLoc = frmInstallUpdateSettings.txtCSGOServer2BatchFilesLocation.Text
End If
'Checking for gametype and setting strGameType string accordingly
If cmbGameTypeSimple.Text = "Classic Competative" Then
strGameType = "classic_competative"
Else : cmbGameTypeSimple.Text = "Classic Casual"
strGameType = "classic_casual"
End If
'Checking for selected map and setting strMap string accordingly
If cmbMapSimple.Text = "de_aztec" Then
strMap = "de_aztec"
End If
If cmbMapSimple.Text = "de_dust2_se" Then
strMap = "de_dust2_se"
End If
If cmbMapSimple.Text = "de_dust" Then
strMap = "de_dust"
End If
If cmbMapSimple.Text = "de_inferno" Then
strMap = "de_inferno"
End If
If cmbMapSimple.Text = "de_nuke_ve" Then
strMap = "de_nuke_ve"
End If
If cmbMapSimple.Text = "de_mirage" Then
strMap = "de_mirage"
End If
If cmbMapSimple.Text = "de_train" Then
strMap = "de_train"
End If
If cmbMapSimple.Text = "de_vertigo" Then
strMap = "de_vertigo"
End If
Shell("C:\Users\Damon\Desktop\batchtest\" & strServer & "_" & strGameType & "_" & strMap & ".bat", AppWinStyle.NormalFocus)
End Sub
Upvotes: 1
Views: 6000
Reputation: 941357
The warning is accurate, if none of the If() statements match then you'll end up with strMap being unassigned. Write better code, use If/ElseIf/Else. The Select statement should be your preference here:
Select Case cmbMapSimple.Text
Case "de_aztec" : strMap = "de_aztec"
Case "de_dust2_se" : strMap = "de_dust2_se"
Case "de_dust" : strMap = "de_dust"
'' etc...
Case Else : Throw New Exception("Invalid map selection")
End Select
Which also helps you arrive at the most likely correct code, since you already made sure that the ComboBox always has a valid selection by setting its DropDownStyle property to DropDownList:
If cmbMapSimple.SelectedIndex < 0 Then
Throw New Exception("Invalid map selection")
End If
strMap = cmbMapSimple.Text
Upvotes: 2
Reputation: 27322
It is possible that strMap is not set to anything. The best solution is to change all those If Else statement s to a Select Case statement then use Case Else to set strMap to "unknown"
Upvotes: 1
Reputation: 216293
If all of your test on the cmbMapSimple combo fails then your strMap is not initalized and the compiler correctly warns you.
You could avoid the problem assigning explicitly a value to your variable when you declare it
Dim strMap As String = String.Empty
Then, of course, what you decide to do if the variable is empty is up to your code.
Upvotes: 1
Reputation: 38875
It is talking about this:
Dim strMap As String
Change it to:
Dim strMap As String = ""
you are correct that it is set before used, but the warning is telling you that IF you tried to use it in the block before it is set, it could result in an exception. The other strings should be set as well.
Upvotes: 3