Introduction to the C# Programming Language for the VB Programmer.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Introduction to C Programming
ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Programming Languages and Paradigms The C Programming Language.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Lecture Set 4 Data Types and Variables Part B – Variables, Constants, Expressions Conversion Rules Options Strict, Option Explicit Scope of Definition.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Objectives Using functions to organize PHP code
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
Introduction to the C# Programming Language for the VB Programmer.
Chapter 4 Functions and Control Structures PHP Programming with MySQL.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Chapter 2 - Java Programming Fundamentals1 Chapter 2 Java Programming Fundamentals.
VB .NET Programming Fundamentals
Introduction to Programming G51PRG University of Nottingham Revision 2 Essam Eliwa.
Programming Concepts MIT - AITI. Variables l A variable is a name associated with a piece of data l Variables allow you to store and manipulate data in.
5.05 Apply Looping Structures
VBA & Excel Barry L. Nelson IEMS 465 Fall Quarter 2003.
COMPUTER PROGRAMMING II SUMMER 2011 Visual Basic to C#
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
CIS Computer Programming Logic
Fundamentals of C and C++ Programming Control Structures and Functions.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
Language Elements 1. Data Types 2 Floating Point (real) Single Precision Double Precision Decimal Fixed Point (integer) Byte Short Integer Long Numerical.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Introduction to Exception Handling and Defensive Programming.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
Visual Basic IITG to be expanded. What is Visual Basic? Object Oriented Programming Language (OOP) Graphical User Interface (GUI) Event Driven – Write.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 2 Functions and Control Structures PHP Programming with MySQL 2 nd Edition.
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
Chapter 7 Additional Control Structures. Chapter 7 Topics l Switch Statement for Multi-Way Branching l Do-While Statement for Looping l For Statement.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
COMPUTER PROGRAMMING II SUMMER 2011 Visual Basic to C#
Applications Development
JavaScript, Fourth Edition
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Visual Basic Programming I 56:150 Information System Design.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
CompSci 100E JB1.1 Java Basics (ala Goodrich & Tamassia)  Everything is in a class  A minimal program: public class Hello { public static void main(String[]
Java Language Basics.
Java Primer 1: Types, Classes and Operators
JavaScript Syntax and Semantics
2. Understanding VB Variables
Chapter 8: Control Structures
Lecture Set 4 Data Types and Variables
Chapter 3: Understanding C# Language Fundamentals
11/10/2018.
Arrays, For loop While loop Do while loop
OPERATORS (2) CSC 111.
Module 2: Understanding C# Language Fundamentals
C Programming Getting started Variables Basic C operators Conditionals
Problem 1 Given n, calculate 2n
Presentation transcript:

Introduction to the C# Programming Language for the VB Programmer

Slide 2 Lecture Overview Some key terms Introduce the C# programming language for the VB developer Mention some important features of the Visual Studio.NET environment Pass along some editing tips and tricks

Slide 3 Key Terms (1) Constructor – A special method that is called when an object is created from its class Class – A template from which objects are created Inheritance – The concept that one class can be created based on another class Method – An action that the object can perform. Property – The data stored in object. The Text property of a text box, for example

Slide 4 Key Terms (2) Object – A class instance. Once an object has been created from its class, it’s possible to call methods and read and write properties Objects are created when a class’ constructor is called

Slide 5 Project Structure Everything is the same as VB Solution file, project file, program files Modules have a file suffix of.cs instead of.vb

Slide 6 THE BIG DIFFERENCE C# IS CASE SENSITIVE

Slide 7 Statements In C#, all statements end with a semicolon ; Statements can span multiple lines There is no need for a continuation character as in VB Example C# System.Console.WriteLine( “This is a line of text”); Example VB System.Console.WriteLine( _ “This is a line of text”);

Slide 8 Comments XMLSummary comments begin with /// Single line comments are marked with // Multi-line comments start with /* and end with */ // A comment. /* This is Also a comment */

Slide 9 Variable Declarations Variable declarations work the same way in both C# and VB Local variables are declared inside of a procedure Module-level variables are declared outside of a procedure In C#, the declaration syntax is reversed from VB

Slide 10 Data Types C#VB intInteger boolBoolean stringString doubleDouble floatSingle dateDateTime

Slide 11 Variable Declaration (Examples) VB Dim Count As Integer Dim MyName As String = “Ekedahl” C# int count; string MyName = “Ekedahl”;

Slide 12 Variable Declarations (Examples) VB syntax to declare a module-level variable Private Counter As Integer C# syntax to declare the same variable private int Counter;

Slide 13 Strings Use the System.String data type as in VB; The C# string type maps to System.String the string concatenation operator is + instead of & The members are the same between C# and VB

Slide 14 Type Conversion (Introduction) In IS 350, you used val to convert strings to numbers In C#, val is used to declare implicitly typed variables We will use a much different strategy here Each primary data type supports a method named TryParse The method accepts 2 arguments The string to parse The output result

Slide 15 TryParse (Example) Try to parse the string arg and store the result in out string arg = "123"; double result; if (System.Double.TryParse(arg, out result) == true) { return true; } return false;

Slide 16 Using System.Convert Members of System.Convert class also convert one type to another System.Convert.ToInt32 System.Convert.ToDouble System.Convert.ToDateTime …

Slide 17 Constants Constants are just variables whose value does not ever change Declare with the const statement Constants can only be initialized when they are declared

Slide 18 Constants (Example) Declare and initialize constants const int x = 0; public const double gravitationalConstant = 6.673e-11; private const string productName = "Visual C#";

Slide 19 Scope Access modifiers control the scope of a procedure or variable The keywords are the same between VB and C# private – scope is the class containing the procedure or variable public – scope is global

Slide 20 C# Blocks In VB, blocks are marked with keywords Sub – End Sub If – End If Do Loop While End While In C#, blocks are all marked with {} as in Java or C++

Slide 21 C# Blocks (Example) namespace Validate { public static class ValidateNumbers { public static bool IsInteger( string arg) { }

Slide 22 Procedures Visual Basic has Function and Sub procedures C# works a bit differently Procedures that don’t return a value have a data type of void Procedures that do return a value have an explicitly defined data type

Slide 23 Procedures (Example 1) The following procedure does not return a value private void InitializeLocal() { // Statements }

Slide 24 Procedures (Example 2) The following procedure returns a value having a data type of bool (Boolean) public static bool IsInteger(string arg, out int result) { if (System.Int32.TryParse(arg, out result) == true) { return true; } return false; }

Slide 25 Calling Procedures To call a procedure, use it’s name If the argument list is empty, the () are required Call the procedure foo without arguments foo();

Slide 26 Operators (Arithmetic) Mathematical operators are the same for both VB and C# with a few exceptions % is the modulus operator ( Mod ) ++ and -- are post and pre increment and decrement operators Increment Count (Example) Count++;

Slide 27 Operators (Logical) They are pretty much the same from VB and C# Inequality ( <> ) in VB is ( != ) in C# Equality ( = ) in VB is ( == ) in C# In C#, the single ( = ) is always used for assignment statements

Slide 28 Relational Operators These are quite different VBC# And& AndAlso&& Or| OrElse|| Xor^ Not!

Slide 29 Decision-Making Statements ( if ) if statements take a boolean expression as an argument Note the parentheses are required if (i >= 0) { // do something if i is // greater than 0 }

Slide 30 Decision-Making Statements ( 2-way if ) Use the else keyword to create a 2-way if statement if ( i >= 0) { // Do something if i is // greater than 0. } else { // Do something else. }

Slide 31 Decision-Making Statements ( multi-way if ) Use else if to create multi-way decisions if (i > 0) { // Do something if i is // greater than 0. } else if (i < 0) { // Do something else. } else { // i must be equal to 0. }

Slide 32 Decision-Making Statements ( switch ) C# uses the switch statement instead of Select Case Both work the same way The break keyword must appear at the end of each case

Slide 33 switch statement (Example) switch (day) { case 0: DayOfWeek = “Sunday”; break; case 1: DayOfWeek = “Monday”; break; }

Slide 34 Loops (Introduction) while is used to create a pre-test loop ( Do While ) do is used to create a post-test loop ( Do Until ) for loops are used when the iteration count is known in advance

Slide 35 while Loops While loops take a boolean expression enclosed in parenthesis The loop executes while the condition is true {} mark the while block instead of End While

Slide 36 while Loops (Example) int i; while (i < 10) { System.Console.WriteLine(i); i++; }

Slide 37 do Loops Do loops test the condition after the loop has executed once Example: do { System.Console.WriteLine(x); x++; // Post increment operator } while (x < 5);

Slide 38 for Loops Like VB, for loops can be used when the number of loop iterations is known in advance The syntax is quite different though

Slide 39 for Loops (Syntax) for ( init-expr; cond-expr; loop-expr ) { // statement block } int-expr contains the expression’s initial value cond-expr contains the condition loop-expr updates the expression

Slide 40 for Loops (Example) Print the counting numbers from 1 to 10 for (int i = 0; i < 10; i++) { System.Console.WriteLine(i); }

Slide 41 foreach loops foreach loops are used to enumerate arrays and collections We will talk about collections more later When using a foreach loop you need not explicitly increment or decrement the counter

Slide 42 foreach loops (Example) Declare and enumerate a one-dimensional array named fibarray int[] fibarray = new int[] { 0, 1, 2, 3, 5, 8, 13 }; foreach (int i in fibarray) { System.Console.WriteLine(i); }

Slide 43 Exiting a Loop Prematurely Use the break statement to exit a loop Use the continue statement to jump to the loop’s condition The condition is tested immediately There is a goto statement to jump to a named label but we will NEVER use it

Slide 44 Importing and Using Namespaces By default, you must fully qualify class and method names System.IO.StreamReader for example In VB, the Imports statement allows unqualified references Imports System.IO In C#, it’s the using statement using System.IO;