Introduction to Class Inheritance
Introduction to Class Inheritance
Fundamentals of Class Inheritance
Introduction
Inheritance consists of creating a class that is based on another class.
Deriving a Class
To create a new class that is based on another class, first create the class has we saw already. After the name of the class, type a colon followed by the name of the existing class. Here is an example:
namespace Exercises.Models
{
public class Circle
{
}
public class Cylinder : Circle
{
}
}
A class that is based on another class is also referred to as derived. The class that existed already is called the based class or the parent class.
Introduction to the Members of a Derived Class
In the derived class, you can add members. For example, you can declare a variable in a class. That is, you can create a field in the class. Here is an example:
namespace Exercises.Models
{
public class Circle
{
}
public class Cylinder : Circle
{
private string purpose;
}
}
The Protected Members of a Class
To indicate that a member can be accessed only in a class or by members of a derived class, mark the member with the protected keyword. Here is an example of a protected field:
@functions{
internal class Circle
{
protected double radius;
}
}
@functions{
public class Cylinder : Circle
{
}
}
Introduction to Built-In Classes
Overview
You can create some of the classes you need for your applications, but to assist you, the .NET Framework provides many built-in classes you can directly use in your projects.
Introduction to the Object
To support objects, the .NET Framework provides the Object class represented in the C# language by the object data type. You can use that type to declare a variable of any kind. After declaring the variable, initialize it with a value of your choice. Here are examples:
@{ // An object variable for a string object employeeName = "Philippe Blayne"; // An object variable for an integer object yearsOfExperience = 5; // An object variable for a decimal number object hourlySalary = 26.95; }
To present the value to the user, you can include its variable in an HTML tag. Here are examples:
@page @model Valuable.Pages.FoundationsModel @{ object employeeName = "Philippe Blayne"; object hourlySalary = 26.95; object yearsOfExperience = 5; } <h3>=//= Employee Record =//=</h3> <b>Employee Name:</b> @employeeName</p> <b>Hourly Salary:</b> @hourlySalary</p> <p><b>Length of Experience:</b> @yearsOfExperience years</p>
This would produce:
=//= Employee Record =//= Employee Name: Philippe Blayne Hourly Salary: 26.95 Length of Experience: 5 years Press any key to continue . . .
Creating an Object-Based Class
Because Object is the most fundamental class in .NET, if you create a class, that class is implicitly derived from Object. Still, you can indicate this inheritance in a class you are creating. Here is an example:
@functions{ public class Person : Object { } }
Random Numbers
To support random numbers, the .NET Framework provides the Random class. You can use it to generate random numbers.
Exception Handling
An exception is an unusual situation in an application. The ability to deal with exceptions in an application is called exception handling. Exception handling is primarily done with the try and the catch keywords.
Exceptions in the .NET Framework
To support exception handling, the .NET Framework provides many classes. The most fundamental class for exceptions is named Exception. There exist many other classes, such as FormatException, OverflowException, ArgumentOutOfRangeException, DivideByZeroException, etc.
Throwing an Exception
To indicate that you want to issue an exception you are anticipating in your code, use the throw keyword, followed by the new operator and an Exception-based class.
Catching Various Exceptions
You can write code that deals with various exceptions. To do this, after the try clause, create a catch() section for each exception. Enter the desired exception argument in a catch() clause.
Nesting an Exception
You can nest an exception inside another exception.
Introduction to Page Models
Introduction
The types of applications we create here are named ASP.NET Core Web applications. The main document in an ASP.NET Core Web application is called a Razor Page. To get it, you start by creating a class, named a Page Model class, or just a Page Model.
Creating a Page Model
A Razor Page is created as a C# class. ASP.NET Web applications expect a class associated to a Razor Page so that you write some C# code in that class. That code can easily be made available to the associated Razor Page. Such a class is called a Page Model.
To support page models, the .NET Framework provides a class named PageModel:
[Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure.PageModel] public abstract class PageModel : Microsoft.AspNetCore.Mvc.Filters.IAsyncPageFilter, Microsoft.AspNetCore.Mvc.Filters.IFilterMetadata, Microsoft.AspNetCore.Mvc.Filters.IPageFilter
The PageModel class is defined in the Microsoft.AspNetCore.Mvc.RazorPages namespace created in the Microsoft.AspNetCore.Mvc.RazorPages.dll assembly. To get an object you can use to perform object-oriented tasks for a Razor Page, create a class derived from PageModel. When creating your class, specify its name but end that name with Model. You should create the class either in the Pages folder or in a sub-folder of the Pages folder. Here is an example:
using Microsoft.AspNetCore.Mvc.RazorPages; namespace Valuable.Pages { public class ExerciseModel : PageModel { } }
Normally, you will (if not always) ask Microsoft Visual Studio to create a Page Model class for you. To do that, create a Razor Page. When you click Add or you press Enter in the Add New Item dialog box, Microsoft Visual Studio creates two documents tied among themselves. The class is created in a document whose file has the name you specified for the RazorPage + Model. The file has the extension .cshtml. The Page Model is a document of a file that uses the name you specified for the Razor Page, followed by a period, and followed by cshtml. The extension of that file is .cs.
The Page of a Razor Page
A Razor Page must be able to use the values from a Page Model. To provide that information to the compiler, in the top section of a Razor Page, you must have a line as @page:
@page
This must be the first line of a Razor Page document and it must stand alone. If you are creating your project in Microsoft Visual Studio, when you create a Razor Page, the studio generates the necessary Razor Page document and adds that line to the document for you. You must not delete that line.
A Variable in a Page Model
Declaring a Variable
Remember that a Page Model is primarily a class, like any other. You can declare a variable in a Page Model. You can do it anywhere in the body of the class. Here is an example:
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace Fundamentals.Pages
{
public class ExerciseModel : PageModel
{
int number;
}
}
A Public Variable
A Page Model is primarily a class like any other. As such, you can declare a variable in it. That is, you can create a field in a Page Model. If you simply declare a variable in a Page Model, that variable can be used and accessed only in the body of the class. If you want to access and use a Page Model declared variable in a Razor Page, mark that variable with the public keyword. Here is an example:
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace Fundamentals.Pages
{
public class ExerciseModel : PageModel
{
public double price;
}
}
When declaring a variable in a Page Model, you can immediately initialize it. Here are examples:
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace Variables.Pages.WaterMeters
{
public class CreateModel : PageModel
{
public int meterId = 2;
public string meterNumber = "938-75-869";
public string make = "Stanford Trend";
public string meterModel = "266G";
public string meterSize = "1 1/2 Inches";
}
}
If necessary, you can make the variable constant by applying the const keyword to its data type.
Displaying a Page Model Variable in a Web Page
When you create a Page Model class, it makes its content available through an object named Model. To display the value of such a variable on a Razor Page, create the desired HTML tag. In that tag, type @Model. followed by the name of the variable in a Page Model. Here are examples:
@page @model Variables.Pages.WaterMeters.CreateModel @{ } <h1 class="ta-center fw-bold common-font">Water Meter Setup</h1> <hr /> <table> <tr> <td class="fw-bold">Meter Id:</td> <td>@Model.meterId</td> </tr> <tr> <td class="fw-bold">Meter #:</td> <td>@Model.meterNumber</td> </tr> <tr> <td class="fw-bold">Make:</td> <td>@Model.make</td> </tr> <tr> <td class="fw-bold">Model:</td> <td>@Model.model</td> </tr> <tr> <td class="fw-bold">Meter Size:</td> <td>@Model.meterSize</td> </tr> </table>
If you want to display the value in a Web form, access the value attribute of a Web control. Assign a "@Model.object-member" to that attribute.
Using the Document of a Page Model
A Class in a Page Mode Document
The document in which a Page Model class is created is just a regular code file. If you want and if you judge it necessary, you can create one or more classes in that document. You should create such (a) class(es) in the body of the namespace and outside any class. Here is an example:
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace Exercises.Pages
{
public class ExerciseModel : PageModel
{
}
public class Vehicle
{
}
}
In the same way, you can create as many classes as you want in the document that contains a Page Model.
A Class in a Page Mode Document
Either way, after creating a class, whether in the document of a Page Model or in the Models folder, if you want to use that class in your Page Model class, you can first declare a variable of it. Here is an example:
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace Variables.Pages.WaterMeters
{
public class CreateModel : PageModel
{
public WaterMeter waterMeter = new WaterMeter();
public void OnGet()
{
}
}
public class WaterMeter
{
public int meterId = 4;
public string meterNumber = "207-94-835";
public string make = "Constance Technologies";
public string model = "TG-6220";
public string meterSize = "5/8 Inches";
}
}
Displaying a Page Model Variable in a Web Page
If you declare a variable of a class in a Page Model class, to display the values of that class in a Razor Page, use @Model., followed by the name of the object (the variable of the class), followed by a period and the member you want to access. Here are examples:
@page @model Variables.Pages.WaterMeters.CreateModel @{ } <h1 class="ta-center fw-bold common-font">Water Meter Setup</h1> <hr /> <table> <tr> <td class="fw-bold">Meter Id:</td> <td>@Model.waterMeter.meterId</td> </tr> <tr> <td class="fw-bold">Meter #:</td> <td>@Model.waterMeter.meterNumber</td> </tr> <tr> <td class="fw-bold">Make:</td> <td>@Model.waterMeter.make</td> </tr> <tr> <td class="fw-bold">Model:</td> <td>@Model.waterMeter.model</td> </tr> <tr> <td class="fw-bold">Meter Size:</td> <td>@Model.waterMeter.meterSize</td> </tr> </table> } }
Web Forms
Introduction
To assist you in creating and managing Web forms, the .NET Framewok provides many built-in classes for the forms, the Web controls in the form, and related operations.
Value and Reference Type
A variable of a primitive type (int, char, float, double, etc) is referred to as a value type. A variable of a class is a reference type. Therefore, after declaring a variable of a class type, you must initialize it using the new operator. This operator can be followed by the name of the class followed by parentheses. Here is an example:
Vehicle sedan = new Vehicle();
An Object in a Code File
You can declare the variable in a code file such as in the body of a Page Model class. If the class is created in the code file of the Page Model where you are declaring the variable, simply use the name of the class. Here is an example:
using Microsoft.AspNetCore.Mvc.RazorPages; namespace Exercises.Pages { public class ExerciseModel : PageModel { StoreItem si = new StoreItem(); } public class StoreItem { } }
If the class is created in the Models folder, when declaring the variable, you can precede the name of the class with Models and a period. Here is an example:
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace Exercises.Pages
{
public class ExerciseModel : PageModel
{
StoreItem si = new StoreItem();
Models.Camera viewer = new Models.Camera();
}
public class StoreItem
{
}
}
An alternative is to create a using line with the Models namespace. Here is an example:
using Microsoft.AspNetCore.Mvc.RazorPages; using Exercises.Models; namespace Exercises.Pages { public class ExerciseModel : PageModel { StoreItem si = new StoreItem(); Camera Viewer = new Camera(); } public class StoreItem { } }
An Object in a Razor Page
You can also declare a variable in a Razor Page. If the class is in the Razor Page, to create an object, declare the variable in an @{} section. Here is an example:
@page @model Exercises.Pages.ExerciseModel @{ Appliance device = new Appliance(); } @functions{ public class Appliance { } }
If the class is created in the Page Model associated with the Razor Page, you can directly declare the variable in an @{} section. Here is an example:
@page
@model Exercises.Pages.ExerciseModel
@{
StoreItem guitar = new StoreItem();
}
If the class was created in the Models folder, you can precede the name of the class with Models and a period. Here is an example:
@page
@model Exercises.Pages.ExerciseModel
@{
Models.Camera show = new Models.Camera();
}
As an alternative, under the @page line, start a line with @using, followed by a space, the name of the project as a namespace, a period, and Models. Then, in an @{} section, declare the variable. Here is an example:
@page @model Exercises.Pages.ExerciseModel @using Exercises.Models @{ Camera show = new Camera(); }
Using Objects and Variables
Declaring a Variable in a Page Model
Remember that a Page Model is primarily a class, like any other. You can declare a variable in a Page Model. You can do it anywhere in the body of the class. Here is an example:
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace Fundamentals.Pages
{
public class ExerciseModel : PageModel
{
int number;
}
}
A Public Variable
If you declare a variable in a Page Model as we did above, the variable can be used and accessed only in the body of the class. If you want to access and use a Page Model declared variable in a Razor Page, mark that variable with the public keyword. Here is an example:
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace Fundamentals.Pages
{
public class ExerciseModel : PageModel
{
public double price;
}
}
When declaring a variable in a Page Model, you can immediately initialize it. Here is an example:
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace Valuable.Pages
{
public class Exercise1Model : PageModel
{
int number = 10_000;
}
}
If necessary, you can make the variable constant by applying the const keyword to its data type.
Displaying a Variable
Introduction
If you declare a variable whether in a Page Model or in an @{} section of a Razor Page and that variable has a value, if you want to display the value of the variable, you have many options.
Displaying a Variable in a Web Page
To display a value on a webpage, outside the @{} section, create the HTML tag that is needed to display the value of the variable. In the tag, type the name of the variable preceded by the @ symbol. This can be done as follows:
@{
data-type variable-name = desired-value;
}
<p>@variable-name</p>
In the HTML tag, you can include anything else you want. The most important two rules are that:
Here is an example:
@{
int age = 15;
}
<p>@age</p>
In the same way, you can display values of variables.
Displaying a Page Model Variable in a Web Page
When you create a PageModel class, it makes its content available through the Model object. Before displaying a variable of a Page Model to a Razor Page, first declare the variable as public as we saw earlier. You must also provide a value for the variable. Here is an example:
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace Fundamentals.Pages
{
public class ExerciseModel : PageModel
{
public double unitPrice = 224.95;
}
}
To display the value of such a variable on a webpage, create the desired HTML tag. In that tag, type @Model. followed by the name of the Page Model variable. Here is an example:
@page
@model Valuable.Pages.Exercise1Model
@{
}
<p>Unit Price: @Model.unitPrice</p>
Operations on Variables
After declaring variables, you can perform operations on them. All algebraic operations are available: the addition, the subtraction, the multiplication, the division, and the remainder. You can also use operators such as parentheses when necessary. Incrementing (pre and post), decrementing (pre and post), and compound operations are also available.
===================================================================================================Introduction to Boolean Values
Overview
C# and Razor Pages support conditional statements. The concepts start with the Boolean structure or the bool data type that you can use to declare a Boolean variable. A Boolean Variable can hold a true or a false value. Here is an example of declaring a Boolean variable:
@{ bool drinkingUnderAge; }
To initialize a Boolean variable or to change its value, assign true or false to it. Here is an example:
@{
bool drinkingUnderAge = true;
}
You can also declare the variable as var, object or dynamic. If you declare the variable using one of those keywords, then you must initialize the variable when declaring it.
Parsing a Boolean Value
To let you parse a Boolean value, the Boolean structure and the bool data type are equipped with the Parse() method whose syntax is:
public static bool Parse(string value);
This method takes a True or False (case-insensitive in both cases) string as argument. The method returns true or false. An alternative is to call the Boolean.TryParse() method whose syntax is:
public static bool TryParse(string value, out bool result);
Introduction to Conditions
A conditional statement is a logical expression that produces a true or false result. The primary operator you can use is if. The basic formula to use it is:
if(expression) statement;
If the statement is long, you can write it on a line different from that of the if condition. This would be done as follows:
if(expression) statement;
You can also write the statement on its own line even if the statement is short enough to fit on the same line with the condition.
The body of the conditional statement is the area after the statement. If you are writing your code in a class and the statement has only one line of code, you can just write the statement. If you are writing your code in a Razor Page, you must include the statement between { and }. This would be done as follows:
@{ if( expression) { . . . . . . . . . } }
If you are writing your code in a class and the expression has more than one line, you must include those lines between { and }. In either case, the section from { to } is the body of the conditional statement.
Checking the Content of a Web Form
When the user clicks a button on a form of a webpage, before doing anything else, you need to check whether the form contains some value(s). To assist you with this, the HttpRequest class is equipped with the HasFormContentType member. It allows a form to indicate when the submit button of a form has been clicked. To deal with this issue, create an if() conditional statement. In the parentheses, access the HttpRequest.HasFormContentType member. This would be done as follows:
@page
@model Valuable.Pages.ExerciseModel
@{
if(Request.HasFormContentType)
{
}
}
In the body of this conditional statement, you will write the necessary statement(s).
Logical Operators
Introduction
In your ASP.NET Web project, you can use any of the logical operators available in C#. Those logical operators are used exactly as in C#.
We already saw that, to perform a logical comparison, you can create an if() conditional statement. The if() operator can receive a logical operation in its parentheses. The primary formula to use it is:
if(condition) statement;
The condition can have the following formula:
operand1 Boolean-operator operand2
If you are working on Microsoft Visual Studio, you can use a code snippet to create an if conditional statement. Any of the operands can be a constant or the name of a variable. The operator is a logical one. The whole expression is the condition. If the expression produces a true result, then the statement would execute.
Introduction to Boolean Operators
A Boolean operator allows you to perform a comparison. The primary formula to use is:
operand1 operator operand2
Value Equality
To find out whether one value is equal to another, use the == operator, as in:
value1 == value2
Negating a Condition
If you have a logical expression or value, to get its logical opposite, you can use the !, as in:
!variable-or-expression
To nullify a variable, you can write the exclamation point to its left. Here is an example:
@{
bool employed = true;
bool validation = !employed;
}
A Value Not Equal
To find out whether one value is equal to another, use the != operator, as in:
value1 != value2
A LowerValue
To find out whether one value is lower than another, use the < operator. To make your code easy to read, you can precede the operator with is as in "is <":
value1 is < value2
A Value Equal or Lower
To find out whether one value is lower than or equal to another, use the "<=" operator or "is <=":
value1 <= value2
Here is an example that uses the <= operator:
@page
@model Valuable.Pages.FoundationsModel
@using static System.Console
@{
double originalPrice = 124.50;
double discountRate = 35.00; // %
double number_of_days_in_store = 75;
if (number_of_days_in_store is <= 45)
discountRate = 25.00;
double discountAmount = originalPrice * discountRate / 100.00;
double markedPrice = originalPrice - discountAmount;
string strOriginalPrice = $"{originalPrice:N}";
string strDaysInStore = $"{number_of_days_in_store}";
string strDiscountRate = $"{discountRate:N}";
string strDiscountAmount = $"{discountAmount:N}";
string strMarkedPrice = $"{markedPrice:N}";
}
<pre>Fun Department Store");
----------------------------------
Original Price: @strOriginalPrice
Days in Store: @number_of_days_in_store
Discount Rate: @discountRate
Discount Amount: @discountAmount
Marked Price: @markedPrice</pre>
@{
number_of_days_in_store = 22;
if (number_of_days_in_store is <= 45)
discountRate = 25.00;
discountAmount = originalPrice * discountRate / 100.00;
markedPrice = originalPrice - discountAmount;
}
<pre>Fun Department Store");
----------------------------------
Original Price: @strOriginalPrice
Days in Store: @number_of_days_in_store
Discount Rate: @discountRate
Discount Amount: @discountAmount
Marked Price: @markedPrice</pre>
This would produce:
Fun Department Store ---------------------------------- Original Price: 124.50 Days in Store: 75 Discount Rate: 35 Discount Amount: 43.575 Marked Price: 80.925 Fun Department Store ---------------------------------- Original Price: 124.50 Days in Store: 22 Discount Rate: 25 Discount Amount: 31.125 Marked Price: 93.375
A Greater Value
To find out whether one value is greater than another, use the > operator or "is >", as in
value1 is > value2
A Value Equal or Greater
To find out whether one value is greater than or equal to another, use the ">=" operator or "is >=", as in
value1 is >= value2
We saw that the <, the <=, the >, and the >= operators can be preceded by the is keyword. Here is an example that uses the >= operator:
@{
int level = 0;
if(level is >= 3)
conclusion = "Welcome on board";
}
To get the opposite result of those operators, you can use the not operator. The expression to use is is not. Here is an example:
@{
int level = 0;
if(level is not >= 3)
conclusion = "You failed the Security Clearance or the Clearance was not conclusive.";
}
Practical Learning: Starting a Project
body { } .w100 { width: 100px; } .w125 { width: 125px; } .w150 { width: 150px; } .ta-right { text-align: right; } .ta-center { text-align: center; } .common-font { font-family: Georgia, Garamond, 'Times New Roman', serif; }
Practical Learning: Comparing for a Lesser Value
@page @model PayrollPreparation10.Pages.TimeSheetModel @{ string? firstName = string.Empty; string? lastName = string.Empty; double hourlySalary = 0.00; double monday = 0.00; double tuesday = 0.00; double wednesday = 0.00; double thursday = 0.00; double friday = 0.00; double timeWorked = 0.00; double netPay = 0.00; double regTime = 0.00; double overtime = 0.00; double overPay = 0.00; double regPay = 0.00; string strMonday = string.Empty; string strTuesday = string.Empty; string strWednesday = string.Empty; string strThursday = string.Empty; string strFriday = string.Empty; string strHourlySalary = string.Empty; string strRegularTime = string.Empty; string strOvertime = string.Empty; string strRegularPay = string.Empty; string strOvertimePay = string.Empty; string strNetPay = string.Empty; if (Request.HasFormContentType) { firstName = Request.Form["txtFirstName"]; lastName = Request.Form["txtLastName"]; hourlySalary = double.Parse(Request.Form["txtHourlySalary"]!); monday = double.Parse(Request.Form["txtMonday"]!); tuesday = double.Parse(Request.Form["txtTuesday"]!); wednesday = double.Parse(Request.Form["txtWednesday"]!); thursday = double.Parse(Request.Form["txtThursday"]!); friday = double.Parse(Request.Form["txtFriday"]!); timeWorked = monday + tuesday + wednesday + thursday + friday; regTime = 40.00; regPay = hourlySalary * 40.00; overtime = timeWorked - 40.00; overPay = hourlySalary * 1.50 * overtime; if (timeWorked is <= 40.00) { regTime = timeWorked; regPay = hourlySalary * timeWorked; overtime = 0.00; overPay = 0.00; } netPay = regPay + overPay; strMonday = $"{monday:F}"; strTuesday = $"{tuesday:F}"; strWednesday = $"{wednesday:F}"; strThursday = $"{thursday:F}"; strFriday = $"{friday:F}"; strHourlySalary = $"{hourlySalary:F}"; strRegularTime = $"{regTime:F}"; strOvertime = $"{overtime:F}"; strRegularPay = $"{regPay:F}"; strOvertimePay = $"{overPay:F}"; strNetPay = $"{netPay:F}"; } } <div class="common-font"> <h1 class="fw-bold ta-center">Payroll Preparation</h1> <hr /> <form name="PayrollEvaluation" method="post"> <h3 class="ta-center fw-bold">Employee Information</h3> <hr /> <table style="width: 625px" align="center"> <tr> <td class="w150 fw-bold">First Name:</td> <td><input type="text" class="w100 form-control" id="txtFirstName" name="txtFirstName" value="@firstName" /></td> </tr> <tr> <td class="fw-bold">Last Name:</td> <td><input type="text" class="w100 form-control" id="txtLastName" name="txtLastName" value="@lastName" /></td> </tr> <tr> <td class="fw-bold">Hourly Salary:</td> <td><input type="text" class="w100 form-control ta-right" id="txtHourlySalary" name="txtHourlySalary" value="@strHourlySalary" /></td> </tr> </table> <hr /> <table style="width: 625px" align="center"> <tr class="fw-bold"> <td class="w125"> </td> <td>Monday</td> <td>Tuesday</td> <td>Wednesday</td> <td>Thursday</td> <td>Friday</td> </tr> <tr> <td>Time Worked:</td> <td><input type="text" class="form-control w100 ta-right" id="txtMonday" name="txtMonday" value="@strMonday" /></td> <td><input type="text" class="w100 ta-right form-control" id="txtTuesday" name="txtTuesday" value="@strTuesday" /></td> <td><input type="text" class="w100 ta-right form-control" id="txtWednesday" name="txtWednesday" value="@strWednesday" /></td> <td><input type="text" class="w100 ta-right form-control" id="txtThursday" name="txtThursday" value="@strThursday" /></td> <td><input type="text" class="form-control w100 ta-right" id="txtFriday" name="txtFriday" value="@strFriday" /></td> </tr> </table> <hr /> <table style="width: 300px" align="center"> <tr> <td style="width: 50px"> </td> <td><input type="submit" value="Evaluate Payroll" name="btnEvaluatePayroll" class="w150" /></td> </tr> </table> <hr /> <table style="width: 625px" align="center"> <tr style="border-bottom: 1px solid black"> <td style="width: 225px"> </td> <td style="width: 225px">Pay Summary</td> <td class="ta-right">Time</td> <td class="ta-right">Pay</td> </tr> <tr style="border-bottom: 1px solid black"> <td> </td> <td class="ta-right">Regular:</td> <td class="ta-right">@strRegularTime</td> <td class="ta-right">@strRegularPay</td> </tr> <tr style="border-bottom: 1px solid black"> <td> </td> <td class="ta-right">Overtime:</td> <td class="ta-right">@strOvertime</td> <td class="ta-right">@strOvertimePay</td> </tr> <tr style="border-bottom: 1px solid black"> <td> </td> <td> </td> <td>Net Pay:</td> <td class="ta-right">@strNetPay</td> </tr> </table> </form> </div>
First Name: Thomas Last Name: Fisher Hourly Salary: 24.28 Monday: 7.5 Tuesday: 8 Wednesday: 6.5 Thursday: 8 Friday: 6.5
First Name: Emilio Last Name: Cordova Hourly Salary: 22.16 Monday: 9.5 Tuesday: 8 Wednesday: 7.5 Thursday: 10.5 Friday: 8
body { } .ta-right { text-align: right; } .ta-center { text-align: center; } .delimiter { margin: auto; width: 650px; } .top-bar { border-bottom: 6px solid blue; background-color: #f06d01 !important; } .common-font { font-family: Georgia, Garamond, 'Times New Roman', serif; } .navbar-light .navbar-brand { color: white; } .navbar-light .navbar-brand:hover { color: yellow; } .navbar-light .navbar-brand:focus { color: khaki; } .navbar-light .navbar-brand { font-family: Georgia, Garamond, 'Times New Roman', serif; } .nav-link { font-family: Georgia, Garamond, 'Times New Roman', serif; }
@page @model MachineDepreciation1.Pages.StraightLineMethodModel @using static System.Console @{ int year = 0; int estimatedLife = 0; double machineCost = 0.00; double salvageValue = 0.00; double depreciationRate = 0.00; double yearlyDepreciation = 0.00; string strDepreciation12 = string.Empty; string strDepreciationRate = string.Empty; string strDepreciableAmount = string.Empty; double bookValueYear0 = 0.00; double bookValueYear1 = 0.00; double bookValueYear2 = 0.00; double bookValueYear3 = 0.00; double bookValueYear4 = 0.00; double bookValueYear5 = 0.00; double bookValueYear6 = 0.00; double bookValueYear7 = 0.00; double bookValueYear8 = 0.00; double bookValueYear9 = 0.00; double bookValueYear10 = 0.00; string strBookValueYear0 = "0.00"; string strBookValueYear1 = "0.00"; string strBookValueYear2 = "0.00"; string strBookValueYear3 = "0.00"; string strBookValueYear4 = "0.00"; string strBookValueYear5 = "0.00"; string strBookValueYear6 = "0.00"; string strBookValueYear7 = "0.00"; string strBookValueYear8 = "0.00"; string strBookValueYear9 = "0.00"; string strBookValueYear10 = "0.00"; string strAccumulatedDepreciation1 = "0.00"; string strAccumulatedDepreciation2 = "0.00"; string strAccumulatedDepreciation3 = "0.00"; string strAccumulatedDepreciation4 = "0.00"; string strAccumulatedDepreciation5 = "0.00"; string strAccumulatedDepreciation6 = "0.00"; string strAccumulatedDepreciation7 = "0.00"; string strAccumulatedDepreciation8 = "0.00"; string strAccumulatedDepreciation9 = "0.00"; string strAccumulatedDepreciation10 = "0.00"; string strYearlyDepreciation = "0.00"; if (Request.HasFormContentType) { machineCost = double.Parse(Request.Form["txtMachineCost"]!); salvageValue = double.Parse(Request.Form["txtSalvageValue"]!); estimatedLife = int.Parse(Request.Form["txtEstimatedLife"]!); depreciationRate = 100 / estimatedLife; yearlyDepreciation = (machineCost - salvageValue) / estimatedLife; strDepreciation12 = $"{(yearlyDepreciation / 12):n}"; strYearlyDepreciation = $"{yearlyDepreciation:f}"; bookValueYear0 = machineCost - (yearlyDepreciation * 0); bookValueYear1 = machineCost - (yearlyDepreciation * 1); bookValueYear2 = machineCost - (yearlyDepreciation * 2); bookValueYear3 = machineCost - (yearlyDepreciation * 3); bookValueYear4 = machineCost - (yearlyDepreciation * 4); bookValueYear5 = machineCost - (yearlyDepreciation * 5); bookValueYear6 = machineCost - (yearlyDepreciation * 6); bookValueYear7 = machineCost - (yearlyDepreciation * 7); bookValueYear8 = machineCost - (yearlyDepreciation * 8); bookValueYear9 = machineCost - (yearlyDepreciation * 9); bookValueYear10 = machineCost - (yearlyDepreciation * 10); strBookValueYear0 = $"{bookValueYear0:F}"; strBookValueYear1 = $"{bookValueYear1:F}"; strBookValueYear2 = $"{bookValueYear2:F}"; strBookValueYear3 = $"{bookValueYear3:F}"; strBookValueYear4 = $"{bookValueYear4:F}"; strBookValueYear5 = $"{bookValueYear5:F}"; strBookValueYear6 = $"{bookValueYear6:F}"; strBookValueYear7 = $"{bookValueYear7:F}"; strBookValueYear8 = $"{bookValueYear8:F}"; strBookValueYear9 = $"{bookValueYear9:F}"; strBookValueYear10 = $"{bookValueYear10:F}"; strAccumulatedDepreciation1 = $"{yearlyDepreciation * 1:F}"; strAccumulatedDepreciation2 = $"{yearlyDepreciation * 2:F}"; strAccumulatedDepreciation3 = $"{yearlyDepreciation * 3:F}"; strAccumulatedDepreciation4 = $"{yearlyDepreciation * 4:F}"; strAccumulatedDepreciation5 = $"{yearlyDepreciation * 5:F}"; strAccumulatedDepreciation6 = $"{yearlyDepreciation * 6:F}"; strAccumulatedDepreciation7 = $"{yearlyDepreciation * 7:F}"; strAccumulatedDepreciation8 = $"{yearlyDepreciation * 8:F}"; strAccumulatedDepreciation9 = $"{yearlyDepreciation * 9:F}"; strAccumulatedDepreciation10 = $"{yearlyDepreciation * 10:F}"; double depreciatiableAmount = machineCost - salvageValue; //depreciationRate = 100 / estimatedLife; yearlyDepreciation = depreciatiableAmount / estimatedLife; strDepreciableAmount = $"{(depreciatiableAmount):n}"; strDepreciationRate = $"{(depreciationRate):n}"; strYearlyDepreciation = $"{yearlyDepreciation:f}"; WriteLine("===================================="); WriteLine("Depreciation - Straight-Line Method"); WriteLine("------------------------------------"); WriteLine("Machine Cost: {0}", machineCost); WriteLine("Salvage Value: {0}", salvageValue); WriteLine("Estimate Life: {0} Years", estimatedLife); WriteLine("Depreciation Rate: {0}%", depreciationRate); WriteLine("------------------------------------"); WriteLine("Depreciable Amount: {0}", depreciatiableAmount); WriteLine("Yearly Depreciation: {0}", yearlyDepreciation); WriteLine("===================================="); } } <div class="common-font"> <h1 class="fw-bold text-center">Machine Depreciation Evaluation</h1> <h2 class="fw-bold text-center">Straight-Line Method</h2> <hr /> <form name="PayrollEvaluation" method="post"> <table align="center" style="width: 325px" class="table"> <tr> <td style="width: 150px" class="fw-bold">Machine Cost:</td> <td><input type="text" style="" id="txtMachineCost" name="txtMachineCost" value="@machineCost" class="form-control ta-right" /></td> <td> </td> </tr> <tr> <td class="fw-bold">Salvage Value:</td> <td><input type="text" id="txtSalvageValue" name="txtSalvageValue" value="@salvageValue" class="form-control ta-right" /></td> <td> </td> </tr> <tr> <td class="fw-bold">Estimated Life:</td> <td><input type="text" id="txtEstimatedLife" name="txtEstimatedLife" value="@estimatedLife" class="form-control ta-right" /></td> <td>years</td> </tr> </table> <hr /> <table style="width: 300px" align="center"> <tr> <td style="width: 50px"> </td> <td><input type="submit" value="Calculate Depreciation" name="btnCalculate" style="width: 200px" /></td> </tr> </table> </form> <hr /> <table style="width: 325px" align="center" class="table"> <tr> <td class="fw-bold">Machine Cost:</td> <td>@machineCost</td> </tr> <tr> <td class="fw-bold">Salvage Value:</td> <td>@salvageValue</td> </tr> <tr> <td class="fw-bold">Estimate Life:</td> <td>@estimatedLife Years</td> </tr> <tr> <td class="fw-bold">Depreciation Rate:</td> <td>@strDepreciationRate %</td> </tr> <tr> <td class="fw-bold">Depreciable Amount:</td> <td>@strDepreciableAmount</td> </tr> <tr> <td class="fw-bold">Yearly Depreciation:</td> <td>@strYearlyDepreciation /month</td> </tr> </table> <hr /> <table style="width: 625px" align="center" border="3"> <tr style="border-bottom: 1px solid black"> <td class="fw-bold">Year</td> <td class="ta-center fw-bold">Yearly Depreciation</td> <td class="fw-bold">Book Value</td> <td class="fw-bold">Accumulated Depreciation</td> </tr> <tr style="border-bottom: 1px solid black"> <td class="ta-center">@year</td> <td></td> <td>@strBookValueYear0</td> <td></td> </tr> <tr style="border-bottom: 1px solid black"> <td class="ta-center">@(year + 1)</td> <td class="ta-center">@strYearlyDepreciation</td> <td>@strBookValueYear1</td> <td class="ta-center">@strAccumulatedDepreciation1</td> </tr> <tr style="border-bottom: 1px solid black"> <td class="ta-center">@(year + 2)</td> <td class="ta-center">@strYearlyDepreciation</td> <td>@strBookValueYear2</td> <td class="ta-center">@strAccumulatedDepreciation2</td> </tr> <tr style="border-bottom: 1px solid black"> <td class="ta-center">@(year + 3)</td> <td class="ta-center">@strYearlyDepreciation</td> <td>@strBookValueYear3</td> <td class="ta-center">@strAccumulatedDepreciation3</td> </tr> <tr style="border-bottom: 1px solid black"> <td class="ta-center">@(year + 4)</td> <td class="ta-center">@strYearlyDepreciation</td> <td>@strBookValueYear4</td> <td class="ta-center">@strAccumulatedDepreciation4</td> </tr> <tr style="border-bottom: 1px solid black"> <td class="ta-center">@(year + 5)</td> <td class="ta-center">@strYearlyDepreciation</td> <td>@strBookValueYear5</td> <td class="ta-center">@strAccumulatedDepreciation5</td> </tr> <tr style="border-bottom: 1px solid black"> <td class="ta-center">@(year + 6)</td> <td class="ta-center">@strYearlyDepreciation</td> <td>@strBookValueYear6</td> <td class="ta-center">@strAccumulatedDepreciation6</td> </tr> <tr style="border-bottom: 1px solid black"> <td class="ta-center">@(year + 7)</td> <td class="ta-center">@strYearlyDepreciation</td> <td>@strBookValueYear7</td> <td class="ta-center">@strAccumulatedDepreciation7</td> </tr> <tr style="border-bottom: 1px solid black"> <td class="ta-center">@(year + 8)</td> <td class="ta-center">@strYearlyDepreciation</td> <td>@strBookValueYear8</td> <td class="ta-center">@strAccumulatedDepreciation8</td> </tr> <tr style="border-bottom: 1px solid black"> <td class="ta-center">@(year + 9)</td> <td class="ta-center">@strYearlyDepreciation</td> <td>@strBookValueYear9</td> <td class="ta-center">@strAccumulatedDepreciation9</td> </tr> <tr style="border-bottom: 1px solid black"> <td class="ta-center">@(year + 10)</td> <td class="ta-center">@strYearlyDepreciation</td> <td>@strBookValueYear10</td> <td class="ta-center">@strAccumulatedDepreciation10</td> </tr> </table> </div>
Practical Learning: Ending the Lesson
|
|||
Previous | Copyright © 2001-2023, FunctionX | Friday 17 December 2021 | Next |
|