Successfully reported this slideshow.
Your SlideShare is downloading. ×

C++ Windows Forms L07 - Collections

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Loading in …3
×

Check these out next

1 of 38 Ad

More Related Content

Slideshows for you (20)

Similar to C++ Windows Forms L07 - Collections (20)

Advertisement

More from Mohammad Shaker (20)

Recently uploaded (20)

Advertisement

C++ Windows Forms L07 - Collections

  1. 1. C++.NET Windows Forms Course L07 –Collections Mohammad Shaker mohammadshakergtr.wordpress.com C++.NET Windows Forms Course @ZGTRShaker
  2. 2. Welcome!
  3. 3. Collections?
  4. 4. Collections Generic generic?
  5. 5. Class Comparer<T> Description Provides a base class for implementations of the IComparer<T>generic interface. Dictionary<TKey, TValue> Dictionary<TKey, TValue>::KeyCollection Dictionary<TKey, TValue>::ValueCollection EqualityComparer<T> Represents a collection of keys and values. Represents the collection of keys in a Dictionary<TKey, TValue>. This class cannot be inherited. HashSet<T> KeyedByTypeCollection<TItem> KeyNotFoundException Represents a set of values. Provides a collection whose items are types that serve as keys. The exception that is thrown when the key specified for accessing an element in a collection does not match any key in the collection. LinkedList<T> LinkedListNode<T> Represents a doubly linked list. Represents a node in a LinkedList<T>. This class cannot be inherited. List<T> Represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists. Represents a first-in, first-out collection of objects. Queue<T> Represents the collection of values in a Dictionary<TKey, TValue>. This class cannot be inherited. Provides a base class for implementations of theIEqualityComparer<T> generic interface.
  6. 6. SortedDictionary<TKey, TValue> Represents a collection of key/value pairs that are sorted on the key. SortedDictionary<TKey, TValue>::KeyCollection Represents the collection of keys in a SortedDictionary<TKey, TValue>. This class cannot be inherited. SortedDictionary<TKey, TValue>::ValueCollection Represents the collection of values in a SortedDictionary<TKey, TValue>. This class cannot be inherited SortedList<TKey, TValue> Represents a collection of key/value pairs that are sorted by key based on the associated IComparer<T> implementation. SortedSet<T> Represents a collection of objects that is maintained in sorted order. Represents a variable size last-in-first-out (LIFO) collection of instances of the same arbitrary type. Stack<T> SynchronizedCollection<T> Provides a thread-safe collection that contains objects of a type specified by the generic parameter as elements. SynchronizedKeyedCollection<K, T> Provides a thread-safe collection that contains objects of a type specified by a generic parameter and that are grouped by keys. SynchronizedReadOnlyCollection<T> Provides a thread-safe, read-only collection that contains objects of a type specified by the generic parameter as elements.
  7. 7. Peak on Collections private : System::Collections::Generic::LinkedList <String ^> ^MyStrList ; private : System::Collections::Generic::List<TextBox ^> ^List ; private : System::Collections::Generic::Stack <String ^> ^ MyStack; private : System::Collections::ArrayList ^MyArrayList ;
  8. 8. Peak on Collections private : System::Collections::Generic::Stack < String ^> ^MyStack; private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { MyStack = gcnew System::Collections::Generic::Stack < String ^>; }
  9. 9. Peak on Collections • Let’s have the following! private : System::Collections::Generic::LinkedList <Button^> ^MyList; private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { MyList = gcnew System::Collections::Generic:: LinkedList <Button ^>; }
  10. 10. Peak on Collections private : System::Collections::Generic::LinkedList < String ^> ^MyList; private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { MyList = gcnew System::Collections::Generic::LinkedList < String ^>; } private: System::Void button1_Click_1(System::Object^ System::EventArgs^ e) { MyList->AddLast(“MeMe"); MyList->AddLast(“MeMa"); } sender,
  11. 11. Peak on Collections • What’s wrong? private : System::Collections::Generic::LinkedList < String ^> ^MyList; private: System::Void button1_Click_1(System::Object^ System::EventArgs^ e) { MyList->AddLast(“MeMe"); MyList->AddLast(“MeMa"); } Runtime error. the LinkedList is still NULL sender,
  12. 12. Peak on Collections • “for each” loop private: System::Void button1_Click_1(System::Object^ sender, System::EventArgs^ e) { for each (String ^str in MyList) { textBox1->Text += str + Environment::NewLine ; } }
  13. 13. private : System::Collections::Generic::LinkedList < String ^> ^MyList; private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { MyList = gcnew System::Collections::Generic::LinkedList < String ^>; } private: System::Void button1_Click_1(System::Object^ System::EventArgs^ e) { MyList->AddLast(textBox1->Text); } sender, private: System::Void button1_Click_2(System::Object^ sender, System::EventArgs^ e) { for each (String ^str in MyList) { textBox1->Text += str + Environment::NewLine ; } }
  14. 14. private : System::Collections::Generic::LinkedList < Button^> ^MyList; private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { MyList = gcnew System::Collections::Generic::LinkedList < Button ^>; } private: System::Void button1_Click_1(System::Object^ System::EventArgs^ e) { Button ^B1 = gcnew Button ; MyList->AddLast(B1); } sender, private: System::Void button1_Click_2(System::Object^ sender, System::EventArgs^ e) { static int Counter = 1 ; for each (Button ^B in MyList) { B->Text = “Button” + Counter.ToString(); B->Height = 30 ; B->Width = 50 ; Counter++ ; } }
  15. 15. Peak on Collections • Needs to be static? private: System::Void button1_Click_2(System::Object^ sender, System::EventArgs^ e) { int Counter = 1 ; for each (Button ^B in MyList) { B->Text = “Button” + Counter.ToString(); B->Height = 30 ; B->Width = 50 ; Counter++ ; } }
  16. 16. Peak on Collections - List private: System::Collections::Generic::List<String^> ^MyList ; private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { MyList = gcnew System::Collections::Generic::List<String^> (4) ; MyList->Add("Z") ; MyList->Add("G") ; MyList->Add("T") ; MyList->Add("R") ; } private: System::Void button1_Click_1(System::Object^ sender, System::EventArgs^ e) { for each (String ^str in MyList) { textBox1->Text += str + " " ; } }
  17. 17. List
  18. 18. List private: System::Collections::Generic::List<String^> ^MyList ; private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { MyList = gcnew System::Collections::Generic::List<String^> (4) ; MyList->Add("Z") ; MyList->Add("G") ; MyList->Add("T") ; MyList->Add("R") ; } private: System::Void button1_Click_1(System::Object^ sender, System::EventArgs^ e) { for(int i=0; i<4 ; i++) { textBox1->Text += MyList[i] + " "; } }
  19. 19. List
  20. 20. List private: System::Collections::Generic::List<String^> ^MyList ; private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { MyList = gcnew System::Collections::Generic::List<String^> (4) ; MyList[0] = “Z” ; MyList[1] = “G” ; MyList[2] = “T” ; MyList[3] = “R” ; } private: System::Void button1_Click_1(System::Object^ sender, System::EventArgs^ e) { for(int i=0; i<4 ; i++) { textBox1->Text += MyList[i] + " "; } }
  21. 21. Peak on Collections – ArrayList private: System::Collections::ArrayList ^MyArrayList ; private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { MyArrayList = gcnew System::Collections::ArrayList (4) ; static int Counter = 0 ; Button ^B1 = gcnew Button ; TextBox ^T1 ; // Not initialized String ^S = "ZGTR"; int i ; // Not initialized MyArrayList->Add(B1) ; MyArrayList->Add(T1) ; MyArrayList->Add(S) ; MyArrayList->Add(i) ; } Everything is good
  22. 22. Peak on Collections – ArrayList private: System::Collections::ArrayList ^MyArrayList ; private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { MyArrayList = gcnew System::Collections::ArrayList (4) ; static int Counter = 0 ; Button ^B1,^B2= gcnew Button ; TextBox ^T1 ; String ^S = "ZGTR"; int i ; MyArrayList->Add(B1) ; MyArrayList->Add(T1) ; MyArrayList->Add(S) ; MyArrayList->Add(i) ; MyArrayList[0] = B2 ; MyArrayList[3] = 6 ; } Everything is good
  23. 23. Peak on Collections – ArrayList private: System::Collections::ArrayList ^MyArrayList ; private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { MyArrayList = gcnew System::Collections::ArrayList (4) ; static int Counter = 0 ; Button ^B1,^B2= gcnew Button ; TextBox ^T1 ; String ^S = "ZGTR"; int i ; MyArrayList->Add(B1) ; MyArrayList->Add(T1) ; MyArrayList->Add(S) ; MyArrayList->Add(i) ; MyArrayList[0] = B2 ; MyArrayList[3] = B2 ; // Here! } Everything is good
  24. 24. Peak on Collections – ArrayList private: System::Collections::ArrayList ^MyArrayList ; private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { MyArrayList = gcnew System::Collections::ArrayList (4) ; static int Counter = 0 ; Button ^B1,^B2= gcnew Button ; TextBox ^T1 ; String ^S = "ZGTR"; int i ; MyArrayList->Add(B1) ; MyArrayList->Add(T1) ; MyArrayList->Add(S) ; MyArrayList->Add(i) ; MyArrayList[0] = 6; // Here! MyArrayList[3] = B2 ; // Here! } Everything is good
  25. 25. Peak on Collections – ArrayList private: System::Collections::ArrayList ^MyArrayList ; private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { MyArrayList = gcnew System::Collections::ArrayList (4) ; static int Counter = 0 ; Button ^B1,^B2= gcnew Button ; TextBox ^T1 ; String ^S = "ZGTR"; int i ; B1->Height = 50 ; MyArrayList->Add(B1) ; MyArrayList->Add(T1) ; MyArrayList->Add(S) ; MyArrayList->Add(i) ; MyArrayList[0] = B2 ; MyArrayList[3] = 6 ; } Compiler error. No new for Button1
  26. 26. Peak on Collections – ArrayList private: System::Collections::ArrayList ^MyArrayList ; private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { MyArrayList = gcnew System::Collections::ArrayList (4) ; static int Counter = 0 ; Button ^B1,^B2= gcnew Button ; B1 = gcnew Button ; TextBox ^T1 ; String ^S = "ZGTR"; int i ; B1->Height = 50 ; MyArrayList->Add(B1) ; MyArrayList->Add(T1) ; MyArrayList->Add(S) ; MyArrayList->Add(i) ; MyArrayList[0] = B2 ; MyArrayList[3] = 6 ; } Everything is good
  27. 27. Peak on Collections – ArrayList private: System::Collections::ArrayList ^MyArrayList ; private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { MyArrayList = gcnew System::Collections::ArrayList (4) ; static int Counter = 0 ; Button ^B1,^B2= gcnew Button ; B1 = gcnew Button ; TextBox ^T1 ; String ^S = "ZGTR"; int i ; B1->Height = 50 ; MyArrayList->Add(B1) ; MyArrayList->Add(T1) ; MyArrayList->Add(S) ; MyArrayList->Add(i) ; MyArrayList[0] = B2 ; MyArrayList[4] = 6 ; } Compiler error. index = 4! Wrong!
  28. 28. Peak on Collections – ArrayList private: System::Collections::ArrayList ^MyArrayList ; private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { MyArrayList = gcnew System::Collections::ArrayList (4) ; static int Counter = 0 ; Button ^B1,^B2= gcnew Button ; B1 = gcnew Button ; TextBox ^T1 ; String ^S = "ZGTR"; int i ; B1->Height = 50 ; MyArrayList->Add(B1) ; MyArrayList->Add(T1) ; MyArrayList->Add(S) ; MyArrayList->Add(i) ; B2 = MyArrayList[0] ; // 1 MyArrayList[3] = 6 ; } Compiler error. object^ and Button^ in 1
  29. 29. Peak on Collections – ArrayList private: System::Collections::ArrayList ^MyArrayList ; private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { MyArrayList = gcnew System::Collections::ArrayList (4) ; static int Counter = 0 ; Button ^B1 = gcnew Button ; TextBox ^T1 ; String ^S = "ZGTR"; int i ; MyArrayList->Add(B1) ; MyArrayList->Add(T1) ; MyArrayList->Add(S) ; MyArrayList->Add(i) ; } Everything is good
  30. 30. Peak on Collections – ArrayList private: System::Collections::ArrayList ^MyArrayList ; private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { MyArrayList = gcnew System::Collections::ArrayList (4) ; static int Counter = 0 ; Button ^B1 = gcnew Button ; TextBox ^T1 ; String ^S = "ZGTR"; int i ; MyArrayList->Add(B1) ; MyArrayList->Add(T1) ; MyArrayList->Add(S) ; MyArrayList->Add(i) ; } private: System::Void button1_Click_1(System::Object^ System::EventArgs^ e) { textBox1->Text =MyArrayList[0] ->Height ; } Compile error sender,
  31. 31. Peak on Collections – ArrayList private: System::Collections::ArrayList ^MyArrayList ; private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { MyArrayList = gcnew System::Collections::ArrayList (4) ; static int Counter = 0 ; Button ^B1 = gcnew Button ; TextBox ^T1 ; String ^S = "ZGTR"; int i ; MyArrayList->Add(B1) ; MyArrayList->Add(T1) ; MyArrayList->Add(S) ; MyArrayList->Add(i) ; } private: System::Void button1_Click_1(System::Object^ sender, System::EventArgs^ e) { textBox1->Text =(MyArrayList[0]->Height)->ToString() ; } Compile error
  32. 32. Peak on Collections – ArrayList private: System::Collections::ArrayList ^MyArrayList ; private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { MyArrayList = gcnew System::Collections::ArrayList (4) ; static int Counter = 0 ; Button ^B1 = gcnew Button ; TextBox ^T1 ; String ^S = "ZGTR"; int i ; MyArrayList->Add(B1) ; MyArrayList->Add(T1) ; MyArrayList->Add(S) ; MyArrayList->Add(i) ; } private: System::Void button1_Click_1(System::Object^ sender, System::EventArgs^ e) { textBox1->Text =(MyArrayList[0]->Height).ToString() ; } Compile error
  33. 33. ArrayList - dynamic_cast private: System::Collections::ArrayList ^MyArrayList ; private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { MyArrayList = gcnew System::Collections::ArrayList (4) ; static int Counter = 0 ; Button ^B1 = gcnew Button ; TextBox ^T1 ; String ^S = "ZGTR"; int i ; MyArrayList->Add(B1) ; MyArrayList->Add(T1) ; MyArrayList->Add(S) ; MyArrayList->Add(i) ; } private: System::Void button1_Click_1(System::Object^ sender, System::EventArgs^ e) { textBox1->Text =((dynamic_cast <Button^>(MyArrayList[0]))->Height).ToString(); } Everything is good. And will print 23. but why?
  34. 34. ArrayList - dynamic_cast private: System::Collections::ArrayList ^MyArrayList ; private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { MyArrayList = gcnew System::Collections::ArrayList (4) ; static int Counter = 0 ; Button ^B1 = gcnew Button ; TextBox ^T1 ; String ^S = "ZGTR"; int i ; B1->Height = 30 ; MyArrayList->Add(B1) ; MyArrayList->Add(T1) ; MyArrayList->Add(S) ; MyArrayList->Add(i) ; } private: System::Void button1_Click_1(System::Object^ sender, System::EventArgs^ e) { textBox1->Text =((dynamic_cast <Button^>(MyArrayList[0]))->Height).ToString(); } Everything is good. And will print 30.
  35. 35. ArrayList • It’s not a Generic* – private: System::Collections::ArrayList ^MyArrayList ; • Drop in performance! _____________________________________________________ *Generic : class Typed
  36. 36. Enough said, let’s dig deep live
  37. 37. That’s it for today!

×