Coding4Fun
10 .NET Programming Projects for Wiimote, World of Warcraft, YouTube and More

Adding [,] to a class type.

Latest post Sat, Mar 28 2009 8:35 PM by Sidar. 12 replies.
  • Sat, Mar 28 2009 5:33 PM

    • Sidar
    • Top 10 Contributor
    • Joined on Sat, Mar 28 2009
    • Posts 7

    Adding [,] to a class type.

     Hi there,

    I'm new here, apparently...
    I'm currently working my way up in the AlienAttack game to make the grid for the enemies. Now i have been doing some programming before but in AS3. It doesn't look too different, so its great getting to know C# with such book.(even though i know its not meant to teach one the whole syntax, ins and outs).

    My problem now is...well not a problem but something i don't really get, is something with the line:

    private Enemy[,] enemies;

    Page 32 Chapter 1:Alien Attack.

    See the thing is, I understand " Enemy " is the class i made before i got to this point, however i don't understand how [,] fits in.
    Looking trough the code you pass some position...right? but even then i don't understand where [,] comes from? is it some sort of an array? list? Or did i just miss something?

    Thanks for your time,

    Sidar

    (ps: Im from the Netherlands, any grammar flaws should be ignored =D)

    Filed under:
  • Sat, Mar 28 2009 5:37 PM In reply to

    • Brian Peek
    • Top 10 Contributor
      Male
    • Joined on Wed, Jul 16 2008
    • Niskayuna, NY
    • Posts 53

    Re: Adding [,] to a class type.

    Thanks for picking up a copy of the book!

     

    To answer the question:

    private Enemy[,] enemies;

    This denotes that "enemies" is a 2 dimensional array whose size has not yet been determined, but will be determined by the constructor.  In the EnemyGroup constructor you'll see this:

    enemies = new Enemy[EnemyRows,EnemyCols];

    This line creates a new array with 2 dimensions, the first being of size EnemyRows (4) and the second being of size EnemyCols (6).

    That way we can easily refer to a specific enemy as enemies[x,y] where x/y are the row and column position.

    Does that help?

  • Sat, Mar 28 2009 5:48 PM In reply to

    • Sidar
    • Top 10 Contributor
    • Joined on Sat, Mar 28 2009
    • Posts 7

    Re: Adding [,] to a class type.

    Thanks for your reply,

    Actually now i'm more confused as i still don't understand where [,] comes from.
    Is it a C# standard?

    But isn't "Enemy"  a class on its own? How is it possible for an existing class to be turned into an 2D array?..

    Few lines further it says enemies[y,x] = enemy.
    I understand that y and x are int's comming from the forloop.
    But what is  "enemies[y,x] = enemy" doing? Does it place a new enemy sprite in that position(y,x)?

    edit: I guess the position enemies[y,x] referr to the new sprite?

    edit:edit: Shouldn't the Enemies[,] load the enemy sprite too since its basically the Enemy class?

    Thanks for your time.

     

  • Sat, Mar 28 2009 5:57 PM In reply to

    • Brian Peek
    • Top 10 Contributor
      Male
    • Joined on Wed, Jul 16 2008
    • Niskayuna, NY
    • Posts 53

    Re: Adding [,] to a class type.

    Yes, it's a C# syntax standard.  [] denotes an array.  [,] denotes a 2d array.  [,,] denotes a 3d array, and so forth.

    And you're absolutely correct.  That line is putting an enemy sprite at position y,x.

  • Sat, Mar 28 2009 6:00 PM In reply to

    • Sidar
    • Top 10 Contributor
    • Joined on Sat, Mar 28 2009
    • Posts 7

    Re: Adding [,] to a class type.

    Ok i get that part now.
    But then, shouldnt this grid load the enemy sprites as well? Since we use the class "Enemy" ?

    Aaand thanks again.

  • Sat, Mar 28 2009 6:02 PM In reply to

    • Brian Peek
    • Top 10 Contributor
      Male
    • Joined on Wed, Jul 16 2008
    • Niskayuna, NY
    • Posts 53

    Re: Adding [,] to a class type.

    It does.  In the constructor, inside the nested "for" loops you'll see this:

    Enemy enemy = new Enemy(contentManager);
    enemy.Position.X = x * enemy.Width + EnemySpacing.X;
    enemy.Position.Y = y * enemy.Height + EnemySpacing.Y;
    enemies[y,x] = enemy;

    That chunk is creating a new Enemy object, setting it's x/y position, and then assigning it to the enemies 2D array.

  • Sat, Mar 28 2009 6:08 PM In reply to

    • Sidar
    • Top 10 Contributor
    • Joined on Sat, Mar 28 2009
    • Posts 7

    Re: Adding [,] to a class type.

     Sorry i think i asked the question wrong.

    As we are using the class Enemy to make a new grid, shouldnt this grid on its own be loading his own batch of sprites since we are saying; enemies = new Enemy[ etc etc]; In the constructor of the Enemy class we load the sprites, am i correct?

    It creates a new Enemy object on that spot? OR is Enemy[,] entirely a different class compared to Enemy(contentManager)?

    (sorry if i'm asking to much in such short time >_<)

    thanks again!

    Sidar.

     

    Edit:
    Does this array limit the content to Enemy class types?

  • Sat, Mar 28 2009 7:08 PM In reply to

    • Brian Peek
    • Top 10 Contributor
      Male
    • Joined on Wed, Jul 16 2008
    • Niskayuna, NY
    • Posts 53

    Re: Adding [,] to a class type.

    Enemy is the class.  Enemy[,] is the same class, just an array of them.  When new Enemy(contentManager) is called, that's where the sprite is being loaded.  So the grid is in fact loading his own batch of sprites...

  • Sat, Mar 28 2009 7:33 PM In reply to

    • Sidar
    • Top 10 Contributor
    • Joined on Sat, Mar 28 2009
    • Posts 7

    Re: Adding [,] to a class type.

    Uhm i think you still misunderstand my question, LoL=P.( Could be my fault though =) )

    So let me get this straight:

    I understand it loads the Enemy objects into the grid in the forloop. But now looking at " Enemy[,] " it doesn't call the constructor of the class Enemy.
    Instead a new array is made where an object (an instance of the class Enemy) can be stored in.
    So It doesn't load anything that goes into the constructor of the class Enemy, it just creates an array that only allows objects of the class Enemy to be stored in? y and x are like an "ID" that reffer back to the created object instance.

    This should be it, right? Cause i can't think of something else.

    Thanks for your time again, I feel like being a bit of a pain.

    Sidar Talei.

     

  • Sat, Mar 28 2009 7:52 PM In reply to

    • Brian Peek
    • Top 10 Contributor
      Male
    • Joined on Wed, Jul 16 2008
    • Niskayuna, NY
    • Posts 53

    Re: Adding [,] to a class type.

    private Enemy[,] enemies;

    ...is only the declaration of the array.  It's a null object at this point.  It's just a name.

    enemies = new Enemy[EnemyRows,EnemyCols];

    ... is where the array is created.  Now it has a size and space to store Enemy objects.

    Enemy enemy = new Enemy(contentManager); <-- this is the constructor for an Enemy
    enemy.Position.X = x * enemy.Width + EnemySpacing.X;
    enemy.Position.Y = y * enemy.Height + EnemySpacing.Y;
    enemies[y,x] = enemy;

    ... is where an Enemy object is created, sprites loaded from the content manager, position in the grid set, and assigned to the correct position (y,x) in the enemies grid.

  • Sat, Mar 28 2009 8:05 PM In reply to

    • Sidar
    • Top 10 Contributor
    • Joined on Sat, Mar 28 2009
    • Posts 7

    Re: Adding [,] to a class type.

    ...Now im confused again.

    Let me ask it differently.

    Enemy[,] and Enemy();
    Are these calling on the same Class.
    Or is Enemy[,] just a new Array that got nothing to do with the class Enemy?

    As you said its just a name, so instead of Enemy[,] I could also use EnemyGrid[,]? and it would still work?

    Also i understand how new objects are being made, but i just keep getting confused because
    new Ememy() would create a new enemy object.
    and new Enemy[,] would create a new Array.

    But the array is also using the same Class name.
    I'm basically confused due the names.


    Is this array just a new array with the name Enemy?
    Or
    is this array a new array doing something with the class Enemy?
    (and I'm not talking about creating the objects in the grid, im talking about the member enemies...is the Class Enemy being attached to this array to do something specifically with Enemy objects?)

    I know it sounds like we are going in circles and apologize for that...

    Thanks again
    Sidar.

  • Sat, Mar 28 2009 8:28 PM In reply to

    • Brian Peek
    • Top 10 Contributor
      Male
    • Joined on Wed, Jul 16 2008
    • Niskayuna, NY
    • Posts 53

    Re: Adding [,] to a class type.

    I'm running out of ways to explain this... :)

    Forget about [] and ().

    Enemy is a class.  Just like Player or Object or List.

    [] is the C# syntax to denote an array of a class.  So Enemy[] or Enemy[,] is just an array that can hold objects of type Enemy.  Player[] would be an array that can hold objects of type Player.  int[] would be an array that can hold integers.  So no, you couldn't use EnemyGrid[] as the EnemyGrid class doesn't exist.

    So "Enemy[,] enemies" is an array named "enemies" that can hold objects of type "Enemy".

    "Player[,] players" would be an array named "players" that can hold objects of type "Player".

    "int[,] numbers" would be an array named "numbers" that can hold "int"s.

    Here's a link to Microsoft's Beginner Developer Learning Center on the C# language.  Perhaps have a look through the information here and it might better explain how arrays work and the syntax of the language:

    http://msdn.microsoft.com/en-us/beginner/bb308730.aspx

  • Sat, Mar 28 2009 8:35 PM In reply to

    • Sidar
    • Top 10 Contributor
    • Joined on Sat, Mar 28 2009
    • Posts 7

    Re: Adding [,] to a class type.

     " So Enemy[] or Enemy[,] is just an array that can hold objects of type Enemy"
    So i was correct on that part.
    as i said :
    " Instead, a new array is made where an object (an instance of the class Enemy) can be stored in.
    So It doesn't load anything that goes into the constructor of the class Enemy, [it just creates an array that only allows objects of the class Enemy to be stored in]
    "

    I understand now =).
    HA! it took a while, but I get it now.

    Thanks again for your time, Glad you helped out.
    I'll be back when i hit against a nother "problem" =P!

    Sidar.

     

Page 1 of 1 (13 items) | RSS
Powered by Community Server (Non-Commercial Edition), by Telligent Systems