Presentation is loading. Please wait.

Presentation is loading. Please wait.

Web Services Week 2 Aims: Getting started with creating simple C# applications within Visual Studio.NET Objectives: –An introduction to the syntax of C#.NET.

Similar presentations


Presentation on theme: "Web Services Week 2 Aims: Getting started with creating simple C# applications within Visual Studio.NET Objectives: –An introduction to the syntax of C#.NET."— Presentation transcript:

1 Web Services Week 2 Aims: Getting started with creating simple C# applications within Visual Studio.NET Objectives: –An introduction to the syntax of C#.NET –Using objects –The creation solutions using the Visual Studio.NET IDE

2 Object Orientated Programming Paradigm for nearly all software development Lets programmer use a number of objects that perform the required tasks Objects are instantiated from classes Prewritten classes in hierarchically ordered packages called namespaces

3 What is C# (pronounced C sharp) Object Orientated Programming Language Syntax very similar to Java and C Optimised for.NET features, e.g. –Managed byte code (safe, tidy and portable) –Run by Microsoft’s Common Language Runtime –Conforms to.NET’s Common Type System –Drag and drop interface and auto code generation

4 Variables and Arrays Declaring variables in C# int x, y, z; float myVal; double PI; string myString; Declaring arrays in C# float [ ] prime_numbers = new float[10]; double [, ] matrix = new double[3,3];

5 Operators Assignment operator = For example… x=10; PI=3.1414; myString="hello world"; prime_numbers[0]=1.0f; matrix[2,0]=0.0; Arithmetic operators ( + - * / ) For example… z=x+y; Average=(a+b+c)/3; myString=“ The Value is “ + myValue;

6 Decision Making Conditional operators == && || > < For example… if(x==10) y=20; else y=3; if(messageStr == "hello" && happy == true) { replyStr = "hello to you too"; } else { replyStr = "be that way"; }

7 Loops // loop while i less than 10 while(i < 10) { sum = sum + prime_numbers[i]; i = i + 1; } //similar using a ‘for loop’ for(i = 0; i<10;i=i++) { val = prime_numbers[i]; sum = sum + val; }

8 Methods All methods will be part of a class Can pass values or references… // defining function float MyAdd(float a, float b, ref float sum) { float product; sum=a+b; product=a*b; return product; } //calling function myProduct=MyAdd(1,2,ref mySum);

9 Classes and Objects Class is a template from which objects can be instantiated

10 Using a class // head file with namespace containing class definition using MyClasses.MyMathsClasses; // instantiate an object using class MyCalculator calcObj= new MyCalculator(); // access methods via object myVal=calcObj.Add(1,2); Note some classes can be used directly e.g. myVal=Math.Sin(angle);

11 Handling Events Objects often generate events –e.g. When a button pressed –Event handler method delegated to handle event –Happily much of this code can be auto-generated //delegates method this.button1.Click += new System.EventHandler(this.button1_Click); //method handles the event private void button1_Click(object sender,System.EventArgs e) { // your code here to handle event }

12 Handling Exceptions Certain methods may generate exceptions (i.e. events caused by errors) Use try and catch to deal with exceptions try { myInputValue=System.Convert.ToDouble(myString); } catch { // code to deal with exception return; }

13 2.6 Creating your first C# application File->New->Project and select ‘Windows Application’

14 Key areas of IDE  Design Area – where you will create the user interface for your application and write your source code. Solution Explorer – for viewing constituent files and references that make up your project. Properties Window – can assign values to some objects Toolbox – where you can select from a variety of objects to add to your application. Output Pane – where the IDE sends messages to you (e.g. compile time errors, etc) Index & Search Tags – for viewing and searching for appropriate documentation

15 Using IntelliSense Can drag and drop some objects onto design area Auto generates code to instantiate objects Can change objects parameters via properties window Can auto generate event handler by double clicking on appropriate object

16 Task Follow worked example for creating windows application using C# &.NET Complete EXERCISE 2.1 –(miles to kilometres) Complete EXERCIRE 2.2 –(earthquake)

17 Consuming A Web Service Refer to Web Service Description Language file, e.g. http://live.capescience.com/wsdl/AirportWeather.wsdl Via Project->Add Web Reference

18 Hence a proxy class created You can use namespace using ConsumeWebService.com.capescience.live; Hence instantiate objects for class AirportWeather myObject = new AirportWeather(); To access remote data and functionality label1.Text=myObject.getTemperature(textBox1.Text);

19 Task Complete EXERCISE 2.3 Consume weather service Complete EXERCISE 2.4 Consume other remote XML web services


Download ppt "Web Services Week 2 Aims: Getting started with creating simple C# applications within Visual Studio.NET Objectives: –An introduction to the syntax of C#.NET."

Similar presentations


Ads by Google