|
Types and Members
Lesson1Using Data Types
Table 1 Implicit conversions
int anInteger =
10000;
short aShort;
aShort = (short)
anInteger;
Data Type Functionality
int I = 100;
object O;
O = I;
string S =
“1234”;
int I =
int.Parse(S);
Table 2 String Instance Methods
Table 3 String Static Methods Lesson 2
Constants
public const double Pi =
3.14159265;
Enums
public enum
DaysOfWeek
{
Monday =
1,
Tuesday =
2,
Wednesday =
3,
Thursday =
4,
Friday =
5,
Saturday =
6,
Sunday =
7
}
public enum DaysOfWeek :
byte
{
…
}
Public enum
Numbers
{
zero,
one,
two
}
MessageBox.Show((int)Numbers.two *
2).ToString()); // Display 4
public void ScheduleDayOff( DaysOfWeek
day )
{
switch( day
)
{
…
}
}
Arrays
int[] myIntegers;
myIntegers = new
int[32];
int[] myIntegers = new
int[32];
int[] myIntegers = new
int[32];
myIntegers = new
int[45];
// Declare 5 by 3
array
int[,] intArray = new
int[5,3];
// Decalre 2D array and set initial
values
int[,] intArray2 =
{{1,2,3},{4,5,6}};
// Declare cubic
array
int[,,] cubeArray =
{{7,2},{1,4},{3,5}};
string[][] Families = new
string[3][];
Families[0] = new string[] {“Smith”,
“Mum”, “Dad”, “Uncle Phil”};
Families[1] = new string[] {“Jones”,
“Mum”, “Dad”, “Suzie”, “Bobby”};
Families[2] = new string[] {“Williams”,
“Earl”, “Bob”};
Collections
Table 4 Members of System.Collections
// Create
ArrayList
System.Collections.ArrayList myList =
new System.Collections.ArrayList;
//Add item
Widget myWidget = new
Widget;
myList.Add(myWidget);
// Access item using indexer (note
ArrayList is zero-based)
object myObject;
myObject =
myList[0];
// Indexer always returns objects. To
obtain reference to same type as stored must cast
Widget aWidget;
aWidget = (Widget)
myList[0];
// Remove uses reference to
object
Widget anotherWidget = new
Widget();
myList.Add(anotherWidget);
myList.Remove(anotherWidget);
// RemoveAt uses
indexer
myList.RemoveAt(0);
// Count property is number of items in
collection (as array zero based the value returned in one more than upper
bound)
int arraySize =
myList.Count;
int[] myArray = new int[]
{1,2,3,4,5};
foreach(int I in
myArray)
{
MessageBox(I.ToString());
}
foreach(object O in
myList)
{
if(o.GetType ==
typeof(string))
{
MessageBox(O.ToString());
}
}
int[] myArray = new int[]
{1,2,3,4,5};
for(int x=0; x <=
myArray.GetUpperBound(0), x++)
{
myArray[x]++;
MessageBox(myArray[x].ToString());
}
Lesson 3Implementing Properties
textBox1.Text = “Text
property”;
string myString;
myString =
textBox1.Text;
private string
theText;
public string
myText
{
get
{
return theText;
}
set
{
theText = value;
}
}
Read Only Properties
private readonly int
theInt;
public int
InstanceNumber
{
get
{
return theInt;
}
}
Write Only Properties
Indexer
private int[]
IntArray;
public int this [int
index]
{
get
{
return IntArray[index];
}
set
{
Intarray[index] = value;
}
}
Collection Properties
private readonly
System.Collections.ArrayList myWidgets = new
System.Collections.ArrayList();
public System.Collections.ArrayList
Widgets
{
get
{
return myWidgets;
}
}
o Wrap collection in property, providing conversion and validation code in accessor. Implement as indexer, or pair of methods (C# does not support parameterised properties)
private System.Collections.ArrayList
myWidgets = new System.Collections.ArrayList ();
public Widget GetWidget(int
I)
{
return
(Widget)myWidgets[I];
}
public void SetWidget(int I, Widget
Wid)
{
myWidgets[I] =
Wid;
}
o Implement own strongly typed collection – derive from System.Collections.CollectionBase. Expose collection directly and ensure only objects of specified type are added
Lesson 4Delegates and Events
Delegates
public delegate int myDelegate(double
D);
// Target method for
delegate
public int ReturnInt(double
D)
{
}
// Create instance of
myDelegate
public void
aMethod()
{
myDelegate
aDelegate = new myDelegate(ReturnInt);
}
// Use delegate to invoke
method
aDelegate(12345);
Declaring and Raising Events
// Declare delegate and
event
public delegate void
calculateDelegate(double D);
public event calculationDelegate
CalculationComplete;
// Raise event
CalculationComplete(66532);
Event Handlers
// Assume existence of method
DisplayResults with signature appropriate for
calculationDelegate.
// Create new delegate to create the
association.
Account.CalculationComplete += new
calculationDelegate(DisplayResults);
// Create association with existing
delegate
CalculationDelegate calc = new
calculationDelegate(DisplayResults);
Account.CalculationComplete +=
calc;
// System.EventHandler is delegate for
most controls in System.windows.Formds namespace. Designate event handler
for Click event of control called button1
button1.Click += new
System.EventHandler(clickHandler);
// Remove association between
Account.CalculationComplete and DisplayResults
method
Account.CalculatioComplete -= new
CalculationDelegate(DisplayResults);
Button1.Click += new
System.EventHandler(ClickHandler);
Button2.Click += new
System.EventHandler(ClickHandler);
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Last updated: 26th December 2006. copyright © 2000 Greystoke Systems Ltd. Web address: http://www.gsys.biz/Documents/Services/Tuition/MCP/70-316/ApplicationDeployment.htm |