Introduction to Built-In Classes and Types

Overview

Many of the classes you will use in developping your websites, you will create them yourself, but to assist you in working on your projects, the .NET Framework provides a very rich collection of classes. A class is referred to as built-in if it is already available in the .NET Framework.

Introduction to Objects

In the .NET Framework, an object is any type of value. To support objects, the .NET Framework provides a class named Object. To support objects, the C# language uses a data type named object. You can use it to declare a variable of any kind. After declaring the variable, initialize it with a value of your choice. Here are examples:

@{
    object employeeName = "Philippe Blayne";
    object hourlySalary = 26.95;
    object yearsOfExperience = 5;
}

You can also use object as a parameter to a method. Here is an example:

class Exercise
{
    private void PresentEmployeeSummary(object something)
    {
    }
}

Remember that, in the body of the method, you can use or ignore the parameter. Still, when calling the method, you must provide a value for the parameter.

In the same way, you can create a method that uses various parameters that are of type object or some are objects while others are not. Here is an example:

class Employee
{
    private void PresentEmployeeSummary(object something, int other)
    {
    }
}

Introduction to Double-Precision Numbers

So far in our lessons, when we needed a decimal number, we used a type named decimal. The decimal type is used when your value or calculation requires a very large number. If your value or calculation is more concerned with precision, to assist you, the C# language supports another data type for decimal numbers, also referred to as floating-point numbers. This type for floating-point numbers is named double. It is the most regular type of floating-point numbers.

To declare a variable for a regular floating-point number, use the double data type followed by a name. Here is an example:

@{
    double number;
}

Of course, you can also use the var keyword to declare the variable. When initializng a double variable, you can assign a natural number or a decimal number to it. Here are examples:

@{
    double number = 90248;
    var distance = 2847;
}

Unlike the decimal type, the value of a double type doesn't require a letter. Here are examples:

@{
    double number = 90248;
    double value = 575.2704;
    var length = 2094.75;
}

Still, if you insist on indicating that the value of a double type, follow the number with d or .

Random Numbers

Introduction to Random Numbers

A number is referred to as random if it has been selected from a pool without a specific pattern to follow. In reality, it is difficult for a number to qualify as random. For this reason, most random numbers are referred to as pseudo-random.

Getting a Random Number

To support the ability to create or choose a random number, the .NET Framework provides a class named Random. To start, you can declare a variable of this class, using one of its two constructors. Here is an example that uses the default constructor:

@{
    Random rndNumber = new Random();
}

After declaring the variable, you can start getting numbers from it. To help you do this, the Random class is equipped with a method named Next. This method is overloaded in three versions. One of the versions of this method takes no argument. This method generates a randomly selected integer between 0 and a constant named MinValue. Here is an example:

@{
    Random rndNumbers = new Random();
    
    int rndNumber = rndNumbers.Next();
}

In the same way, you can call this version of the Next() method repeatedly to get random numbers. Here is an example:

<!DOCTYPE html>
<html>
<head>
<title>Random Numbers</title>
</head>
<body>
<h3>Random Numbers</h3>

@{
    Random rndNumbers = new Random();

    int rndNumber1 = rndNumbers.Next();
    int rndNumber2 = rndNumbers.Next();
    int rndNumber3 = rndNumbers.Next();
    int rndNumber4 = rndNumbers.Next();
    int rndNumber5 = rndNumbers.Next();
}

<p>Random Number: @rndNumber1</p>
<p>Random Number: @rndNumber2</p>
<p>Random Number: @rndNumber3</p>
<p>Random Number: @rndNumber4</p>
<p>Random Number: @rndNumber5</p>
</body>
</html>

Here is an example of what this could produce:

Random Numbers

The Seed of a Random Number

When creating a program that repeatedly gets a series of random numbers, you may (or may not) want the Random class to generate the same number over and over again. A seed is a constant value that controls whether a random generation would produce the same result every time it occurs. For example, using a seed, you can impose it upon the Random class to generate the same number every time the Next() method is called. To support the ability to use a seed, the Random class is equipped with a second constructor. Its syntax is:

public Random(int Seed);

Based on this, to specify a seed, when declaring a Random variable, pass a constant integer to the constructor.

Generating Random Numbers in a Range of Numbers

So far, we have been using any number that would fit an integer. In some assignments, you may want to restrict the range of numbers that can be extracted. Fortunately, the Random class allows this. Using the Random class, you can generate random positive numbers up to a maximum of your choice. To support this, the Random class is equipped with another version of the Next() method. It takes as argument an integer. The argument to pass to the method determines the highest integer that can be generated by the Next() method. The method returns an integer. Here is an example that generates 5 random numbers between 0 and 100:

<!DOCTYPE html>
<html>
<head>
<title>Random Numbers</title>
</head>
<body>
<h3>Random Numbers</h3>

@{
    Random rndNumbers = new Random();

    int rndNumber1 = rndNumbers.Next(100);
    int rndNumber2 = rndNumbers.Next(100);
    int rndNumber3 = rndNumbers.Next(100);
    int rndNumber4 = rndNumbers.Next(100);
    int rndNumber5 = rndNumbers.Next(100);
}

<p>Random Number: @rndNumber1</p>
<p>Random Number: @rndNumber2</p>
<p>Random Number: @rndNumber3</p>
<p>Random Number: @rndNumber4</p>
<p>Random Number: @rndNumber5</p>
</body>
</html>

Here is an example of what this could produce:

Random Numbers

The above version of the Next() method generates numbers starting at 0. If you want, you can specify the minimum and the maximum range of numbers that the Next() method must work with. To support this, the Random class is equipped with one more versions of this method and that takes two arguments. The first argument specifies the lowest value that can come from the range. The second argument holds the highest value that the Next() method can generate. Therefore, the method would operate between both values. Here is an example that generates random numbers from 6 to 18:

<!DOCTYPE html>
<html>
<head>
<title>Random Numbers</title>
</head>
<body>
<h3>Random Numbers</h3>

@{
    Random rndNumbers = new Random();

    int rndNumber1 = rndNumbers.Next(6, 18);
    int rndNumber2 = rndNumbers.Next(6, 18);
    int rndNumber3 = rndNumbers.Next(6, 18);
    int rndNumber4 = rndNumbers.Next(6, 18);
    int rndNumber5 = rndNumbers.Next(6, 18);
}

<p>Random Number: @rndNumber1</p>
<p>Random Number: @rndNumber2</p>
<p>Random Number: @rndNumber3</p>
<p>Random Number: @rndNumber4</p>
<p>Random Number: @rndNumber5</p>
</body>
</html>

Here is an example of what this could produce:

Random Numbers

Built-In Classes: Math

Introduction

To help you perform some of the basic algebraic and geometric operations in a website, the .NET Framework provides a static class named Math. Besides the Math class, you can also take advantage of Visual Basic's very powerful library of functions. This library is one of the most extended set of functions of various area of business mathematics.

The Sign of a Number

When initializing a numeric variable using a constant, you decide whether it is negative, 0 or positive. This is referred to as its sign. To help you determine the sign of a variable or of a number, the Math static class provides a method named Sign(). It is overloaded in various versions, for for each type. When calling this method, pass the value or the variable you want to consider, as argument. The method returns:

The Integral Side of a Floating-Point Number

We already know that a decimal number consists of an integral side and a precision side; both are separated by the decimal symbol which, in US English, is the period. To let you get the integral side of a decimal value, the Math static class provides a method named Truncate. It is overloaded in two versions whose syntaxes are:

public static double Truncate(double d);
public static double Truncate(double d);

When calling this method, pass it a number or a variable of numeric type. The method returns the int side of the value.

The Minimum of Two Values

If you have two numbers, you can find the minimum of both without writing your own code. To assist you with this, the Math class is equipped with a method named Min. This method is overloaded in various versions with each version adapted to each integral or decimal type.

The Maximum of Two Values

As opposed to the minimum of two numbers, you may be interested in the higher of both. To help you find the maximum of two numbers, you can call the Max() method of the Math class. It is overloaded in various versions with one of each type of numeric data.

Value Conversions

Implicit Conversions

If you have code with mixed types of variables, you may need to convert a value from one type to another. Implicit conversion is the ability to convert a value of one type into a value of another type. This is possible when the amount of memory that one type is using is lower than the amount necessary for another type. For example, a value of an integer can be directly converted into a double type. Here is an example:

<!DOCTYPE html>
<html>
<head>
<title>Numeric Conversion</title>
</head>
<body>
<h3>Numeric Conversion</h3>

@{
    int iNumber = 2445;

    double dNumber = iNumber;
}

<p>Number: @iNumber<br />
Number: @dNumber</p>
</body>
</html>

In the same way, as saw in our introduction to the double type, you can assign an integral value to a variable of type double. Here is an example:

@{
    double number = 927084;
}

This characteristic is referred to as implicit conversion.

Explicit Conversions

Because of memory requirements, the direct reverse of implicit conversion is not possible. Since the memory reserved for an int variable is smaller than that of a decimal, you cannot assign a value of type decimal to a variable of type int. As a result, the following code produces an error:

@{
    int hourlySalary = 24.75;
}

In the same way, you cannot assign a variable of types double or decimal to a variable of type int. This means that the following code produces an error:

@{
    double nbr = 9270.84;

    int number = nbr;
}

Value Casting

Value casting consists of converting a value of one type into a value of another type. Value casting is also referred to as explicit conversion.

To cast a value or a variable, precede it with the desired data type in parentheses. Here is an example:

<!DOCTYPE html>
<html>
<head>
<title>Numeric Conversion</title>
</head>
<body>
<h3>Numeric Conversion</h3>

@{
    double number = 9270.84;

    int nbr = (int)number;
}

<p>Number: @number<br />
Number: @nbr</p>
</body>
</html>

This would produce:

Casting a Value froom One Type Into Another Type

When performing explicit conversion, that is, when casting, you should pay close attention to the value that is being cast. If a value cannot fit in the memory of the other, you may get an unpredictable result.

The Convert Class

We already know that, to convert a numeric value to text, you can call a method named ToString applied to the value or its variable. To support the conversion of a value from one type to another, the .NET Framework provides a static class named Convert. This class is equipped with various static methods; they are so numerous that we cannot review all of them.

To adapt the Convert class to each C# data type, the class is equipped with a static method whose name starts with To, ends with the .NET Framework name of its type, and takes as argument the type that needs to be converted. Based on this, to convert a value to a double type, you can call the ToDouble() method and pass the value as argument. Its syntax is:

public static int ToDouble(value);

Practical LearningPractical Learning: Converting a Value from One Type to Another

  1. Start Microsoft Visual Studio
  2. To create a new application, on the main menu, click File -> New -> Project...
  3. In the middle list, click ASP.NET Web Application (.NET Framework) and set the Name to BusinessAccounting1
  4. Click OK
  5. In the New ASP.NET Web Application, click Empty and press Enter
  6. In the Solution Explorer, right-click BusinessAccounting1 -> Add -> New Folder
  7. Type Content and press Enter
  8. In the Solution Explorer, right-click Content -> Add -> New Item...
  9. In the left frame of the Add New Item dialog box, click Style Sheet
  10. Change the file Name to Site
  11. Press Enter
  12. Create some styles as follows:
    body {
    }
    
    .container {
        margin: auto;
        width:  520px; }
    
    table        { width:       100%;  }
    .accent      { font-weight: 600;   }
    .short-text  { width:       50px;  }
    .medium-text { width:       150px; }
  13. In the Solution Explorer, right-click BusinessAccounting1 -> Add -> New Item...
  14. In the left frame, expand Web and click Razor
  15. In the middle list, click Web Page (Razor v3)
  16. Set the name as Index
  17. Click Add
  18. Change the code as follows:
    <!DOCTYPE html>
    <html>
    <head>
    <title>Business Accounting</title>
    <link rel="stylesheet" type="text/css" href="~/Content/Site.css" />
    </head>
    <body>
    @{
        double rent = 0.00;
        double supplies = 0.00;
        double salaries = 0.00;
        double revenues = 0.00;
        double netIncome = 0.00;
        double totalExpenses = 0.00;
        double otherExpenses = 0.00;
    
        if (IsPost)
        {
            revenues = Convert.ToDouble(Request["txtRevenuesSources"]);
            salaries = Convert.ToDouble(Request["txtSalaries"]);
            supplies = Convert.ToDouble(Request["txtSupplies"]);
            rent =     Convert.ToDouble(Request["txtRent"]);
            otherExpenses = Convert.ToDouble(Request["txtOtherExpenses"]);
    
            totalExpenses = salaries + supplies + rent + otherExpenses;
            netIncome = revenues - totalExpenses;
        }
    }
    
    <div class="container">
        <h2>Fun Department Store</h2>
            <h3>- Business Accounting -</h3>
    
            <form name="frmBusiness" method="post">
                <table>
                    <tr>
                        <td colspan="3" class="accent">Revenues</td>
                    </tr>
                    <tr>
                        <td class="short-text">&nbsp;</td>
                        <td style="width: 300px; font-weight: bold">All Revenues Sources</td>
                        <td><input type="text" name="txtRevenuesSources" value="@revenues" /></td>
                    </tr>
                </table>
                <table>
                    <tr>
                        <td colspan="4" class="accent">Expenses</td>
                    </tr>
                    <tr>
                        <td class="short-text">&nbsp;</td>
                        <td class="medium-text accent">Salaries</td>
                        <td><input type="text" name="txtSalaries" value="@salaries" /></td>
                        <td>&nbsp;</td>
                    </tr>
                    <tr>
                        <td class="short-text">&nbsp;</td>
                        <td class="medium-text accent">Supplies</td>
                        <td><input type="text" name="txtSupplies" value="@supplies" /></td>
                        <td>&nbsp;</td>
                    </tr>
                    <tr>
                        <td class="short-text">&nbsp;</td>
                        <td class="medium-text accent">Rent</td>
                        <td><input type="text" name="txtRent" value="@rent" /></td>
                        <td>&nbsp;</td>
                    </tr>
                    <tr>
                        <td class="short-text">&nbsp;</td>
                        <td class="medium-text accent">Other Expenses</td>
                        <td><input type="text" name="txtOtherExpenses" value="@otherExpenses" /></td>
                        <td><input type="submit" name="btnCalculate" value="Calculate" /></td>
                    </tr>
                </table>
                <table style="width: 520px;">
                    <tr>
                        <td>----------------------------------------------------------------------------------------------</td>
                    </tr>
                </table>
                <table>
                    <tr>
                        <td class="short-text">&nbsp;</td>
                        <td style="width: 300px;" class="accent">Total Expenses</td>
                        <td><input type="text" name="txtTotalExpenses" value="@totalExpenses" /></td>
                    </tr>
                </table>
                <table>
                    <tr>
                        <td style="width: 355px;" class="accent">Net Income</td>
                        <td><input type="text" name="txtNetIncome" value="@netIncome" /></td>
                    </tr>
                </table>
            </form>
        </div>
    </body>
    </html>
  19. To execute the application, press Ctrl + F5:

    Converting a Value to Double

  20. Enter some values in the text boxes above the line, such as:
    All Revenues Sources: 1528665
    Salaries: 535775
    Supplies: 12545
    Rent: 12500
    Other Expenses: 327448

    Converting a Value to Double

  21. Click the Calculate button

    Converting a Value to Double

  22. Close the browser and return to your programming environment

Arithmetic

Absolute Values

The absolute value of a number x is x if the number is (already) positive. If the number is negative, its absolute value is its positive equivalent. To let you get the absolute value of a number, the Math static class is equipped with a method named Abs, which is overloaded in various versions. The syntax for the int and the double types are:

public static int Abs(int value);
public static double Abs(double value);

This method takes the argument whose absolute value must be found.

The Ceiling of a Number

Consider a floating-point number such as 12.155. This number is between integer 12 and integer 13:

Ceiling

In the same way, consider a number such as -24.06. As this number is negative, it is between -24 and -25, with -24 being greater.

In arithmetic, the ceiling of a number is the closest integer that is greater or higher than the number considered. In the first case, the ceiling of 12.155 is 13 because 13 is the closest integer greater than or equal to 12.155. The ceiling of -24.06 is 24.

To support the finding of a ceiling, the Math static class is equipped with a method named Ceiling that is overloaded with two versions. The syntax for a double value is:

public static double Ceiling(double a);

This method takes as argument a number or variable whose ceiling needs to be found. Here is an example:

@{
    double value1 = 155.55;
    double value2 = -24.06;

    double nbr1 = Math.Ceiling(value1));
    double nbr2 = Math.Ceiling(value2));
}

The Floor of a Number

Consider two floating numbers such as 128.44 and -36.72. The number 128.44 is between 128 and 129 with 128 being the lower. The number -36.72 is between -37 and -36 with -37 being the lower. The lowest but closest integer value of a number is referred to as its floor.

To assist you with finding the floor of a number, the Math static class provides the Floor() method. It is overloaded with two versions. The syntax for a double is:

public static double Floor(double d);

The Math.Floor() method takes the considered value as the argument and returns the integer that is less than or equal to the argument. Here is an example:

@{
    double value1 = 1540.25;
    double value2 = -360.04;

    double nbr1 = Math.Floor(value1);
    double nbr2 = Math.Floor(value2);
}

The Power of a Number

The power is the value of one number or expression raised to another number. This follows the formula:

value = xy

To support this operation, the Math static class is equipped with a method named Pow. Its syntax is:

public static double Pow(double x, double y);

This method takes two arguments. The first argument, x, is used as the base number to be evaluated. The second argument, y, also called the exponent, will raise x to this value.

Practical LearningPractical Learning: Introducing if...else if Conditions

  1. On the main menu of Microsoft Visual Studio, click File -> New -> Project...
  2. In the middle list, click ASP.NET Web Application (.NET Framework) and change the project Name to CompoundInterest1
  3. Click OK
  4. In the New ASP.NET Web Application dialog box, make sure Empty is selected and press Enter
  5. In the Solution Explorer, right-click CompoundInterest -> Add -> New Folder
  6. Type Content and press Enter
  7. In the Solution Explorer, right-click Content -> Add -> New Item...
  8. In the left frame of the Add New Item dialog box, click Style Sheet
  9. Change the file Name to Site
  10. Press Enter
  11. Create some styles as follows:
    .container {
        margin: auto;
        width:  520px; }
    
    table        { width:       100%;  }
    .accent      { font-weight: 600;   }
    .short-text  { width:       50px;  }
    .medium-text { width:       120px; }
  12. In the Solution Explorer, right-click CompoundInterest1 -> Add -> New Item...
  13. In the left list, make sure Web is selected and click Razor
  14. In the middle list, click Web Page (Razor v3)
  15. Set the name as Index
  16. Click Add
  17. Change the code as follows:
    <!DOCTYPE html>
    <html>
    <head>
    <title>Compound Interest</title>
    <link rel="stylesheet" href="~/Content/Site.css" type="text/css" />
    </head>
    <body>
    @{
        double periods = 0.00;
        double principal = 0.00;
        double interestRate = 0.00;
        string strFutureValue = "";
        string strInterestEarned = "";
    
        if (IsPost)
        {
            double frequency = 12.00;
            double futureValue = 0.00;
            double interestEarned = 0.00;
    
            principal = Convert.ToDouble(Request["txtPrincipal"]);
            interestRate = Convert.ToDouble(Request["txtInterestRate"]);
            periods = Convert.ToDouble(Request["txtPeriods"]);
            double per = periods / 100;
    
            futureValue = principal * Math.Pow((1.00 + (interestRate / frequency)), frequency * per);
            interestEarned = futureValue - principal;
    
            strFutureValue = futureValue.ToString("F");
            strInterestEarned = interestEarned.ToString("F");
        }
    }
    
    <div class="container">
        <h2>Compound Interest</h2>
    
        <form name="frmCompoundInterest" method="post">
            <table>
                <tr>
                    <td class="medium-text accent">Principal:</td>
                    <td><input type="text" name="txtPrincipal" value="@principal" /></td>
                </tr>
                <tr>
                    <td class="medium-text accent">Interest Rate:</td>
                    <td><input type="text" name="txtInterestRate" class="short-text" value="@interestRate" />%</td>
                </tr>
                    <tr>
                        <td class="accent">Periods:</td>
                        <td><input type="text" name="txtPeriods" class="short-text" value="@periods" /> Years</td>
                    </tr>
                    <td>&nbsp;</td>
                    <td><input type="submit" name="txtSubmit" class="medium-text" value=Calculate /></td>
                    <tr>
                        <td class="accent">Interest Earned:</td>
                        <td><input type="text" name="txtInterestEarned" value="@strInterestEarned" /></td>
                    </tr>
                    <tr>
                        <td class="accent">Future Value:</td>
                        <td><input type="text" name="txtFutureValue" value="@strFutureValue" /></td>
                    </tr>
                </table>
            </form>
        </div>
    </body>
    </html>   
  18. To execute the application, on the main menu, click Debug -> Start Without Debugging

    The Power of a Number

  19. In the Principal text box, type a number such as 5284.65
  20. In the Interest Rate text box, type a number such as 12.35

  21. In the Periods text box, type a natural number such as 5:

    The Power of a Number

  22. Click the Calculate button:

    The Power of a Number

  23. Close the form and return to your programming environment

The Exponential

To let you calculate the exponential value of a number, the Math static class provides the Exp() method. Its syntax is:

public static double Exp(double d);

If the value of of the argument x is less than -708.395996093 (approximately), the result is reset to 0 and qualifies as underflow. If the value of the argument x is greater than 709.78222656 (approximately), the result qualifies as overflow.

The Natural Logarithm

To calculate the natural logarithm of a number, you can call the Math.Log() method. It is provides in two versions. The syntax of one is:

public static double Log(double d);

Here is an example:

@{
    double log = 12.48D;

    double number = Math.Log(log));
}

The Base 10 Logarithm

The Math.Log10() method calculates the base 10 logarithm of a number. The syntax of this method is:

public static double Log10(double d);

The number to be evaluated is passed as the argument. The method returns the logarithm on base 10 using the formula:

y = log10x

which is equivalent to

x = 10y

Here is an example:

@{
    double log10 = 12.48D;

    double number = Math.Log10(log10));
}

The Logarithm of Any Base

The Math.Log() method provides another version whose syntax is:

public static double Log(double a, double newBase);

The variable whose logarithmic value will be calculated is passed as the first argument to the method. The second argument allows you to specify a base of your choice. The method uses the formula:

Y = logNewBasex

This is the same as

x = NewBasey

Here is an example of calling this method:

@{
    double logN = 12.48D;

    double number = Math.Log(logN, 4));
}

The Square Root

You can calculate the square root of a decimal positive number. To support this, the Math static class is equipped with a method named Sqrt whose syntax is:

public static double Sqrt(double d);

This method takes one argument as a positive floating-point number. After the calculation, the method returns the square root of x:

using System;

class Program
{
    static int Main()
    {
        double sqrt = 8025.73;

        double number = Math.Sqrt(sqrt));
	
        return 0;
    }
}

ApplicationPractical Learning: Finding the Square Root of a Number

  1. Save the following picture somewhere on your computer:

    Geometry - Equilateral Triangle Prism

  2. Start Microsoft Visual Studio
  3. On the main menu, click File -> New -> Project...
  4. In the middle list, click ASP.NET Web Application (.NET Framework) and set the project Name to Geometry06
  5. Click OK
  6. In the New ASP.NET Web Application dialog box, click Empty and click OK
  7. In the Solution Explorer, right-click Geometry06 -> Add -> New Folder
  8. Type Images and press Enter
  9. Copy the above triangle illustration and paste it in this images folder
  10. In the Solution Explorer, right-click Geometry06 -> Add -> New Folder
  11. Type Content and press Enter
  12. In the Solution Explorer, right-click Content -> Add -> New Item...
  13. In the left frame of the Add New Item dialog box, click Style Sheet
  14. Change the file Name to Site
  15. Press Enter
  16. Create some styles as follows:
    .container {
        margin: auto;
        width:  520px; }
    
    table        { width:       100%;  }
    .medium-text { width:       150px; }
  17. In the Solution Explorer, right-click Geometry06, position the mouse on Add, position the mouse on Add ASP.NET Folder, and click App_Code
  18. In the Solution Explorer, right-click App_Code -> Add -> Class...
  19. Change the name to EquilateralTriangle
  20. Press Enter
  21. Type the code as follows:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace Geometry06.App_Code
    {
        public class EquilateralTriangle
        {
            private double s;
            const double NumberOfSides = 3;
    
            public EquilateralTriangle(double side) => s = side;
    
            public double Side { get; set; }
            public double Perimeter => s * NumberOfSides;
            public double Height => s * Math.Sqrt(3) / 2;
            public double Area => s * s * Math.Sqrt(3) / 4;
            public double Inradius => s * Math.Sqrt(3) / 6;
            public double Circumradius => s / Math.Sqrt(3);
        }
    }
  22. In the Solution Explorer, right-click Geometry06 -> Add -> New Item...
  23. In the left list, expand Web and click Razor
  24. In the middle list, click Web Page (Razor v3)
  25. Set the name as Index
  26. Click Add
  27. Change the code as follows:
    <!DOCTYPE html>
    <html>
    <head>
    <title>Geometry: Triangle</title>
    <link href="~/Content/Site.css" type="text/css" rel="stylesheet" />
    </head>
    <body>
    @{
        double side = 0.00;
        Geometry06.App_Code.EquilateralTriangle et = new Geometry06.App_Code.EquilateralTriangle(0.00);
    
        if (IsPost)
        {
            side = Convert.ToDouble(Request["txtSide"]);
    
            et = new Geometry06.App_Code.EquilateralTriangle(side);
        }
    }
    
    <div class="container">
            <h2 style="text-align: center">Geometry - Equilateral Triangle</h2>
    
            <form name="frmGeometry" method="post">
                <table>
                    <tr>
                        <td style="width: 392px" rowspan="9">
                            <img src="~/Images/triangle2.png" width="391" height="315" alt="Geometry - Equilateral Triangle" border="1">
                        </td>
                        <td class="medium-text">Side:</td>
                        <td><input type="text" name="txtSide" value="@side" /></td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                        <td style="text-align: center"><input type="submit" name="btnSubmit" value="Calculate" /></td>
                    </tr>
                    <tr>
                        <td>Height:</td>
                        <td><input type="text" name="txtHeight" value="@et.Height" /></td>
                    </tr>
                    <tr>
                        <td>Perimeter:</td>
                        <td><input type="text" name="txtPerimeter" value="@et.Perimeter" /></td>
                    </tr>
                    <tr>
                        <td>Area:</td>
                        <td><input type="text" name="txt>Area" value="@et.Area" /></td>
                    </tr>
                    <tr>
                        <td>Inscribed Radius:</td>
                        <td><input type="text" name="txtInscribedRadius" value="@et.Inradius" /></td>
                    </tr>
                    <tr>
                        <td>Circumscribed Radius:</td>
                        <td><input type="text" name="txtBottomArea" value="@et.Circumradius" /></td>
                    </tr>
                </table>
            </form>
        </div>
    </body>
    </html>
  28. To execute the application to test it, on the main menu, click Debug -> Start Without Debugging:
    If you receive an error that states:
    "Feature 'Pattern Matching' is not available in C# 6. Please us language version 7 or greater."
    Then, on the main menu, click Tools -> NuGet Packet Manager -> Package Manager Console
    Type Install-Package Microsoft.Net.Compilers
    Press Enter.
    In the Solution Explorer, double-click the Web.config file to open it. Find the compilerOptions attributes. Set the value of lanversion to default:
    . . . compilerOptions="/langversion:default . . .

    Introducing Constructors with No Bodies

  29. In the Side text box, type a number such as 309.66

    Introducing Constructors with No Bodies

  30. Click the Calculate button:

    Introducing Constructors with No Bodies

  31. Close the browser and return to your programming environment

Trigonometry

Introduction

Measures

A circle is a group or series of distinct points drawn at an exact same distance from another point referred to as the center. The distance from the center C to one of these equidistant points is called the radius, R. The line that connects all of the points that are equidistant to the center is called the circumference of the circle. The diameter is the distance between two points of the circumference to the center; in other words, a diameter is double the radius.

To manage the measurements and other related operations, the circumference is divided into 360 portions. Each of these portions is called a degree. The unit used to represent the degree is the degree, written as ˚. Therefore, a circle contains 360 degrees, that is 360˚. The measurement of two points A and D of the circumference could have 15 portions of the circumference. In this case, this measurement would be represents as 15˚.

The distance between two equidistant points A and B is a round shape geometrically defined as an arc. An angle is the ratio of the distance between two points A and B of the circumference divided by the radius R. This can be written as:

Angle

Therefore, an angle is the ratio of an arc over the radius. Because an angle is a ratio and not a "physical" measurement, which means an angle is not a dimension, it is independent of the size of a circle. Obviously this angle represents the number of portions included by the three points. A better unit used to measure an angle is the radian or rad.

Arc

A cycle is a measurement of the rotation around the circle. Since the rotation is not necessarily complete, depending on the scenario, a measure is made based on the angle that was covered during the rotation. A cycle could cover part of the circle in which case the rotation would not have been completed. A cycle could also cover the whole 360˚ of the circle and continue there after. A cycle is equivalent to the radian divided by 2 * Pi.

The PI Constant

The letter п, also written as Pi, is a constant number used in various mathematical calculations. Its approximate value is 3.1415926535897932. The calculator of MicrosoftWindows represents it as 3.1415926535897932384626433832795.

To support the Pi constant, the Math class is equipped with a constant named PI.

A diameter is two times the radius. In geometry, it is written as 2R. In C++, it is written as 2 * R or R * 2 (because the multiplication is symmetric). The circumference of a circle is calculated by multiplying the diameter to Pi, which is 2Rп, or 2 * R * п or 2 * R * Pi.

A radian is 2Rп/R radians or 2Rп/R rad, which is the same as 2п rad or 2 * Pi.

To perform conversions between the degree and the radian, you can use the formula:

360˚ = 2п rad which is equivalent to 1 rad = 360˚ / 2п = 57.3˚.

The Cosine of a Value

Consider the following geometric figure:

Trigonometry

Consider AB the length of A to B, also referred to as the hypotenuse. Also consider AC the length of A to C which is the side adjacent to point A. The cosine of the angle at point A is the ratio AC/AB. That is, the ratio of the adjacent length, AC, over the length of the hypotenuse, AB:

Cosine

The returned value, the ratio, is a double-precision number between -1 and 1.

To calculate the cosine of an angle, the Math class provides the Cos() method. Its syntax is:

public static double Cos(double d);

Here is an example:

@{
    int number = 82;

    double nbr = Math.Cos(number));
}

Consider AB the length of A to B, also called the hypotenuse to point A. Also consider CB the length of C to B, which is the opposite side to point A. The sine represents the ratio of CB/AB; that is, the ratio of the opposite side, CB over the hypotenuse AB.

To calculate the sine of a value, you can call the Sin() method of the Math class. Its syntax is:

public static double Sin(double a);

Here is an example:

@{
    double number = 82.55;

    double nbr = Math.Sin(number));
}

Tangents

Consider AC the length of A to C. Also consider BC the length of B to C. The tangent is the result of BC/AC; that is, the ratio of BC over AC. To assist you with calculating the tangent of of a number, the Math class is equipped with a method named Tan whose syntax is:

public static double Tan(double a);

The Arc Tangent

Consider BC the length of B to C. Also consider AC the length of A to C. The arc tangent is the ratio of BC/AC. To calculate the arc tangent of a value, you can use the Math.Atan()method. Its syntax is

public static double Atan(double d);

Practical LearningPractical Learning: Ending the Lesson


Previous Copyright © 2001-2019, FunctionX Next