Array of a class within ANOTHER class

I am attempting to put an array of one class and use it within another

Page 1 of 1

3 Replies - 1297 Views - Last Post: 12 May 2009 - 05:43 PM Rate Topic: -----

#1 krum110487  Icon User is offline

  • D.I.C Regular

Reputation: 39
  • View blog
  • Posts: 291
  • Joined: 07-February 09

Array of a class within ANOTHER class

Post icon  Posted 12 May 2009 - 03:46 PM

I am fairly new to C#, but I have done C++ and C before.

C# is slightly different, and I can't quite figure out how to do something...

I have the following class:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;


struct TextData
{
	public string TextValue;
	public int X;
	public int Y;
	public int Width;
	public int Height;
}

struct EquationData
{
	public string Equation;
	public bool AutoFit;
	public int X;
	public int Y;
	public int Width;
	public int Height;
}

namespace Astrila.Eq2ImgWinForms
{
	public class FlashCardClass
	{
		int EquationLength, TextLength;
		EquationData[] E;
		TextData[] T;

		public FlashCardClass()
		{
			//Set how many of each there are within this flash card.
			EquationLength = 10;
			TextLength = 10;
			E = new EquationData[EquationLength];
			T = new TextData[TextLength];

			for (int i = 0; i < EquationLength; i++)
			{
				//Initialize the equations within the struct and the class.
				E[i].X = 0;
				E[i].Y = 0;
				E[i].Width = 0;
				E[i].Height = 0;
				E[i].AutoFit = false;
				E[i].Equation = "";
			}

			for (int i = 0; i < TextLength; i++)
			{
				//Initialize the text within the struct and the class.
				T[i].X = 0;
				T[i].Y = 0;
				T[i].Width = 0;
				T[i].Height = 0;
				T[i].TextValue = "";
			}

			test();
		}

		public int test()
		{
			return 10;
		}

	}

	public class FlashCardDeck
	{
		int CardsLength;
		FlashCardClass C;

		public FlashCardDeck()
		{
			//Set how many of each there are within this flash card.
			CardsLength = 10;
			C = new FlashCardClass();
		}

		public int test()
		{
			return C.test();
		}
	}

	public class Decks
	{
		int DecksLength;
		FlashCardDeck D;

		public Decks()
		{
			//Set how many of each there are within this flash card.
			DecksLength = 10;
			D = new FlashCardDeck();
		}

		public void Test()
		{
			MessageBox.Show(Convert.ToString(D.test()));
		}
	}
}



Everything works fine like so, But I want to be able to have multiple FlashCardDecks's within the "Decks" class, and within' the "FlashCardDeck" I want to be able to have multiple "FlashCardClass"

After making this class I sat back and looked it over, I am wondering, is it worth it to go this route (if it is even possible) or should I simply make structs instead of classes?

What I have tried:
FlashCardDeck[] D;

D = new FlashCardDeck[DeckLength];
D[0].test();


this did not work...It gave me an out of bounds error...should I figure this out and do it this way, or am I making this harder than what it should be?

Is This A Good Question/Topic? 0
  • +

Replies To: Array of a class within ANOTHER class

#2 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Array of a class within ANOTHER class

Posted 12 May 2009 - 04:27 PM

View Postkrum110487, on 12 May, 2009 - 04:46 PM, said:

I am fairly new to C#, but I have done C++ and C before.

C# is slightly different, and I can't quite figure out how to do something...

I have the following class:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;


struct TextData
{
	public string TextValue;
	public int X;
	public int Y;
	public int Width;
	public int Height;
}

struct EquationData
{
	public string Equation;
	public bool AutoFit;
	public int X;
	public int Y;
	public int Width;
	public int Height;
}

namespace Astrila.Eq2ImgWinForms
{
	public class FlashCardClass
	{
		int EquationLength, TextLength;
		EquationData[] E;
		TextData[] T;

		public FlashCardClass()
		{
			//Set how many of each there are within this flash card.
			EquationLength = 10;
			TextLength = 10;
			E = new EquationData[EquationLength];
			T = new TextData[TextLength];

			for (int i = 0; i < EquationLength; i++)
			{
				//Initialize the equations within the struct and the class.
				E[i].X = 0;
				E[i].Y = 0;
				E[i].Width = 0;
				E[i].Height = 0;
				E[i].AutoFit = false;
				E[i].Equation = "";
			}

			for (int i = 0; i < TextLength; i++)
			{
				//Initialize the text within the struct and the class.
				T[i].X = 0;
				T[i].Y = 0;
				T[i].Width = 0;
				T[i].Height = 0;
				T[i].TextValue = "";
			}

			test();
		}

		public int test()
		{
			return 10;
		}

	}

	public class FlashCardDeck
	{
		int CardsLength;
		FlashCardClass C;

		public FlashCardDeck()
		{
			//Set how many of each there are within this flash card.
			CardsLength = 10;
			C = new FlashCardClass();
		}

		public int test()
		{
			return C.test();
		}
	}

	public class Decks
	{
		int DecksLength;
		FlashCardDeck D;

		public Decks()
		{
			//Set how many of each there are within this flash card.
			DecksLength = 10;
			D = new FlashCardDeck();
		}

		public void Test()
		{
			MessageBox.Show(Convert.ToString(D.test()));
		}
	}
}



Everything works fine like so, But I want to be able to have multiple FlashCardDecks's within the "Decks" class, and within' the "FlashCardDeck" I want to be able to have multiple "FlashCardClass"

After making this class I sat back and looked it over, I am wondering, is it worth it to go this route (if it is even possible) or should I simply make structs instead of classes?

What I have tried:
FlashCardDeck[] D;

D = new FlashCardDeck[DeckLength];
D[0].test();


this did not work...It gave me an out of bounds error...should I figure this out and do it this way, or am I making this harder than what it should be?


With C# there are built in generic collections that will simplify this. There is the List collection. You can use it like an array.

List<FlashCardDeck> D = new List<FlashCardDeck>();



As you need more decks you would do this:

FlashCardDeck newDeck = new FlashCardDeck();
D.Add(newDeck);



You can access the decks just like you would an array:

myDeck = D[index];



To find out how many decks are in the collection you would do this:

D.Count;



I hope that helps.
Was This Post Helpful? 0
  • +
  • -

#3 krum110487  Icon User is offline

  • D.I.C Regular

Reputation: 39
  • View blog
  • Posts: 291
  • Joined: 07-February 09

Re: Array of a class within ANOTHER class

Posted 12 May 2009 - 05:18 PM

and I am assuming that this will work when I want to add more cards to the deck itself?
Was This Post Helpful? 0
  • +
  • -

#4 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Array of a class within ANOTHER class

Posted 12 May 2009 - 05:43 PM

Yes, it would. Just create a list of cards inside the deck and do the same with the cards.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1