locked
Arrays RRS feed

  • Question

  • I am a little confused on how to declare a variable to be used in a program.

    The program I am writing, has a list box with 6 Players Names in it. user selects the player, hits the view button, it populates another form using parrallel arrays.

    It works fine, but I am having a Warning about the variable ("strPlayerName" is used before it has been assigned a value.)

    How can I fix this issue?

            Dim strTeamName() As String = {"New England Patriots", "Denver Broncos", "Green Bay Packers", "New Orleans Saints",
                               "New York giants", "Dallas Cowboys"} 'Arrays with Team Names
            Dim strPosition() As String = {"Quarterback", "Wide Reciever", "Quarterback", "Tailback",
                                           "Quarterback", "Tight End"} 'Arrays with Player Positions
            Dim intJerseyNumber() As Integer = {12, 83, 12, 23, 10, 82} 'Arrays with Players Jersey Number
            Dim strPlayerName As String
            Dim Player As New frmPlayerInfo
    
            Try
                Select Case lstPlayers.SelectedIndex
                    Case 0
                        strPlayerName = "Tom Brady"
                    Case 1
                        strPlayerName = "Wes Welker"
                    Case 2
                        strPlayerName = "Aaron Rodgers"
                    Case 3
                        strPlayerName = "Pierre Thomas"
                    Case 4
                        strPlayerName = "Eli Manning"
                    Case 5
                        strPlayerName = "Jason Witten"
                End Select
    
                If lstPlayers.SelectedIndex > -1 And
                     lstPlayers.SelectedIndex < strTeamName.Length Then 'test to make sure that the user selects a player
    
                    'displays in the new form the player info into labels
                    Player.lblName.Text = strPlayerName
                    Player.lblPosition.Text = strPosition(lstPlayers.SelectedIndex)
                    Player.lblTeam.Text = strTeamName(lstPlayers.SelectedIndex)
                    Player.lblJerseyNumber.Text = intJerseyNumber(lstPlayers.SelectedIndex).ToString()
    
                Else
                    MessageBox.Show("You must select a player.") 'displays to use if they don't select player
                End If
                Player.ShowDialog() 'opens new form to display player info
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub

    Wednesday, April 3, 2013 9:06 PM

Answers

  • Hi 

    Try 

        Dim strPlayerName As String = Nothing


    Regards Les, Livingston, Scotland

    • Marked as answer by Rosco2382 Thursday, April 4, 2013 12:57 PM
    Wednesday, April 3, 2013 9:11 PM

All replies

  • Hi 

    Try 

        Dim strPlayerName As String = Nothing


    Regards Les, Livingston, Scotland

    • Marked as answer by Rosco2382 Thursday, April 4, 2013 12:57 PM
    Wednesday, April 3, 2013 9:11 PM
  • Hi 

    Try 

        Dim strPlayerName As String = Nothing


    Regards Les, Livingston, Scotland


    Is this considered acceptable? I'm doing self teaching with iTunesU and this book. I don't wanna get into to many bad habits.
    Wednesday, April 3, 2013 9:19 PM
  • The warning is incorrect.  You can ignore it, or you can specifically initialise the variable in order to get rid of the warning. But whether or not you initialise the variable will have no effect at run time.

    The warning occurs because the logic which analyses code to determine whether or not a variable has been initialised before it is used does not consider the complete logic flow of the code.  In your case that logic flow is a combination of the Try/Catch and Select/Case.

    Note that your Try/Catch serves no purpose as you are already trapping the only error that can occur - SelectedIndex = -1.

     

    Wednesday, April 3, 2013 9:53 PM
  • Hi

    You took the words right out of my mouth Acamar :)


    Regards Les, Livingston, Scotland

    Wednesday, April 3, 2013 10:22 PM
  • hi Rosco

    not related to your specific question, which leshay and Acamar have already answered.

    consider creating a custom class for your players with a Property for the name, team, jersey number, position, etc. I just see lots of problems and headaches for you down the road. Trying to keep all those array indexes synced will be a nightmare. What if Pierre Thomas retires, so you remove New Orleans Saints from the Team array and Tailback from the Position array, but forget to remove or remove the third instead of fourth item from the JerseyNumber array? Now all the rest of the players after that will have the wrong jersey.

    And this may work for 6 players, but I'm sure you will want to expand that. How are you going to save the info between app runs? You aren't really going to have a 100 element array for each property, and a Select Case with 100 Cases? You don't have to go the database route - you can simply save it in a CSV file, an XML file, something simple like that. That would easily allow you to use your app to add, remove or edit players.

    Just some thoughts.


    “This forum post is my own opinion and does not necessarily reflect the opinion or view of Microsoft, its employees, or other MVPs.”

    • Proposed as answer by Frank L. Smith Thursday, April 4, 2013 3:00 AM
    Thursday, April 4, 2013 12:15 AM
  • iwavila,

    thanks for your input. I am merely working through iTunesU class and a "Starting out with Visual Basic 2010" By Tony Gaddis adn Kip Irvine. I merely made the program to get an idea how arrays work. I know that what I did doesn't make sense in a grand scheme of things, but my intentions was for a few small parrallel arrays in conjunction with a list box.

    If you anyone knows of a better book, to help me I would greatly appreciate it.

    Thursday, April 4, 2013 1:20 PM