C++ Windows Forms L07 - Collections
Upcoming SlideShare
Loading in...5
×
 

C++ Windows Forms L07 - Collections

on

  • 212 views

C++ Windows Forms L07 - Collections of C++ Windows Forms Light Course

C++ Windows Forms L07 - Collections of C++ Windows Forms Light Course

Statistics

Views

Total Views
212
Views on SlideShare
212
Embed Views
0

Actions

Likes
0
Downloads
10
Comments
0

0 Embeds 0

No embeds

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    C++ Windows Forms L07 - Collections C++ Windows Forms L07 - Collections Presentation Transcript

    • C++.NET Windows Forms Course L07 –Collections Mohammad Shaker mohammadshakergtr.wordpress.com C++.NET Windows Forms Course @ZGTRShaker
    • Welcome!
    • Collections?
    • Collections Generic generic?
    • 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.
    • 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.
    • 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 ;
    • 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 ^>; }
    • 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 ^>; }
    • 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,
    • 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,
    • 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 ; } }
    • 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 ; } }
    • 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++ ; } }
    • 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++ ; } }
    • 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 + " " ; } }
    • List
    • 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] + " "; } }
    • List
    • 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] + " "; } }
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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!
    • 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
    • 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
    • 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,
    • 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
    • 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
    • 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?
    • 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.
    • ArrayList • It’s not a Generic* – private: System::Collections::ArrayList ^MyArrayList ; • Drop in performance! _____________________________________________________ *Generic : class Typed
    • Enough said, let’s dig deep live
    • That’s it for today!