c++ windows forms l07 - collections

38
Mohammad Shaker mohammadshakergtr.wordpress.com C++.NET Windows Forms Course @ZGTRShaker C++.NET Windows Forms Course L07 –Collections

Upload: mohammad-shaker

Post on 05-Dec-2014

356 views

Category:

Technology


4 download

DESCRIPTION

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

TRANSCRIPT

Page 1: C++ Windows Forms L07 - Collections

Mohammad Shakermohammadshakergtr.wordpress.com

C++.NET Windows Forms Course@ZGTRShaker

C++.NET Windows Forms Course

L07 –Collections

Page 2: C++ Windows Forms L07 - Collections

Welcome!

Page 3: C++ Windows Forms L07 - Collections

Collections?

Page 4: C++ Windows Forms L07 - Collections

Collections Genericgeneric?

Page 5: C++ Windows Forms L07 - Collections

Class DescriptionComparer<T> Provides a base class for implementations of

the IComparer<T>generic interface.

Dictionary<TKey, TValue> Represents a collection of keys and values.

Dictionary<TKey, TValue>::KeyCollection

Represents the collection of keys in a Dictionary<TKey, TValue>. This class cannot be inherited.

Dictionary<TKey, TValue>::ValueCollection

Represents the collection of values in a Dictionary<TKey, TValue>. This class cannot be inherited.

EqualityComparer<T> Provides a base class for implementations of theIEqualityComparer<T> generic interface.

HashSet<T> Represents a set of values.

KeyedByTypeCollection<TItem> Provides a collection whose items are types that serve as keys.

KeyNotFoundException 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> Represents a doubly linked list.

LinkedListNode<T> 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.

Queue<T> Represents a first-in, first-out collection of objects.

Page 6: C++ Windows Forms L07 - Collections

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.

Stack<T> Represents a variable size last-in-first-out (LIFO) collection of instances of the same arbitrary type.

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.

Page 7: C++ Windows Forms L07 - Collections

Peak on Collections

private : System::Collections::Generic::LinkedList <String ^> ^MyStrList ;

private : System::Collections::Generic::Stack <String ^> ^ MyStack;

private : System::Collections::ArrayList ^MyArrayList ;

private : System::Collections::Generic::List<TextBox ^> ^List ;

Page 8: C++ Windows Forms L07 - Collections

Peak on Collections

private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)

{

MyStack = gcnew System::Collections::Generic::Stack < String ^>;

}

private : System::Collections::Generic::Stack < String ^> ^MyStack;

Page 9: C++ Windows Forms L07 - Collections

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 ^>;

}

Page 10: C++ Windows Forms L07 - Collections
Page 11: C++ Windows Forms L07 - Collections

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^ sender,

System::EventArgs^ e)

{

MyList->AddLast(“MeMe");

MyList->AddLast(“MeMa");

}

Page 12: C++ Windows Forms L07 - Collections

Peak on Collections

• What’s wrong?private : System::Collections::Generic::LinkedList < String ^> ^MyList;

private: System::Void button1_Click_1(System::Object^ sender,

System::EventArgs^ e)

{

MyList->AddLast(“MeMe");

MyList->AddLast(“MeMa");

}

Runtime error. the LinkedList is still NULL

Page 13: C++ Windows Forms L07 - Collections

Peak on Collections

• “for each” loopprivate: System::Void button1_Click_1(System::Object^ sender,

System::EventArgs^ e)

{

for each (String ^str in MyList)

{

textBox1->Text += str + Environment::NewLine ;

}

}

Page 14: C++ Windows Forms L07 - 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^ sender,

System::EventArgs^ e)

{

MyList->AddLast(textBox1->Text);

}

private: System::Void button1_Click_2(System::Object^ sender,

System::EventArgs^ e)

{

for each (String ^str in MyList)

{

textBox1->Text += str + Environment::NewLine ;

}

}

Page 15: C++ Windows Forms L07 - Collections

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^ sender,

System::EventArgs^ e)

{

Button ^B1 = gcnew Button ;

MyList->AddLast(B1);

}

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++ ;

}

}

Page 16: C++ Windows Forms L07 - Collections

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++ ;

}

}

Page 17: C++ Windows Forms L07 - Collections

Peak on Collections - Listprivate: 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 + " " ;

}

}

Page 18: C++ Windows Forms L07 - Collections

List

Page 19: C++ Windows Forms L07 - Collections

Listprivate: 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] + " ";

}

}

Page 20: C++ Windows Forms L07 - Collections

List

Page 21: C++ Windows Forms L07 - Collections

Listprivate: 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] + " ";

}

}

Page 22: C++ Windows Forms L07 - Collections

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

Page 23: C++ Windows Forms L07 - Collections

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

Page 24: C++ Windows Forms L07 - Collections

Peak on Collections – ArrayListprivate: 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

Page 25: C++ Windows Forms L07 - Collections

Peak on Collections – ArrayListprivate: 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

Page 26: C++ Windows Forms L07 - Collections

Peak on Collections – ArrayListprivate: 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

Page 27: C++ Windows Forms L07 - Collections

Peak on Collections – ArrayListprivate: 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

Page 28: C++ Windows Forms L07 - Collections

Peak on Collections – ArrayListprivate: 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!

Page 29: C++ Windows Forms L07 - Collections

Peak on Collections – ArrayListprivate: 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

Page 30: C++ Windows Forms L07 - Collections

Peak on Collections – ArrayListprivate: 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

Page 31: C++ Windows Forms L07 - Collections

Peak on Collections – ArrayListprivate: 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 ;

}

Compile error

Page 32: C++ Windows Forms L07 - Collections

Peak on Collections – ArrayListprivate: 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

Page 33: C++ Windows Forms L07 - Collections

Peak on Collections – ArrayListprivate: 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

Page 34: C++ Windows Forms L07 - Collections

ArrayList - dynamic_castprivate: 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?

Page 35: C++ Windows Forms L07 - Collections

ArrayList - dynamic_castprivate: 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.

Page 36: C++ Windows Forms L07 - Collections

ArrayList

• It’s not a Generic*– private: System::Collections::ArrayList ^MyArrayList ;

• Drop in performance!

_____________________________________________________*Generic : class Typed

Page 37: C++ Windows Forms L07 - Collections

Enough said, let’s dig deep live

Page 38: C++ Windows Forms L07 - Collections

That’s it for today!