Presentation is loading. Please wait.

Presentation is loading. Please wait.

C# and.NET programming – introduction 1 Introduction A course in C# and.NET programming Associated book:

Similar presentations


Presentation on theme: "C# and.NET programming – introduction 1 Introduction A course in C# and.NET programming Associated book:"— Presentation transcript:

1 C# and.NET programming – introduction 1 Introduction A course in C# and.NET programming Associated book:

2 2 C# and.NET programming – introduction 2 Development environment We will create Windows applications

3 3 C# and.NET programming – introduction 3 Design environment: Toolbar messages Properties/events window Code editor Form Solution explorer Menus

4 4 C# and.NET programming – introduction 4 The Form – Most important - place controls – the UI. Display by clicking Form1.cs [Design] tab Form Textbox Button Label Listbox

5 5 C# and.NET programming – introduction 5 The Toolbox – Grouped by task Contains controls Common controls are: Buttons, Textboxes, Labels, Radio buttons etc.

6 6 C# and.NET programming – introduction 6 The Properties / Events window Properties Each control has properties – e.g. Name Position (top and left) Size (height and width) Text Description of property

7 7 C# and.NET programming – introduction 7 The Properties / Events window Events Events – happen to controls e.g: Button click KeyPress MouseMove MouseDown Others – Form load

8 8 C# and.NET programming – introduction 8 The Code Editor – where you enter your code Double-click object to enter code Some added for you – do not delete

9 9 C# and.NET programming – introduction 9 Your First C# Program Run C#, start a new Project, > Windows Application and call it ‘Hello world’ Save the project. Select File>Save All. Display the form (click form1.cs[Design] tab). Add button (drag and drop) from Toolbox to form

10 10 C# and.NET programming – introduction 10 Change the button’s text display (a property). Display the properties window, Scroll to the Text property, type in ‘Hello world’

11 11 C# and.NET programming – introduction 11 Place TextBox and label to form Change label’s caption property to ‘My First C# Program’. Form looks like:

12 12 C# and.NET programming – introduction 12 Run program – not much happens. Close it. Double-click button to add code for button click Add code: textBox1.Text="Hello world";

13 13 C# and.NET programming – introduction 13 Run program, click button. ‘Hello World’ is displayed – Your first C# program ! Note use dot notation to access property C# is case sensitive

14 14 C# and.NET programming – introduction 14 Summary Free software from Microsoft Development environment Form, Code editor, Toolbox, properties/event window Drag/drop controls (buttons) to form Double-click to add code First program

15 15 C# and.NET programming – OOP2 - 15 Topics Adding methods to class Static classes – available to all objects Overriding default methods Inheritance Protected declaration

16 16 C# and.NET programming – OOP2 - 16 Methods Add method Move Move Point one place in X and Y direction Code: public void Move( )// declare public { _x++; // move X by one _y++;// move Y by one } // end move Use: myPoint.Move( );

17 17 C# and.NET programming – OOP2 - 17 Method overloading Add second Move method – pass distance to move public void Move(int Xdistance, int Ydistance) { _x = _x + Xdistance; _y = _y + Ydistance; } Use both: myPoint.Move(12,34);// pass X and Y myPoint.Move( ); // one unit in X and Y

18 18 C# and.NET programming – OOP2 - 18 IntelliSense knows about both:

19 19 C# and.NET programming – OOP2 - 19 Static Classes - don’t have to be instantiated. ‘Distance from Origin’ example of this – available to all objects Code: class Calculate // pass x,y return distance { public static double DistanceToOrigin(int x, int y) { return Math.Sqrt(x * x + y * y); } Use: distance = Calculate.DistanceToOrigin (myPoint.X, myPoint.Y);

20 20 C# and.NET programming – OOP2 - 20 More useful ToString method ? - Override default ToString method Add code: public override string ToString( ) { return "My Point Object is at : " + _x + “," + _y; } Use: MessageBox.Show(MyPoint.ToString( )); Displays: ‘My Point Object is at : 123,456’

21 21 C# and.NET programming – OOP2 - 21 Inheritance Take a class and extend Seen this when we create our Form: public partial class Form1 : Form Let’s create Circle Class from our Point Class Can add radius and area Code: public Circle : Point

22 22 C# and.NET programming – OOP2 - 22 Add new class Circle: (Project > Add class) Call it Circle, code: class Circle : Point { } Can now create a circle: Circle smallcircle = new Circle( ); Because we are using existing code, it’s more reliable

23 23 C# and.NET programming – OOP2 - 23 Extend – define radius Constructors: class Circle : Point { private double _radius;// internal – private public Circle( ) { } public Circle(int xValue, int yValue, double radius) { _x = xValue; // _x and _y now declared protected in Point class // still private to outside world _y = yValue; _radius = radius; }

24 24 C# and.NET programming – OOP2 - 24 // add property - radius – use get and set public double radius { get { return _radius; } set { if (value >= 0) _radius = value; }

25 25 C# and.NET programming – OOP2 - 25 Extend further Add method – Area: // method Area public double area( ) { return Math.PI * _radius * _radius; }

26 26 C# and.NET programming – OOP2 - 26 Override ToString method: public override string ToString() { return "Circle at x,"+_x+" y,"+_y+ "radius,"+_radius; }

27 27 C# and.NET programming – OOP2 - 27 Use: Circle smlCircle = new Circle( ); Circle largeCircle = new Circle(12, 34, 56); smlCircle.X = 98; smlCircle.Y = 87; smlCircle.Radius = 10; MessageBox.Show(smlCircle.ToString( )); MessageBox.Show(largeCircle.ToString( )); MessageBox.Show (smlCircle.area( ).ToString( ));

28 28 C# and.NET programming – OOP2 - 28 Summary: Adding methods Static classes – available to all objects Overriding default methods Inheritance – extend class Protected declaration

29 29 C# and.NET programming – Hardware 29 Topics: Serial port Parallel port API DLLs USB USB Module

30 30 C# and.NET programming – Hardware 30 Serial Port control Non-visual control. Properties: BaudRate: 9600, DataBits: 8, Parity: None, PortName: COM1, StopBits: One. Main event: DataReceived Occurs when data is received from the port

31 31 C# and.NET programming – Hardware 31 Needs: using System.IO.Ports; Set properties serialPort1.BaudRate = 9600; serialPort1.DataBits = 8; serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), "None"); serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), "One"); Open device serialPort1.Open();

32 32 C# and.NET programming – Hardware 32 Send and receive data serialPort1.WriteLine(textBox1.Text); listBox1.Items.Add(serialPort1.ReadLine()); Or use DataReceived event

33 33 C# and.NET programming – Hardware 33 e.g: private void serialPort1_DataReceived (object sender, SerialDataReceivedEventArgs e) { listBox1.Items.Add(serialPort1.ReadLine()); }

34 34 C# and.NET programming – Hardware 34 Parallel interface. One way of getting digital I/O. Data register: Bits 0-7 data Status Register: Bits: 0-2 not used, 3-Error, 4-Select, 5- paper out, 6-acknowledge, 7 busy. Control Register: Bits: 0 strobe, 1-Auto-feed, 2-initialise, 3-select, 4-IRQ enable, 5-7 not used Base address (data register) is at 0x378 Status and control at 0x379 and 0x37A. Eight outputs Only status register bits are guaranteed inputs

35 35 C# and.NET programming – Hardware 35 Accessing the parallel port Use inpout32.dll - Lake View Research (www.lvr.com). Provides direct read and write of the I/O [DllImport("inpout32.dll", EntryPoint = "Out32")] public static extern void Output(int adress, int value); [DllImport("inpout32.dll", EntryPoint = "Inp32")] public static extern int Input(int address); Use: Output(port, data); // writes data to port temp = Input(port); // read port, puts data in temp

36 36 C# and.NET programming – Hardware 36 USB interfacing Most popular way of interfacing to the PC. Complete design involves: Hardware / USB interface PC drivers Understanding protocol and hardware limitations Difficult

37 37 C# and.NET programming – Hardware 37 The USB interface - 1 USB 2.0 three modes of operation: High speed (480 Mbits/s), Full speed (12 Mbits/s) and Low speed (1.5 Mbits/s). Device indicates its speed by pulling D+ or D- data line high. Power can be taken from USB bus – but strict limitations

38 38 C# and.NET programming – Hardware 38 The USB interface – 2 The host controls the bus - initiates & controls all messages Up to 127 devices on the bus - a device may not run at its full speed. USB Connectors: The A-type is exclusively for a host B-types are for connection to slaves. Smaller B-type for small devices such as mobile phones and digital cameras.

39 39 C# and.NET programming – Hardware 39 USB interfacing Many manufacturers make USB / I/O modules One is from DLP design: DLP-245PB-G

40 40 C# and.NET programming – Hardware 40 The module features - 1: USB 1.0 and 2.0 compatible – communication at up to 2Mbits/s 18 digital I/O lines (6 as A/D inputs) Programmable Microchip 16F877A PIC Pre-programmed code to interface to USB

41 41 C# and.NET programming – Hardware 41 The module features - 2: Code provides access to: I/O (analogue and digital) EEPROM and external digital temperature sensors Access to the PIC data bus for further expansion. No in-depth knowledge of USB hardware or software is required 40-pin DIL pin-out: further expansion is easy.

42 42 C# and.NET programming – Hardware 42 Using the module Install drivers and DLL – can then use from C# Can read and write directly to I/O Need to understand protocol

43 43 C# and.NET programming – Hardware 43

44 44 C# and.NET programming – Hardware 44 Summary Serial port Parallel port API DLLs USB USB Module


Download ppt "C# and.NET programming – introduction 1 Introduction A course in C# and.NET programming Associated book:"

Similar presentations


Ads by Google