Accessing Class once intialized from another form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rb0135
    New Member
    • Jan 2010
    • 20

    Accessing Class once intialized from another form

    I have one form that initializes a class, called Players.

    But, I need to access these initialized values in another form.

    Obiously, if I do Players player = new Players(); it creates a new instance of the Players class, but has all variables set to defaults, not the previous values when the first form was run.

    I have tried just Players player; the compile compiles OK, but then I get an NULL REFERENCE error when trying to access the class..

    Can I access this initialized class information from another form? I have read a bit, but I think I am confusing myself.
  • markmcgookin
    Recognized Expert Contributor
    • Dec 2006
    • 648

    #2
    Hey Rob,

    No worries, glad to hear everything is going well.

    When you have created your Players class on Form1, it's just like creating any class, whether it be a string, an int, or anything else, it now "belongs" to Form1, so if you create Form2, it doesn't know anything about your players class.

    There are a few ways of solving this, here would be my two options:

    1) Create a static class that contains your Players class variable.. this way this class can be accessed from anywhere in the application and updated/read from whenever you want... we can chat about this a bit more if you decide to go down that route

    2) This is the simpler option, however it has it's limits. Which is to pass your existing players class into your new form. This really only suits if you want to read from this class, as if you want to change/update it's values and then use those again in other places, it can be a bit trickey.

    to do this, go to the constructor of your "Form2", add a players variable for this form and allow it to accept a Players class, i.e.

    Code:
    public partial class frmForm2 : Form
        {
            Players pMyPlayersClass; //This is a players class belonging to THIS form
    
            public frmForm2(Players myPassedPlayersClass)       
            {
                  //Here we are accepting the class from Form1
                  //and filling this form's players class with the info
                  //so it can be accessed once this constructor is finished
    
                  pMyPlayersClass = myPassedPlayersClass;
            }
    So now your second form has a copy of the first players class, all it's properties and methods

    You simply pass the value into it as follows:

    Code:
    //Somewhere on Form1
    frmForm2 myForm2 = new frmForm2(myOriginalPlayersClass);
    myForm2.Show();
    Hope that helps,

    Just remember that this is a COPY of the original Players, and not the original, so if you change something in the Form2 one (like delete a player) then close that form, it will not be deleted in the Players on Form1. If you want to reflect changes like that, we need to use Option 1.

    Let me know if you want to try option 1 and need help with it.

    Mark

    Comment

    • rb0135
      New Member
      • Jan 2010
      • 20

      #3
      Thanks Mark

      The good thing in my case, is that the first form setups the Players info. The form I need access the info on, only needs to READ it (and there are no other forms that need this info) but the first form DOESNT call the second form where I need the data... it is the form after which is called by the second form. This may sound strange, but the steps are Select Player and that form shows the players stats/info, then select Course (another form showing course info) then select the Hole you are playing (another form showing hazards, hole info, calculates distances, calculates club slection which is why I need the players info as this contains the clubs for the player, etc).

      I do prefer your first option though (because that is what I would do in VB.net).

      I tried doing the STATIC class but got a compiler error "access modifiers are not allowed on static constructors".

      I suppose you need some code to show how I have constructed the Players class, but it has a lot in it.

      I'll have another go at your option 1 suggestion.

      Thanks,
      Rob

      Comment

      • markmcgookin
        Recognized Expert Contributor
        • Dec 2006
        • 648

        #4
        Hi Rob,

        Well you could pass the Players variable multiple times with no issue, but yeah sorting out the static class would be less hassle. Just give the static class a private variable of Players and give it some get{} set{} methods and it should be ok

        Mark

        Comment

        • rb0135
          New Member
          • Jan 2010
          • 20

          #5
          Thanks for the tip.

          I think I did something similar with the CLUBS class within the Players class (but not static).

          Will give it a go soon. Probably post back tomorrow.

          Thanks,
          Rob

          Comment

          • rb0135
            New Member
            • Jan 2010
            • 20

            #6
            Actually, I quickly tried it, but failed.

            The code
            Code:
            namespace GolfCaddy
            {
                public static class MPlayers
                {
                    private Players[] player;
            
                    public Players[] MyPlayer
                    {
                        get { return player; }
                        set { player = value; }
                    }
            
                }
            }
            I get a compiler error on the private Players[] player stating "cannot declare instance members in a static class"

            Realised I needed static after the private and public Players[]

            This compiled OK, but trying to use it, do i need to initiate the Players class
            Code:
            Players newplayer = new Players();
            then attach is to the Static Class such as

            Code:
            MyPlayer.player = newplayer;
            Sorry if this is obvious to other people.. I am learning and feel I have done pretty good so far.. Just cant get my head around it at the moment.. Well into the evening here.

            Thanks,
            Rob

            Comment

            • rb0135
              New Member
              • Jan 2010
              • 20

              #7
              I Got it... Amazing what a sleep does.

              In my code in the Static Class, for some reason I was thinking my Players was an array of players, when in fact, this was the single class after a player is selected elsewhere in the program.

              So, I removed the array references, and everything just clicked in.

              Code:
              namespace GolfCaddy 
              { 
                  public static class MPlayers 
                  { 
                      private Players player; 
                
                      public Players MyPlayer 
                      { 
                          get { return player; } 
                          set { player = value; } 
                      } 
                
                  } 
              }
              I also answered my own question. Yes, you need to initiate the class Players then assign it to the static class.

              Thanks,
              Rob

              Comment

              • markmcgookin
                Recognized Expert Contributor
                • Dec 2006
                • 648

                #8
                Great work Rob!

                Keep us posted with your progress

                Comment

                Working...