Data Structure: Part 4, Even More on List<T>, WRT Generics

In the code example in my previous blogs, I discussed how to use a List<T> to store data strings.  To use our list in Windows Phone 7, we need to determine how to utilize the List<T> to store our data, AND put data into the List<T>.  List<T> is a type of programming object referred to as a Generic.

For more information about Generics, see the link:

For a discussion on how to work with arrays and random numbers see my blog:

List<T> is one of a number of Generics. 

Generics are used with collections and methods that operate on them, and for code using Framework version 2.0 and later they should be utilized. 

In the previous blogs we utilized the List<T> to do a manipulation of a list of names.  The initialization was performed in the method that utilized the List<T>.  What if you want to use add to the list in one method and then use it in another method in the same class.  Good luck finding an example of how to do that.  It must be that all of the writers think that it is obvious that the generics are biologically wired in all readers.

To resolve this, here is an example using the List<T> within a simple Windows Phone 7 project.

Build a Windows Phone Project, name doesn’t make any difference.

Add the variables to (if you named your game class differently then it will say that class name) to the top of the class definition, you should see the lines show below. 

public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; //This is the class definition anything put in here is available to the methods and properties in the class, including //subclasses public Game1( )

Make the top of your code look like the following, you are adding a variable to hold the font, another one to hold the position for the text, the strings from the list and finally the List<T> Generic object.  Placing these variables in this position of the program makes the variables available to the methods and properties in the class.

public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; //Start code copy here SpriteFont Font1; Vector2 FontPos; string output, output1; List<String> Demo = new List<String>( ); //End code copy here public Game1( ) //*******END

Scroll down in your code and find the “Initialize” method, add the following code, in this case when the code is initialized the method testList is called where string items are added to the list, and the BuildingList builds the output so that is can be drawn one time in the draw method.  If you put the “foreach” in the Draw method, it will keep generating the list and look pretty ugly.  The “ \n” forces a linefeed and is needed so that the items in the list aren’t drawn on top of each other.

protected override void Initialize() { // TODO: Add your initialization logic here testList(); BuildingList(); base.Initialize(); } void testList() { Demo.Add("Beep"); Demo.Add("Blah"); Demo.Add("Jelly"); Demo.Add("Silly"); } private void BuildingList() { foreach (string demo in Demo) { output = output + demo + " \n"; } }

Now scroll down a bit and find the draw method.  Just to demonstrate the use of a list in a separate method I added the Demo.Add(“Silly”).  In the output I also placed “Test” during my exploration of the process.


protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); Demo.Add("Silly"); // Draw Hello World spriteBatch.Begin(); Vector2 FontOrigin = Font1.MeasureString("Test \n" + output) / 2; // Draw the string spriteBatch.DrawString(Font1, "Test \n" + output, FontPos, Color.LightGreen, 0, FontOrigin, 1.0f, SpriteEffects.None, 0.5f); spriteBatch.End(); base.Draw(gameTime); }

If you haven’t already, you need to add the spritefon1.sprintefont file to your Content Project:



Once you have the squiggly lines removed, run the code, and you should see something like the following:









List Generic

Comments (0)

Skip to main content