C# Introduction ISYS 350.

Slides:



Advertisements
Similar presentations
CHAPTER TWO Creating Simple Visual Basic.NET Windows Applications.
Advertisements

ISYS 573 Special Topic – VB.Net David Chao. The History of VB Early 1960s:BASIC-Beginner’s All-Purpose Symbolic Instruction Code –Teaching –Simple syntax,
.NET Framework.NET Framework class libraries: A large set of classes that forms the basis for objects that can be used programmatically. –Programming in.
VB.Net Introduction. .NET Framework.NET Framework class libraries: A large set of classes that forms the basis for objects that can be used programmatically.
C# Introduction ISYS 512. Major Differences Between VB Project and C# Project The execution starts from the Main method which is found in the Program.cs.
VB.Net Introduction. .NET Framework.NET Framework class libraries: A large set of classes that forms the basis for objects that can be used programmatically.
VB.Net Introduction. .NET Framework.NET Framework class libraries: A large set of classes that forms the basis for objects that can be used programmatically.
VB.Net Introduction. Visual Studio 2010 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# or VB –Windows.
Chapter 4 Introduction to Numeric Data Types and Variables.
C# Introduction ISYS 350. Visual Studio 2012 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.
CSCI 3328 Object Oriented Programming in C# Chapter 2: Introduction to Visual C# Programming 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,
© 2006 Lawrenceville Press Slide 1 Chapter 3 Visual Basic Interface.
CS0004: Introduction to Programming Variables – Strings.
Visual Basic.NET BASICS Lesson 4 Mathematical Operators.
C# Introduction ISYS 512. Visual Studio 2013 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.
VB.Net Introduction. What is programming? Writing computer programs means writing instructions in a logical sequence, that will make the computer follow.
Microsoft Visual Basic 2005 CHAPTER 4 Variables and Arithmetic Operations.
C# Introduction ISYS 350. Visual Studio 2013 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.
CHAPTER TWO Creating Simple Visual Basic.NET Windows Applications.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CS0004: Introduction to Programming Project 1 – Lessons Learned.
Applications Development
ECA 225 Applied Interactive Programming1 ECA 225 Applied Online Programming basics.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project.
C# Introduction ISYS 350. Visual Studio 2012 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.
VB.Net Introduction. Visual Studio 2012 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# /VB/Other language.
Introduction to Programming Python Lab 3: Arithmetic 22 January PythonLab3 lecture slides.ppt Ping Brennan
COMPUTER PROGRAMMING I 3.01 Apply Controls Associated With Visual Studio Form.
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Visual Basic.NET Programming for the Rest of Us Keith Mulbery Utah Valley State College.
COMPUTER PROGRAMMING I 3.01 Apply Controls Associated With Visual Studio Form.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
Visual Basic/ Visual Studio Brandon Large. Connecting to prior knowledge In your notes write down what the two main parts of the computer are. The “software”
Microsoft Visual Basic 2010 CHAPTER FOUR Variables and Arithmetic Operations.
C# Introduction ISYS 350.
IS 350 Numeric Data Types.
Visual Basic.NET Windows Programming
A variable is a name for a value stored in memory.
Arithmetic operations and operators, converting data types and formatting programs for output. Year 11 Information Technology.
Variables and Arithmetic Operations
3.01 Apply Controls Associated With Visual Studio Form
Variables, Expressions, and IO
C# Introduction ISYS 350.
Introduction to Scripting
3.01 Apply Controls Associated With Visual Studio Form
Java Programming: From Problem Analysis to Program Design, 4e
Lecture Set 4 Data Types and Variables
C# Introduction ISYS 350.
Data Types and Variables Part D – Common Methods (ToString and Parse)
C# Introduction ISYS 350.
Variables and Arithmetic Operations
C# Introduction ISYS 350.
Working with DateTime Data
C# Introduction ISYS 350.
Advanced Programming Lecture 02: Introduction to C# Apps
The University of Texas – Pan American
WEB PROGRAMMING JavaScript.
C# Introduction ISYS 350.
Chapter 2: Basic Elements of Java
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
CIS16 Application Development Programming with Visual Basic
C# Introduction ISYS 350.
VB.Net Introduction.
Unit 3: Variables in Java
C# Introduction ISYS 350.
Overview of the IDE Visual Studio .NET is Microsoft’s Integrated Development Environment (IDE) for creating, running and debugging programs (also.
Presentation transcript:

C# Introduction ISYS 350

Visual Studio 2013 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ C# Windows Windows form application Project name/Project folder Project windows: Form design view/Form code view Solution Explorer View/Solution Explorer ToolBox Property Window Properties and Events Server Explorer Project/Add New Item Property window example

Introduction to C# Event-driven programming The interface for a C# program consists of one or more forms, containing one or more controls (screen objects). Form and controls have events that can respond to. Typical events include clicking a mouse button, type a character on the keyboard, changing a value, etc. Event procedure

Form Properties: Events: Name, FormBorderStyle, Text, BackColor, BackImage, Opacity Events: Load, FormClosing, FormClosed GotFocus, LostFocus MouseHover, Click, DoubleCLick

Common Controls TextBox Label Button CheckBox RadioButton ListBox ComboBox PictureBox

Text Box Properties: BorderStyle, CauseValidation, Enabled, Locked, Multiline, PasswordChar, ReadOnly, ScrollBar, TabIndex, Text, Visible, WordWrap, etc. Properties can be set at the design time or at the run time using code. To refer to a property: ControlName.PropertyName Ex. TextBox1.Text Note: The Text property is a string data type and automatically inherits the properties and methods of the string data type.

Typical C# Programming Tasks Creating the GUI elements that make up the application’s user interface. Visualize the application. Make a list of the controls needed. Setting the properties of the GUI elements Writing procedures that respond to events and perform other operations.

To Add an Event-Procedure 1. Select the Properties window 2. Click Events button 3. Select the event and double-click it. Note: Every control has a default event. Form: Load event Button control: Click event Textbox: Text Changed event To add the default event procedure, simply double-click the control.

Demo FirstName LastName .Control properties Show Full Name .Control properties .Event: Click, MouseMove, Form Load, etc. .Event procedures FullName: textBox3.Text textBox3.Text = textBox1.Text + " " + textBox2.Text; Demo: Text alignment (TextBox3.TextAlign=HorizontalAlign.Right) TextBox3.BackColor=Color.Aqua;

Demo Num1 Num2 Compute Sum .Control properties .Event: Click, MouseMove, Form Load, etc. .Event procedures Sum: textBox3.Text = (double.Parse(textBox1.Text) + double.Parse(textBox2.Text)).ToString(); In-Class lab: Show the product of Num1 and Num2.

C# Project The execution starts from the Main method which is found in the Program.cs file. Solution/Program.cs Contain the startup code Example: Application.Run(new Form1());

What does Variable mean? A variable, in the context of programming, is a symbolic name given to an unknown quantity that permits the name to be used independent of the information it represents. Variables are associated with data storage locations, and values of a variable are normally changed during the course of program execution.

Variable Names A variable name identifies a variable Always choose a meaningful name for variables Basic naming conventions are: the first character must be a letter (upper or lowercase) or an underscore (_) the name cannot contain spaces do not use C# keywords or reserved words Variable name is case sensitive

Declare a Variable C# is a strongly typed language. This means that when a variable is defined we have to specify what type of data the variable will hold. DataType VaraibleName; A C# statement ends with “;”

string DataType string Variables: Examples: String concatenation: + string empName; string firstName, lastAddress, fullName; String concatenation: + fullName = firstName + lastName; MessageBox.Show(“Total is “ + 25.75);

Numeric Data Types int, double Examples: double mydouble=12.7, rate=0.07; int Counter = 0;

Inputting and Outputting Numeric Values Input collected from the keyboard are considered combinations of characters (or string literals) even if they look like a number to you A TextBox control reads keyboard input, such as 25.65. However, the TextBox treats it as a string, not a number. In C#, use the following Parse methods to convert string to numeric data types int.Parse double.Parse Examples: int hoursWorked = int.Parse(hoursWorkedTextBox1.Text); double temperature = double.Parse(temperatureTextBox.Text); Note: We can also use the .Net’s Convert class methods: ToDouble, ToInt, ToDecimal: Example: hoursWorked = Convert.ToDouble(textBox1.Text);

Explicit Conversion between Numeric Data Types with Cast Operators C# allows you to explicitly convert among types, which is known as type casting You can use the cast operator which is simply a pair of parentheses with the type keyword in it int iNum1; double dNum1 = 2.5; iNum1 = (int) dNum1; Note: We can also use the .Net’s Convert class methods

Implicit conversion and explicit conversion int iNum1 = 5, iNum2 = 10; double dNum1 = 2.5, dNum2 = 7.0; dNum1 = iNum1 + iNum2; /*C# implicitly convert integer to double*/ iNum1 = (int) dNum1 * 2; /*from doulbe to integer requires cast operator*/

Performing Calculations Basic calculations such as arithmetic calculation can be performed by math operators Operator Name of the operator Description + Addition Adds two numbers - Subtraction Subtracts one number from another * Multiplication Multiplies one number by another / Division Divides one number by another and gives the quotient % Modulus Divides one number by another and gives the remainder Other calculations: Use Math class’s methods.

Example int dividend, divisor, quotient, remainder; dividend = int.Parse(textBox1.Text); divisor = int.Parse(textBox2.Text); quotient = dividend / divisor; remainder = dividend % divisor; textBox3.Text = quotient.ToString(); textBox4.Text = remainder.ToString(); Note: The result of an integer divided by an integer is integer. For example, 7/2 is 3, not 3.5.

Lab Exercise Enter length measured in inches in a textbox; then show the equivalent length measured in feet and inches. For example, 27 inches is equivalent to 2 feet and 3 inches.

Change Machine to Return Smallest Number of Coins int changes, quarters, dimes, nickles, pennies; changes = int.Parse(textBox1.Text); quarters = changes / 25; dimes = (changes % 25) / 10; nickles = (changes - quarters * 25 - dimes * 10) / 5; pennies = changes - quarters * 25 - dimes * 10 - nickles * 5; textBox2.Text = quarters.ToString(); textBox3.Text = dimes.ToString(); textBox4.Text = nickles.ToString(); textBox5.Text = pennies.ToString();

FV = PV * (1 +Rate) Year double pv, rate, years, fv; pv = double.Parse(textBox1.Text); rate = double.Parse(textBox2.Text); years = double.Parse(textBox3.Text); fv = pv*Math.Pow(1 + rate, years); textBox4.Text = fv.ToString();

Formatting Numbers with the ToString Method The ToString method can optionally format a number to appear in a specific way The following table lists the “format strings” and how they work with sample outputs Format String Description Number ToString() Result “N” or “n” Number format 12.3 ToString(“n3”) 12.300 “F” or “f” Fixed-point scientific format 123456.0 ToString("f2") 123456.00 “E” or “e” Exponential scientific format ToString("e3") 1.235e+005 “C” or “c” Currency format -1234567.8 ToString("C") ($1,234,567.80) “P” or “p” Percentage format .234 ToString("P") 23.40%

Working with DateTime Data Declare DateTime variable: Example: DateTime mydate; Convert date entered in a textbox to DateTime data: Use Convert: mydate = Convert.ToDateTime(textBox1.Text); Use DateTime class Parse method: mydate = DateTime.Parse(textBox1.Text); DateTime variable’s properties and methods: MinValue, MaxValue

DateTime Example DateTime myDate; myDate = DateTime.Parse(textBox1.Text); MessageBox.Show(myDate.ToLongDateString()); MessageBox.Show(DateTime.MinValue.ToShortDateString()); MessageBox.Show(DateTime.MaxValue.ToShortDateString());

How to calculate the number of days between two dates? TimeSpan class: TimeSpan represents a length of time. Define a TimeSpan variable: TimeSpan ts; We may use a TimeSpan class variable to represent the length between two dates: ts = laterDate-earlierDate;

Code Example DateTime earlierDate, laterDate; double daysBetween; TimeSpan ts; earlierDate = DateTime.Parse(textBox1.Text); laterDate = DateTime.Parse(textBox2.Text); ts = laterDate-earlierDate; daysBetween = ts.Days; MessageBox.Show("There are " + daysBetween.ToString() + " days between " + earlierDate.ToShortDateString() + " and " + laterDate.ToShortDateString()); Note: Pay attention to how we create the output string.

Comments Line comment: // Block comment: /* …… */ // my comment Comment n */