Download as pdf or txt
Download as pdf or txt
You are on page 1of 49

Visual Programming

Department of CSE, QUEST


Dr. Irfana Memon
Dr. Irfana Memon
Department of CSE, QUEST

https://sites.google.com/a/quest.edu.pk/dr-irfana-memon/lecture-slides
Course Content
Review of C# Syntax: Overview of Writing Applications using C#, Data types,
Operators, and Expressions
C# Programming Language Constructs, Creating Methods
Invoking Methods, Handling Exceptions, Creating overloaded Methods

Developing the Code for a Graphical Application: Implementing Structs and Enums
Implementing Type-safe Collections: Creating Classes, Organizing Data into Collections,

Department of CSE, QUEST


Dr. Irfana Memon
Handling Events, Defining and Implementing Interfaces
Creating a Class Hierarchy by Using Inheritance, Extending .NET Framework Classes,
Creating Generic Types
Accessing a Database: Creating and Using Entity Data Models, Querying and Updating Data
by Using LINQ
Designing the User Interface for a Graphical Application: Using XAML, Binding Controls to
Data, Styling a User Interface
Improving Application Performance and Responsiveness: Implementing Multitasking by
using Tasks and Lambda Expressions
2
Performing Operations Asynchronously, Synchronizing Concurrent Access to Data
Review of C# Syntax:
Overview of Writing
Applications using C#,

Department of CSE, QUEST


Dr. Irfana Memon
Data types, Operators,
and Expressions
3
Visual Programming

• A visual programming language (VPL) is a programming


language that uses graphical elements and figures to
develop a program.

Department of CSE, QUEST


Dr. Irfana Memon
• A VPL employs techniques to design a software program
in two or more dimensions, and includes graphical
elements, text, symbols and icons within its
programming context.

• A visual programming language is also known as an


executable graphics language. 4
Why C# is important?
• To become professional software developer
• Software industry work on software products
• Every software industry are making their products in 3
categories:

Department of CSE, QUEST


Dr. Irfana Memon
1. Applications : are of two types , Desktop application
(e.g., adobe photoshop) and mobile application
(e.g., whatsapp)

1. Server side : web applications (ASP.net tool)

2. Games (candy crush ) using C# and unity tool can design


5
game
INTRODUCTION
• C# is a modern, general-purpose, object-oriented
programming language developed by Microsoft and
approved by European Computer Manufacturers
Association (ECMA) and International Standards
Organization (ISO).

Department of CSE, QUEST


Dr. Irfana Memon
• C# was developed by Anders Hejlsberg and his team
during the development of .Net Framework.

• C# is designed for Common Language Infrastructure (CLI),


which consists of the executable code and runtime
environment that allows use of various high-level
6
languages on different computer platforms and
architectures.
INTRODUCTION
• The following reasons make C# a widely used professional
language:

 It is a modern, general-purpose programming language


 It is object oriented.

Department of CSE, QUEST


Dr. Irfana Memon
 It is component oriented.
 It is easy to learn.
 It is a structured language.
 It produces efficient programs.
 It can be compiled on a variety of computer platforms.
 It is a part of .Net Framework.

7
C# Advantages and
Disadvantages
Advantages of C#
Object-Oriented Language
Automatic Garbage Collection
Cross Platform
Backward Compatibility

Department of CSE, QUEST


Dr. Irfana Memon
Better Integrity and Interoperability

Applications of C#
Games using Unity
Web Applications Client-Server Applications
Windows Applications that run on desktops
Web Services Applications 8
Console Applications
Class Libraries
Characteristics of C#
• C# has strong resemblance with Java, it has numerous
strong programming features that make it endearing to a
number of programmers worldwide.
• Following is the list of few important features of C#
 Boolean Conditions
 Automatic Garbage Collection
 Standard Library

Department of CSE, QUEST


Dr. Irfana Memon
 Assembly Versioning
 Properties and Events
 Delegates and Events Management
 Easy-to-use Generics
 Indexers
 Conditional Compilation
 Simple Multithreading 9
 LINQ and Lambda Expressions
 Integration with Windows
C# Environment
• The tools required for creating C# programming.

• Already mentioned that C# is part of .Net framework and is


used for writing .Net applications.

Department of CSE, QUEST


Dr. Irfana Memon
• Before discussing the available tools for running a C#
program, let us understand how C# relates to the .Net
framework.

10
.NET Framework
• The .NET framework is a revolutionary platform that helps
you to write the following types of applications
 Windows applications
 Web applications
 Web services
• The .NET framework applications are multi-platform
applications.

Department of CSE, QUEST


Dr. Irfana Memon
• The framework has been designed in such a way that it can
be used from any of the following languages:
 C#
 C++
 Visual Basic
 Jscript
 COBOL, etc. 11
• All these languages can access the framework as well as
communicate with each other.
.NET Framework
• The .NET framework consists of an
enormous library of codes used by the
client languages such as C#.
• Following are some of the components of
the .NET framework
 Common Language Runtime (CLR)
 The .NET Framework Class Library
 Common Language Specification
 Common Type System

Department of CSE, QUEST


Dr. Irfana Memon
 Metadata and Assemblies
 Windows Forms
 ASP.NET and ASP.NET AJAX
 ADO.NET
 Windows Workflow Foundation (WF)
 Windows Presentation Foundation
 Windows Communication Foundation
(WCF)
 LINQ (Language Integrated Query)
data querying methodology which 12
provides querying capabilities to .NET
languages with a syntax similar to a
SQL query.
IDE for C#
• Microsoft provides the following development tools for C#
programming
 Visual Studio.NET
 Visual Studio 2019 (VS)
 Visual C# 2019 Express (VCE)
 Visual Web Developer
• The last two are freely available from Microsoft official website.

Department of CSE, QUEST


Dr. Irfana Memon
• Using these tools, you can write all kinds of C# programs from
simple command-line applications to more complex applications.
• You can also write C# source code files using a basic text editor,
like Notepad, and compile the code into assemblies using the
command-line compiler, which is again a part of the .NET
Framework.
• Visual C# Express and Visual Web Developer Express edition are
13
trimmed down versions of Visual Studio and has the same
appearance.
• They retain most features of Visual Studio.
The Simple Stuff
• Most of C# is pretty similar to languages you are used
to:
• Declarations
• Expressions

Department of CSE, QUEST


Dr. Irfana Memon
• Assignment and control statements
• Other elements are:
• Classes
• Functions
• Polymorphism
14
studio
Download and Install Visual

Dr. Irfana Memon


15

Department of CSE, QUEST


Visual Studio
1. Download Visual studio.
2. Click on Microsoft visual studio 2010 setup. You will see
Figure 1. Click install Microsoft visual studio 2010.

Department of CSE, QUEST


Dr. Irfana Memon
16

Figure 1
Visual Studio
• The next window you will see Figure 2 is the welcome
window. This figure shows that I am installing.

Department of CSE, QUEST


Dr. Irfana Memon
17

Figure 2. Setup welcome Window


Visual Studio
• In Figure 3, you will see what components will be installed.

Department of CSE, QUEST


Dr. Irfana Memon
18

Figure 3. Setup licensing Window


Visual Studio
• In Figure 4, Lets you choose full/custom installation. If you
click custom option, you will be able to choose precisely with
component should be installed.

Department of CSE, QUEST


Dr. Irfana Memon
19

Figure 4. Setup customization Window


Visual Studio
• In Figure 5, during the installation process where the small
check mark indicate which VS component have successfully
installed.

Department of CSE, QUEST


Dr. Irfana Memon
20

Figure 5. Setup progress Window


Visual Studio
• During the installation, the VS installer will need to reboot
your computer, showing the restart message in Figure 6.
Make sure you close any application you currently have open,
so that you can not lose your work.

Department of CSE, QUEST


Dr. Irfana Memon
Figure 6. Setup Restart Window
21
Visual Studio
• In Figure 7, You will see the success window, when installation
complete without any error.

Department of CSE, QUEST


Dr. Irfana Memon
22

Figure 7. Setup Success Window


Visual Studio
• You are now ready to run VS for the first time. At that time you
will need to perform one more easy configuration step, where
you will choose default environment setting as shown in
Figure 8.

Department of CSE, QUEST


Dr. Irfana Memon
23
Figure 8. Checking for service release
Visual Studio
• In Figure 9, We choose Visual C#. The choice selection
depends, what language/environment you will use to write
software in.

Department of CSE, QUEST


Dr. Irfana Memon
24

Figure 9. Default Environment setting Window


Visual Studio
• Figure 10 shows how VS appears when first started.

Department of CSE, QUEST


Dr. Irfana Memon
25

Figure 10. Visual studio 2010 start screen


1st Simple C# Program
1. Start application “Microsoft Visual Studio 2010”
2. In the “start page” of Microsoft Visual Studio .NET, choose new
project
3. In “New Project” window, under “Project Types” choose
“Visual C# Projects”, under “Templates”

Department of CSE, QUEST


Dr. Irfana Memon
A. Choose “Console Application” – to develop command line
programs
(OR) choose “Windows Application” – to develop GUI
components by drag and drop

For 1st simple C# program, we will choose Console


Application 26
1st Simple C# Program
4. Now, under “Name”, call it “FirstProgram”, under location
a. You can choose default location given
b. Or choose desktop or folder of your choice.

5. At this point the following class appears as in Figure.

Department of CSE, QUEST


Dr. Irfana Memon
27
Visual Environment
• The .NET environment adds some code pertaining to
namespace, which is not necessary for the 1st and simple
program. Therefore, let’s modify it so that you are aware of
the essentials of a basic C# program. See Figure given below.

Department of CSE, QUEST


Dr. Irfana Memon
28
Visual Environment
6. You can rename the class name, to anything you which, by
right clicking on “Class1.cs”, in the “Solution Explorer” frame
and clicking on “rename”. When you do this you must also
delete “Class1” in the code view to whatever you called the
class. See Figure.

Department of CSE, QUEST


Dr. Irfana Memon
29
Visual Environment
7. Now add the following code in the “Main” method
Console.WriteLine(“This is my first C# program!”);
At this point, the class should look like as it does in Figure.

Department of CSE, QUEST


Dr. Irfana Memon
30
Visual Environment
8. Now, to compile the program,
a. Select the “Build” menu item from the top menu
b. And select “Build Solution” from the drop down.
At the bottom of the window, the message “Build succeeded”
should appear, informing of successful compilation.

Department of CSE, QUEST


Dr. Irfana Memon
9. Now, to run the program,
a.Select “Debug” menu item from the top menu
b. And select “Start Without Debugging” from the drop
down.
At this point, a new window should appear as in Figure on next
slide.
31
Visual Environment

Dr. Irfana Memon


32

Department of CSE, QUEST


Creating Hello World Program

Before we study basic building blocks of the C# programming


language, let us look at a bare minimum C# program structure
so that we can take it as a reference in upcoming chapters.

Department of CSE, QUEST


Dr. Irfana Memon
33
Creating Hello World Program

• Creating Hello World Program


• A C# program consists of the following parts
 Namespace declaration
 A class

Department of CSE, QUEST


Dr. Irfana Memon
 Class methods
 Class attributes
 A Main method
 Statements and Expressions
 Comments
• Let us look at a simple code that prints the words "Hello
World" 34
First C# Program
using System;
namespace HelloWorldApplication
{
class HelloWorld

Department of CSE, QUEST


Dr. Irfana Memon
{
static void Main(string[] args)
{
/* my first program in C# */
Console.WriteLine("Hello World");
Console.ReadKey();
}
35
}
}
First C# Program
using System;
namespace HelloWorldApplication
{
class HelloWorld
{
static void Main(string[] args)
{
/* my first program in C# */
Console.WriteLine("Hello World");
Console.ReadKey();

Department of CSE, QUEST


Dr. Irfana Memon
}
}
}

• The first line of the program using System;


• the using keyword is used to include the System namespace
in the program.
• A program generally has multiple using statements. 36
First C# Program
using System;
namespace HelloWorldApplication
{
class HelloWorld
{
static void Main(string[] args)
{
/* my first program in C# */
Console.WriteLine("Hello World");
Console.ReadKey();

Department of CSE, QUEST


Dr. Irfana Memon
}
}
}

• The next line has the namespace declaration.


• A namespace is a collection of classes.
• The HelloWorldApplication namespace contains the 37
class HelloWorld.
First C# Program
using System;
namespace HelloWorldApplication
{
class HelloWorld
{
static void Main(string[] args)
{
/* my first program in C# */
Console.WriteLine("Hello World");
Console.ReadKey();

Department of CSE, QUEST


Dr. Irfana Memon
}
}
}

• The next line has a class declaration, the


class HelloWorld contains the data and method definitions
that your program uses. Classes generally contain multiple 38
methods. Methods define the behavior of the class. However,
the HelloWorld class has only one method Main.
First C# Program
using System;
namespace HelloWorldApplication
{
class HelloWorld
{
static void Main(string[] args)
{
/* my first program in C# */
Console.WriteLine("Hello World");

Department of CSE, QUEST


Dr. Irfana Memon
Console.ReadKey();
}
}
}

• The next line defines the Main method, which is the entry
point for all C# programs. The Main method states what the
class does when executed. 39
First C# Program using System;
namespace HelloWorldApplication
static: This keyword is used {
for invoking any method class HelloWorld
{
without creating an instance
static void Main(string[] args)
of class. Static is generally {
used for defining things at /* my first program in C# */
class level and don’t need any Console.WriteLine("Hello World");
instance of class for accessing
these. Console.ReadKey();
}

Department of CSE, QUEST


Dr. Irfana Memon
void: This is used for return }
type where method is not }
returning any data after its
execution.

Main(String[] args) :- The Main method can be declared with or without


a string[] args parameter that contains command-line arguments.
String[] args: This is used for accessing any parameter which is pass to method as
input from command line.
40
First C# Program
using System;
namespace HelloWorldApplication
{
class HelloWorld
{
static void Main(string[] args)
{
/* my first program in C# */
Console.WriteLine("Hello World");
Console.ReadKey();

Department of CSE, QUEST


Dr. Irfana Memon
}
}
}

• The next line /*...*/ is ignored by the compiler and it is put to


add comments in the program.
41
First C# Program
using System;
namespace HelloWorldApplication
{
class HelloWorld
{
static void Main(string[] args)
{
/* my first program in C# */
Console.WriteLine("Hello World");
Console.ReadKey();

Department of CSE, QUEST


Dr. Irfana Memon
}
}
}

The Main method specifies its behavior with the


statement Console.WriteLine("Hello World");
WriteLine is a method of the Console class defined in 42
the System namespace. This statement causes the message
"Hello, World!" to be displayed on the screen.
First C# Program
using System;
namespace HelloWorldApplication
{
class HelloWorld
{
static void Main(string[] args)
{
/* my first program in C# */
Console.WriteLine("Hello World");
Console.ReadKey();

Department of CSE, QUEST


Dr. Irfana Memon
}
}
}

The last line Console.ReadKey(); is for the VS.NET Users. This


makes the program wait for a key press and it prevents the
screen from running and closing quickly when the program is 43
launched from Visual Studio .NET.
Important points for C# Program

It is worth to note the following points:


• C# is case sensitive.
• All statements and expression must end with a
semicolon (;).
• The program execution starts at the Main method.

Department of CSE, QUEST


Dr. Irfana Memon
• Program file name could be different from the class
name.

44
Compiling and Execution of C#
Program
If you are using Visual Studio.Net for compiling and executing
C# programs, take the following steps:
• Start Visual Studio.
• On the menu bar, choose File -> New -> Project.

Department of CSE, QUEST


Dr. Irfana Memon
• Choose Visual C# from templates, and then choose Windows.
• Choose Console Application.
• Specify a name for your project and click OK button.
• This creates a new project in Solution Explorer.
• Write code in the Code Editor.
• Click the Run button or press F5 key to execute the
project.
45
• A Command Prompt window appears that contains the
line Hello World.
Compiling and Execution of C#
Program
You can compile a C# program by using the command-line
instead of the Visual Studio IDE:
• Open a text editor and add the above-mentioned code.
• Save the file as helloworld.cs

Department of CSE, QUEST


Dr. Irfana Memon
• Open the command prompt tool and go to the directory
where you saved the file.
• Type csc helloworld.cs and press enter to compile your
code.
• If there are no errors in your code, the command prompt
takes you to the next line and
generates helloworld.exe executable file.
46
• Type helloworld to execute your program.
• You can see the output Hello World printed on the screen.
C# console reading values
We can use the Console class to read values as well.
Program.cs
using System;
namespace ReadLine
{
class Program
{
static void Main(string[] args)

Department of CSE, QUEST


Dr. Irfana Memon
{
Console.Write("Enter your name: ");
string name = Console.ReadLine();
Console.WriteLine("Hello {0}", name);
}
}
}
string name = Console.ReadLine();
We read a line from the terminal. When we hit the Enter key, the 47
input is assigned to the name variable. The input is stored into
the name variable, which is declared to be of type string.
C# console reading values
We can use the Console class to read values as well.
Program.cs
using System;
namespace ReadLine
{
class Program
{
static void Main(string[] args)

Department of CSE, QUEST


Dr. Irfana Memon
{
Console.Write("Enter your name: ");
string name = Console.ReadLine();
Console.WriteLine("Hello {0}", name);
} The {0} specifier is replaced with the value of
} the name variable.
}
string name = Console.ReadLine();
We read a line from the terminal. When we hit the Enter key, the 48
input is assigned to the name variable. The input is stored into
the name variable, which is declared to be of type string.
Wish You Good Luck

Dr. Irfana Memon


49

Department of CSE, QUEST

You might also like