//C# Collecties - Inleiding Object Oriented Programming namespace Alternative1 { public class Sentence { private int m_Count; private int m_Capacity = 16; private string[] m_Words = new string[0]; public virtual int Count { get { Count = this.m_Count; } } public virtual string this[int index] { get { Item = this.m_Words(index); } } public virtual void Add(string word) { this.m_Count += new System.EventHandler(1); if ((Count > this.m_Capacity)) { this.m_Capacity = 2; } this.m_Words((Count - 1)) = word; } public virtual void IndexOf(string word) { IndexOf = -1; if ((Count > 0)) { bool found; for (bool _do1 = true; _do1; _do1 = (found || (IndexOf == (Count - 1)))) { this.IndexOf += new System.EventHandler(1); found = (this.Item(IndexOf) == word); } if ((found == false)) { IndexOf = -1; } } } public virtual void Contains(string word) { Contains = (this.IndexOf(word) != -1); } } } namespace Alternative2 { public class Sentence { private Word m_StartWord = new Word(); } } namespace Global { public class Person { private string m_Name; public virtual string Name { get { Name = this.m_Name; } set { this.m_Name = value; } } } public class Client1 { Person[] persons; int personsCount; void Main() { Person person1 = new Person(); person1.Name = "John"; Person person2 = new Person(); person2.Name = "Jane"; Console.WriteLine(this.personsCount); this.AddPerson(person1); this.AddPerson(person2); Console.WriteLine(this.personsCount); Console.WriteLine(this.persons(1).Name); Console.ReadLine(); } void AddPerson(Person item) { this.persons(this.personsCount) = item; this.personsCount += new System.EventHandler(1); } } public class Persons { private Person[] m_Items; private int m_Count; public virtual Person this[int index] { get { Item = this.m_Items(index); } } public virtual int Count { get { Count = this.m_Count; } } public virtual void Add(Person item) { this.m_Items(Count) = item; this.m_Count += new System.EventHandler(1); } } public class Client2 { void Main() { Person person1 = new Person(); person1.Name = "John"; Person person2 = new Person(); person2.Name = "Jane"; Persons persons1 = new Persons(); Console.WriteLine(persons1.Count); persons1.Add(person1); persons1.Add(person2); Console.WriteLine(persons1.Count); Console.WriteLine(this.persons1(1).Name); Console.WriteLine(persons1.Item(1).Name); Console.ReadLine(); } } public class Client3 { void Main() { Person person1 = new Person(); person1.Name = "John"; Persons persons1 = new Persons(); persons1.Add(person1); } } public class Rectangle { private int m_Height; private int m_Width; public virtual int Height { get { Height = this.m_Height; } set { this.m_Height = value; } } public virtual int Width { get { Width = this.m_Width; } set { this.m_Width = value; } } public virtual void GetArea() { GetArea = (Height * Width); } } public class Rectangles { private Rectangle[] m_Items; private int m_Count; public virtual Rectangle this[int index] { get { Item = this.m_Items(index); } } public virtual int Count { get { Count = this.m_Count; } } public virtual void Add(Rectangle rectangle) { this.m_Items(Count) = rectangle; this.m_Count += new System.EventHandler(1); } public virtual void RemoveAt(int index) { this.m_Items(itemIndex) = this.m_Items((itemIndex + 1)); this.m_Count = 1; } public virtual void GetTotalArea() { for (System.Collections.IEnumerator _it1 = this.m_Items.GetEnumerator(); _it1.MoveNext(); ) { Rectangle item = ((Rectangle)(_it1.Current)); this.GetTotalArea += new System.EventHandler(item.GetArea()); } } } public class Exercise1Solution { void Main() { Rectangle rectangle1 = new Rectangle(); rectangle1.Height = 1; rectangle1.Width = 2; Console.WriteLine(rectangle1.GetArea()); Rectangle rectangle2 = new Rectangle(); rectangle2.Height = 3; rectangle2.Width = 4; Console.WriteLine(rectangle2.GetArea()); Rectangles rectangles1 = new Rectangles(); rectangles1.Add(rectangle1); rectangles1.Add(rectangle2); Console.Write((this.rectangles1(index).GetArea() + " ")); Console.WriteLine(); Console.WriteLine(rectangles1.GetTotalArea()); rectangles1.RemoveAt(1); Console.Write((this.rectangles1(index).GetArea() + " ")); Console.WriteLine(); Console.WriteLine(rectangles1.GetTotalArea()); Console.ReadLine(); } } public class SentenceTestFixture { void Main() { Sentence sentence1 = new Sentence(); Console.WriteLine((sentence1.Count == 0)); Console.WriteLine((sentence1.IndexOf("Hello") == -1)); Console.WriteLine((sentence1.Contains("Hello") == false)); sentence1.Add("Hello"); Console.WriteLine((sentence1.Count == 1)); Console.WriteLine((sentence1.Item(0) == "Hello")); Console.WriteLine((sentence1.IndexOf("Hello") == 0)); Console.WriteLine((sentence1.Contains("Hello") == true)); Console.WriteLine((sentence1.IndexOf("World") == -1)); Console.WriteLine((sentence1.Contains("World") == false)); sentence1.Add("World"); Console.WriteLine((sentence1.Count == 2)); Console.WriteLine((sentence1.Item(0) == "Hello")); Console.WriteLine((sentence1.Item(1) == "World")); Console.WriteLine((sentence1.IndexOf("Hello") == 0)); Console.WriteLine((sentence1.Contains("Hello") == true)); Console.WriteLine((sentence1.IndexOf("World") == 1)); Console.WriteLine((sentence1.Contains("World") == true)); Console.ReadLine(); } } public class Sentence { private string[] m_Words = new string[0]; public virtual int Count { get { Count = this.m_Words.Length; } } public virtual string this[int index] { get { if (((index >= 0) && (index < Count))) { Item = this.m_Words(index); } } } public virtual void Add(string word) { this.m_Words((Count - 1)) = word; } public virtual void IndexOf(string word) { IndexOf = -1; if ((Count > 0)) { bool found; for (bool _do1 = true; _do1; _do1 = (found || (IndexOf == (Count - 1)))) { this.IndexOf += new System.EventHandler(1); found = (this.Item(IndexOf) == word); } if ((found == false)) { IndexOf = -1; } } } public virtual void Contains(string word) { Contains = (this.IndexOf(word) != -1); } } //Bezoek www.vbvoorbeelden.be voor meer C# voorbeelden. //Copyright - De Wolf / vbvoorbeelden - 2003-2011 - Alle rechten voorbehouden.