Can't input to a global object array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Barda4
    New Member
    • Jan 2009
    • 5

    #1

    Can't input to a global object array

    Hi,

    So I'm making my final project for my CS class. I'm making a pokemon game.
    So right now I'm trying to input information(The pokemon the player has) into my array of objects(The list of pokemon the player has). I have the array in a global class so every form can access it. I have it declared as;

    Code:
    public class GlobalVariables
        {
                public static PlayerPokemon[] PlayerPokemonList = new PlayerPokemon[5];
        }
    but when I try to input something to that array with;

    GlobalVariables .PlayerPokemonL ist[0].Name = "BULBASAUR" ;

    It says the object is null. I've tried to declare the array under the initilizer in the form but it doesn't work, I tried it like this;

    Code:
     public IntroForm()
            {
                InitializeComponent();
                GlobalVariables.PlayerPokemonList[] PlayerList = new GlobalVariables.PlayerPokemonList[];
            }
    I have also tried to put it under Public Partial Class, but it doesnt work

    Any help would be greatly appreciated
    Last edited by tlhintoq; Jan 10 '10, 12:30 AM. Reason: [cODE] ...Your code goes between code tags [/CODE]
  • alexis4
    New Member
    • Dec 2009
    • 113

    #2
    try this:
    Code:
    public static PlayerPokemon[] PlayerPokemonList = 
       new PlayerPokemon[5];
    
    for (byte i = 0; i < 5; i++)
      PlayerPokemonList[i] = new PlayerPokemon();

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      Bytes has a policy regarding assisting students with their homework.

      The short version is that the volunteers here can't help you with schoolwork.
      A) We don't know what material you have and have not learned in class.
      B) We don't know the guidelines you must follow.
      C) In the long run giving you the answers actually short changes your education.

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          Please forgive me but...
          So I'm making my final project for my CS class.
          So you've gone through the entire class and they never taught you that you have initialize the objects before you can use them? Ask for your money back and put it toward an assortment of self-teaching books from the book store. The "Sams" line is quite good. "Sam's Teach yourself C# in 21 days" for example.

          Please understand that I'm not insulting you - I am expressing my deep distain for whatever school stole your money and gave you little for it.

          Comment

          • Barda4
            New Member
            • Jan 2009
            • 5

            #6
            My class is only a grade 12 class, so I don't pay for it. And it's not that I don't know how to initilze objects, it's just I'm trying to initilize a global object array, but whatever I've tried, doesnt work.

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              it's just I'm trying to initilize a global object array
              As Alexis4 pointed out, you have to initialize each object in the array.
              Making the array allocates a variable and memory space, but it doesn't cause each object *in* the array to be created.
              So you make the array, then make a new object for element of the array.

              Comment

              Working...