Populate Class/List and use globaly?

yogi_bear_79

Member
Joined
Sep 30, 2009
Messages
19
Programming Experience
Beginner
I am probably not even saying it correctly when I pose the question.

I have this simple class (starting out)

VB.NET:
Public Class Players

    Public Name As String

    Public Sub New(ByVal name As String)
        Me.Name = name
    End Sub
End Class

So far I can populate the List as so

VB.NET:
 Dim players As List(Of Players) = New List(Of Players)
        players.Add(New Players(txtNewPlayer.Text))

        Dim x As Integer
        Dim obj As Players

        For x = 0 To players.Count - 1
            obj = players.Item(x)
            frmPlayers.PlayerList.Items.Add(obj.Name)
        Next

I'd like to be able to use players globaly thru-out the project. So if I navigate away from the form that created it. I can access it again.

VB.NET:
Dim players As List(Of Players) = New List(Of Players)
 
If you want a global variable in a VB.NET app then that generally means using a Module. There are better options but this is the simplest so it's a good place to start. Add a new Module to your project and declare a Public variable in it. You can now access that variable throughout your project.
 
I've been doing that. I have a module that I list my public variables in. I tried adding the code below before I posted, but it wasn't accesable by the other classes (forms).

Public players As List(Of Players) = New List(Of Players)
 
I've been doing that. I have a module that I list my public variables in. I tried adding the code below before I posted, but it wasn't accesable by the other classes (forms).

Public players As List(Of Players) = New List(Of Players)
Yes it is. If you put that code in a module then that 'players' variable is accessible to every form. That means that every form must use that 'players' variable though. There's no use declaring that variable and then creating a different List in your main form because you're back where you started. The main form has to use the 'players' variable too, so it's using the same List object that all the other forms are.
 
This is where I am confused. I added:

VB.NET:
Public Class Players

    Public Name As String

    Public Sub New(ByVal name As String)
        Me.Name = name
    End Sub
End Class

Module Helper
       Public players As List(Of Players) = New List(Of Players)
End Module

then updated:

VB.NET:
Public Class frmAddNewPlayer

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        '...declare filestream w/write access
        Dim fs As New FileStream(FilePath, FileMode.Append, FileAccess.Write)
        '...create streamwriter filestream object fs as argument
        Dim s As New StreamWriter(fs)
        s.WriteLine(txtNewPlayer.Text)
        s.Close()

        players.Add(New Players(txtNewPlayer.Text))

        Dim x As Integer
        Dim obj As Players

        For x = 0 To players.Count - 1
            obj = players.Item(x)
            frmPlayers.PlayerList.Items.Add(obj.Name)
        Next
        Me.Close()
    End Sub

End Class

I get errors that 'add', 'count, and 'item' are not members of ProgramName.Players
 
VB code is case-insensitive, so when you write 'players' which one do you really mean, the class or the module field? You can qualify the statements, but it would be more appropriate to name a player class Player and not Players.
 
The problem is that you have named your class 'Players' and you've named your module variable 'players'. VB is case-insensitive so those two are the same. As such, if you refer to just 'Players' or 'players' in code you are referring to the class, not the variable. To indicate that you're referring to the variable rather than the class you need to qualify the name, i.e. use 'Helper.players'.

That said, it shouldn't actually come to that. When you name something you should always use a name that reflects the actual purpose of the thing your naming. Look at your Players class. What does it represent? Does it represent multiple players or does it represent a single player? It's the latter, isn't it? As such, it's name should be 'Player' (singular), not 'Players' (plural). If you change the class name then there will be no name clash and you won't have to qualify the variable name.
 
Back
Top