Hello community Wrox!
I ran into a problem running the below code. What happens is the strFavoriteColor variable has a green line under it and the tooltip states it is being used before being declared and could be declared null at runtime. To my understanding of that according to the book this variable will have an empty value when executed. The program runs fine except when you click a name it states the "UsersName favorite color is ". It does not display the name. What I did to attempt to resolve the issues was double check my spelling of the names because I thought the names were not the same in the case statement and was returning false for each case evaluation. Does anyone have any ideas of the steps I may have overlooked?
Code:
Public Class Form1
Private Sub lstData_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstData.SelectedIndexChanged
'Declare Variables
Dim strName As String
Dim strFavoriteColor As String
'Get the selected name
strName = lstData.Items(lstData.SelectedIndex).ToString
'User a select Case statement to get the favorite color of the selected name
Select Case Name
Case "Bryan"
strFavoriteColor = "Madras Yellow"
Case "Ashley"
strFavoriteColor = "Sea Blue"
Case "Jennifer"
strFavoriteColor = "Morning Mist"
Case "Eddie"
strFavoriteColor = "Passionate Purple"
Case "Katelyn"
strFavoriteColor = "Red"
End Select
'Display the favorite color of the selected name
MessageBox.Show(strName & "'s favorite color is " & strFavoriteColor, "Select Demo")
End Sub
End Class