Upload
mohammad-shaker
View
351
Download
4
Embed Size (px)
DESCRIPTION
C++ Windows Forms L07 - Collections of C++ Windows Forms Light Course
Citation preview
Mohammad Shakermohammadshakergtr.wordpress.com
C++.NET Windows Forms Course@ZGTRShaker
C++.NET Windows Forms Course
L07 –Collections
Welcome!
Collections?
Collections Genericgeneric?
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.
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.
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 ;
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;
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^ sender,
System::EventArgs^ e)
{
MyList->AddLast(“MeMe");
MyList->AddLast(“MeMa");
}
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
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 ;
}
}
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 ;
}
}
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++ ;
}
}
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 - 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 + " " ;
}
}
List
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] + " ";
}
}
List
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] + " ";
}
}
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 – 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
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
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
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
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!
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
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
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
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
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
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?
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.
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!