Results 1 to 2 of 2

Thread: Creating a Global OBject Variable

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    36

    Creating a Global OBject Variable

    I have created a custom class named Player.

    In my Form Load() event I create an instance of the class Player. When I try to access that Object in a different control on the same form it says the Object is not defined. Is there some way to allow other controls in the form to access that Object once it has been created in the Load() method of the form?

    Basically what I am trying to do is when a button is clicked create an instance of my Player object that will then be accessible to the rest of my Program and not just inside the button control. Side Note: When the program is running there will be multiple intances of the Player object.

    What would be the best way to do this?

    One thing I tried to no avial was to create a module named CreatePlayer in which there is a function called fCreatePlayer which creates the object. In the button Press event I call the fCreatePlayer() function which creates the Player.Unfortunatly the new object still cannot be accessed in other controls on the form.

    My New Module

    Code:
    Module CreatePlayer 
        Public Function fCreatePlayer(ByVal pForm As Form1) 
            'Create a Player Object 
            Dim Player1 As Player 
            Player1 = New Player(pForm) 
            Player1.Name = "Patrick" 
            Player1.Seat = 1 
        End Function 
    End Module
    The Call to create the Object

    Code:
        Private Sub BtnQuickSet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnQuickSet.Click 
                    fCreatePlayer(Me) 
        End Sub
    The Code where I attempt to access the Object

    Code:
    Private Sub BtnCard1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCard1.Click, BtnCard2.Click 
    
    MessageBox.Show(Player1.Name) 
          
    End Sub
    You guys have all been so helpful I sincerely do appreciate the help,
    -Patrick

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    How about Public instead of Dim?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width