1.56k likes | 1.73k Views
98-372 Microsoft .NET Fundamentals. 1.1 Understand Basic A pplication Settings. Application Settings. Make it easy to create, store, and maintain custom application and user preferences on the client computer.
E N D
Application Settings • Make it easy to create, store, and maintain custom application and user preferences on the client computer. • Useful for important data that you don’t want to include in the application’s code. Examples include: • Database connection strings • User preferences, such as a color scheme, font sizes, etc. • Individual settings can be “scoped” as Application settings or User settings
.config Files • Generally, Application Settings are stored in .config files: • App.config for Windows Forms, WPF, and other desktop applications • Web.config for ASP.NET web applications • Settings that are specific to individual users (“user-scoped”) are stored in a file named user.config, where ‘user’ is the user name of the person currently running the application.
Lesson Review • List 3 reasons why developers use application settings?
Event-driven Programming • Event-driven programming is a model or paradigm in which the flow of program execution is determined by events. • Often, these events are forms of user input (such as mouse clicks, key presses, or touch-screen interactions). • An event is like a signal to another part of the application. • A simple example is a Save button on a Microsoft Windows form. • When the user clicks the button, that causes a “signal” to be sent to code that saves the current data.
Events • Events are actions that occur in a program that the developer can respond to, or handle, in code. • When an event occurs, we say that it has been “raised.” • In addition to mouse clicks, what are some other events in .NET applications?
Event Handlers • Event handlers are methods or procedures that are invoked when the corresponding event is raised. • In addition to the terms “raised” and “handled,” developers may also use the terms “publish” and “subscribe.” • A class can publish an event, and another class can subscribe to that event.
Delegates • Delegates are objects that refer to methods. • In political diplomacy, a delegate is a person authorized to represent another (a person or an organization), such as when a government sends delegates to represent the country at the Olympics. Likewise, a delegate in this case stands in for, or represents, a method. • .NET applications use delegates to connect events to event handlers.
Lesson Review • Explain the event-driven programming model, including the roles of events and event handlers.
Types of Errors • Syntax errors occur when code does not meet the rules (or “syntax”) of the programming language in use. • For example, using incorrect case (such as ‘If’ in C# code) is a syntax error. • Syntax errors occur at compile time. An application cannot compile—and therefore cannot run—if the code contains syntax errors.
Logic errors occur when code executes but does not behave in the intended manner. Consider the following if statement:if (x < 0 && x > 10)This code follows C# syntax, and it will not crash the application. However, it is not likely to be what the developer intended. • Logic errors are often simply called bugs. • Exceptions occur when something unexpected happens that disrupts the flow of the application.
Exceptions • An exception is an event that is raised when a method encounters a condition that prevents it from executing. • Often, this is due to invalid data. • Exceptions that are not handled will cause the application to terminate
Common types of Exceptions • There are many different types of exceptions. Commonly encountered exceptions include: • A DivideByZeroException • Occurs when there is an attempt to divide a value by zero, as shown in the Anticipatory Set. • A FileNotFoundException • Occurs when an attempt to access a file that does not exist on disk fails. This would happen if the code refers to an incorrect file name or path, or the file does not exist.
Handling Exceptions • Structured exception handling allows the developer to address potential exceptions so that the application doesn’t terminate, or at least it allows for a graceful termination of the application. • Exception handling uses the try, catch, and finally keywords (in Visual Basic: Try, Catch, and Finally) to attempt actions that may not succeed, to handle failures, and to clean up resources afterwards. For example, the code from the Anticipatory Set in the presentation throws a DivideByZeroException.
try • The try (or Try) keyword is used to indicate that a block of code should be monitored for exceptions. • You can think of it as giving a warning to the application that there may be trouble ahead—so watch out! catch • The catch (or Catch) keyword is used to designate a block of code used to handle an exception if one is thrown. • You can use multiple catch blocks to handle different types of exceptions.
Example of try/catch Bloacks intSafeDivision(int x, int y) { try { return (x / y); } catch (System.DivideByZeroExceptiondbz) { System.Console.WriteLine("Division by zero!"); return 0; } }
finally • The code in a finally (or Finally) block always executes last, just before the error-handling block loses scope, regardless of whether the code in the catch blocks has executed. • finally blocks are not mandatory in structured exception handling, but they provide a great place for cleanup code (such as code for storing important data, closing files, etc).
Lesson Review • List three basic types of errors. • What are exceptions? • What keywords are used to handle exceptions in applications?
Assemblies • An assembly is a collection of types and resources that are built to work together and form a logical unit of functionality. • Each time you create a .NET application, you are creating an assembly. • There are other types of assemblies as well, such as class libraries.
Namespaces Namespaces are organizational units used for two purposes: • To organize the many classes within the .NET Framework • To help control scope and avoid name collisions (having two classes with the same name)
Namespace Example • The Button type commonly used in Forms applications is nested within several namespaces: • The fully qualified name for the Button class is System.Windows.Forms.Button. System Namespace Windows Namespace Forms Namespace Button Class
Using and Imports Statements • Because using fully qualified names can be cumbersome (as with the Button example), .NET languages provide a way to shorten references. • In Microsoft C#, this is done by adding using statements at the top of your code. • Microsoft Visual Basic uses the Imports statement. • These statements allow developers to refer to Button rather than System.Windows.Forms.Button.
Object Class • All classes in the .NET Framework are ultimately derived from a class called Object. • In other words, all classes in the .NET Framework—and all classes you define in your applications—inherit from Object, either directly or indirectly. • The Object class includes a small number of methods that are often overridden but derived classes, such as Equals(Object), ToString(), and Finalize().
System Namespaces • The System namespace contains fundamental classes and base classes that define commonly used value and reference data types, events and event handlers, interfaces, attributes, and processing exceptions. • These classes and their members provide a foundation of classes, types, and methods for creating applications. This extensive library is one of the primary strengths of developing with the .NET Framework.
Lesson Review • What are assemblies? • Why does the .NET Framework use namespaces?
Object-oriented programming(OOP) • OOP is a programming paradigm or model in which classes and objects work together to create a program or application. • It enables developers to think about a problem in a way that is similar to how we look at the real world—many objects that interact with each other. • The fundamental building blocks of object-oriented programs are classes and objects.
Classes and Objects • Classes are like blueprints or recipes for objects. • You can create multiple houses from one blueprint and multiple batches of cookies from one recipe. • Each house (or each batch of cookies) is referred to as an instance. • Classes and objects are not the same thing; a class is not an object, just as a recipe is not a batch of cookies.
Inheritance • Inheritance is a technique in which a class inherits the attributes and behaviors of another class. • Allows the developer to reuse, extend, and modify an existing class. Example: • The Aircraft class might define the basic characteristics and behaviors of a flying vehicle. Methods in that class might include Fly(), TakeOff(), and Land(). • The Helicopter class could add a method called Hover(), whichmight replace or “override” the TakeOff() method so that takeoffs are vertical.
The class whose members are inherited is called the base class. • The class that inherits those members is called the derived class. • In our previous example, Aircraft is the base class; Helicopter is the derived class. • Some developers refer to a base class as a superclass or a parent class; a derived class could then be called a subclass or a child class. • We can test the validity of an inheritance relationship with the so-called “Is-A” test. “A helicopter is an aircraft,” so that is an appropriate use of inheritance. • However, the Car class should not inherit from Aircraft because “a car is an aircraft” doesn’t make sense
Inheritance in .NET Languages • In Microsoft C#, inheritance is established using the colon (:)public class Helicopter : Aircraft {} • In Microsoft Visual Basic, inheritance is indicated with the Inherits keyword:Public Class Helicopter Inherits AircraftEnd Class
Polymorphism • Polymorphism is a language feature that allows a derived class to be used interchangeably with its base class. • In our example, a Helicopter can be treated like an Aircraft when the developer writes code. If you developed a JumboJetclass, it could also be used like an Aircraft. • Polymorphism is especially useful when working with collections, such as lists or arrays. A collection of Aircraft could be populated with Helicopter instances and JumboJet instances, making the code much more flexible.
Interfaces • An interface defines a set of properties, methods, and events, but it does not provide any implementation. • It is essentially a contract that dictates how any class implementing the interface must be implemented. • Consider a power outlet in the wall, which is similar to an interface. It dictates that any appliance that wants to use that power must follow some basic rules: how many prongs are required, the shape and spacing of those prongs, and so on. • An object cannot be instantiated (“constructed”) from an interface.
Lesson Review • Explain the relationship between a base class and a derived class. • Why do developers use polymorphism? • How is an interface like a contract?
Namespaces • Namespaces are organizational units used for two purposes: • To organize the many classes within the .NET Framework • To help control scope and avoid name collisions
Name Collisions • Sometimes a developer of a class library will encounter difficulties caused by the use of similar names in another library. • For example, a developer writing a library for handling game pad input might have several classes and methods related to the gamepad buttons; however, the name Button is already used for buttons on Microsoft Windows forms. • This type of conflict is called a name collision. • Namespaces avoid name collisions by allowing the developer to use a fully qualified name to reference a class.
Namespace Example • The Button type commonly used in Forms applications is nested within several namespaces: • The fully qualified name for the Button class is System.Windows.Forms.Button. System Namespace Windows Namespace Forms Namespace Button Class
Fully Qualified Names • Fully qualified names are object references that are prefixed with the name of the namespace in which the object is defined. • In our previous example, the gamepad developer can create a class called Button; if he or she wishes to use a Forms button as well, he can use the following fully qualified name when referencing that particular button control: System.Windows.Forms.Button • This ensures that the compiler will use the Button control rather than the class that the developer implemented.
Using/Imports Directives • Because using fully qualified names can be cumbersome (as with the Button example), .NET languages provide a way to shorten references. • In C#, this is accomplished by adding using directives at the top of your code. • In Visual Basic, it is accomplished using the Imports directive. • So long as there are no name collisions, these strategies allow developers to refer to ListBox, rather than System.Windows.Forms.Listbox.
Namespaces in .NET Applications • When you create a new project in Visual Studio, it automatically defines a new namespace with the same name as your project. • By default, all classes that you add to the project within Visual Studio will use this default namespace. However, you can easily define your own namespace using the namespace (C#) or Namespace (Visual Basic) keyword. • In C#:namespace MyNewNamespace{} • In Visual Basic:Namespace MyNewNamespaceEnd Namespace
Lesson Review • What is fully Qualified namespace? • What is the fully Qualified namespace for Button Class ?
Reusing Classes • Often, a class developed for one application may be useful in another application. • For example, if a developer creates a class with methods for calculating shipping costs for an internal inventory system, that class may be helpful when developing a retail website where the customer may want to know shipping costs. • If you rewrite the code—or copy it to the second project—you have maintainability concerns. • If you fix a bug or make any improvement in one of the projects, you have to make the same change in the other. • To avoid this problem, developers often use class libraries
Class Library • A class library is a collection of classes (and related code) that is packaged for reuse. • Unlike an application, typically packaged as an executable (.exe) file, a class library project creates a dynamic-link library (.dll) file. • There are several key advantages of class libraries: • DLLs are easy to distribute and easy to replace when making software revisions. • A DLL can be used by developers in many different programming languages regardless of the language used for creating the class library.
Lesson Review • What is the DLL ? • What is the use of Class library?