The Nullity of an Object

If an Object Is Null

The equality operator is used to find out whether an object is equal to a certain value. Because an application may deal with various types of values, an alternative is to find out whether a certain object is compatible with another. To let you perform this comparison, the C# language provides a Boolean operator named is. The formula to find out whether an object is null is:

instance-object is null

You can use an if conditional statement to obtain this information.

Practical LearningPractical Learning: Introducing Nullity of Objects

  1. Start Microsoft Visual Studio.
    Create a C# Console App that supports the .NET 8.0 (Long-Term Support).
    Name the project as Chemistry8
  2. To create a new folder, in the Solution Explorer, right-click Chemistry8 -> Add -> New Folder
  3. Type Models as the name of the new folder
  4. In the Solution Explorer, right-click , right-click Models -> Add -> New Class...
  5. Change the file name to Element
  6. Click Add
  7. Change the document and class as follows:
    namespace Chemistry8.Models
    {
        public enum Phase { Gas, Liquid, Solid, Unknown }
    
        internal class Element(int number, string symbol, string name, double mass, Phase phase)
        {
            internal string ElementName
            {
                get { return name; }
            }
            internal string Symbol
            {
                get { return symbol; }
            }
            internal int AtomicNumber
            {
                get { return number; }
            }
            internal double AtomicWeight
            {
                get { return mass; }
            }
            internal Phase Phase
            {
                get { return phase; }
    
            }
        }
    }
  8. In the Solution Explorer, right-click Program.cs and click Rename
  9. Type PeriodicTable (to get PeriodicTable.cs) and press enter
  10. Change the PeriodicTable.cs document as follows:
    using Chemistry6.Models;
    using static System.Console;
    
    void Present(Element obj)
    {
        if (obj is null)
        {
            WriteLine("The chemical element is not available.");
            WriteLine("=======================================");
        }
        else
        {
            WriteLine("Chemistry");
            WriteLine("--------------------------");
            WriteLine("Symbol:        " + obj.Symbol);
            WriteLine($"Atomic Number: {obj.AtomicNumber}");
            WriteLine("Element Name:  " + obj.ElementName);
            WriteLine($"Atomic Weight: " + obj.AtomicWeight);
            WriteLine("Phase/State:   " + obj.Phase);
            WriteLine("==========================");
        }
    }
    
    Element Prepare()
    {
        Element h  = new Element(1, "H", "Hydrogen", 1.008, Phase.Gas);
        Element he = new Element(2, "He", "Helium", 4.002602, Phase.Gas);
        Element li = new Element(3, "Li", "Lithium", 6.94, Phase.Solid);
        Element be = new Element(4, "Be", "Beryllium", 9.0121831, Phase.Solid);
        Element b  = new Element(5, "B", "Boron", 10.81, Phase.Solid);
        Element c  = new(name: "Carbon", mass: 12.011, symbol: "C", number: 6, phase: Phase.Solid);
        Element n  = new(7, "N", "Nitrogen", 14.007, Phase.Gas);
        Element o  = new(8, "O", "Oxygen", 15.999, Phase.Gas);
        Element f  = new(9, "F", "Fluorine", 18.998, Phase.Gas);
        Element ne = new(10, "Ne", "Neon", 20.180, Phase.Gas);
        Element na = new(11, "Na", "Sodium", 22.990, Phase.Solid);
        Element mg = new(12, "Mg", "Magnesium", 24.305, Phase.Solid);
        Element al = new(phase : Phase.Solid, symbol : "Al", number : 13, name : "Aluminium", mass : 26.982);
        Element si = new(14, "Si", "Silicon", 28.085, Phase.Solid);
        Element p  = new(symbol : "P", mass : 30.974, number : 15, name : "Phosphorus", phase : Phase.Solid);
        Element s  = new(16, "S", "Sulfur", 32.06, Phase.Solid);
        Element cl = new(17, "Cl", "Chlorine", 35.45, Phase.Gas);
        Element ar = new Element(18, "Ar", "Argon", 39.948, Phase.Gas);
        Element k  = new Element(phase: Phase.Solid, number: 19, symbol: "K", name: "Potassium", mass: 39.098);
        Element ca = new(20, "Ca", "Calcium", 40.078, Phase.Solid);
        Element sc = new(number: 21, symbol: "Sc", phase: Phase.Solid, name: "Scandium", mass: 44.956);
        Element ti = new(name: "Titanium", number: 22, symbol: "Ti", mass: 47.867, phase: Phase.Solid);
        Element v  = new Element(number : 23, symbol : "V", phase : Phase.Solid, mass : 50.942, name : "Vanadium");
        Element cr = new(number: 24, symbol: "Cr", phase: Phase.Solid, name: "Chromium", mass: 51.996);
        Element mn = new Element(name: "Manganese", number: 25, symbol: "Mn", mass: 54.938, phase: Phase.Solid);
        Element fe = new Element(mass: 55.845, symbol: "Fe", name: "Iron", number: 26, phase: Phase.Solid);
        Element co = new Element(symbol: "Co", number: 27, name: "Cobalt", mass: 58.933, phase: Phase.Solid);
        Element ni = new Element(number: 28, symbol: "Ni", mass: 58.6934, phase: Phase.Solid, name: "Nickel");
        Element cu = new Element(phase: Phase.Solid, number: 29, name: "Copper", symbol: "Cu", mass: 63.546);
        Element zn = new Element(30, "Zn", "Zinc", 65.38, Phase.Solid);
        Element ga = new Element(31, "Ga", "Gallium", 69.723, Phase.Solid);
        Element ge = new Element(32, "Ge", "Germanium", 72.630, Phase.Solid);
    
        return ge;
    }
    
    Element? elm = null;
    
    WriteLine("Presenting a chemical element...");
    Present(elm!);
    
    elm = Prepare();
    
    WriteLine("Presenting a chemical element...");
    Present(elm);
  11. To execute, on the main menu, click Debug -> Start Without Debugging:
    Presenting a chemical element...
    The chemical element is not available.
    =======================================
    Presenting a chemical element...
    Chemistry
    --------------------------
    Symbol:        Ge
    Atomic Number: 32
    Element Name:  Germanium
    Atomic Weight: 72.63
    Phase/State:   Solid
    ==========================
    
    Press any key to close this window . . .
  12. Return to your programming environment

Checking for Non-Nullity

As opposed to checking the nullity of an object, you may be interested to know that an object is not null. To assist you with this, besides the != operator, the C# language provides an operator named not. You can use it to find out whether an argument or object is not null.

Practical LearningPractical Learning: Comparing an Object for Non-Nullity

  1. Change the PeriodicTable class as follows:
    using Chemistry8.Models;
    using static System.Console;
    
    void Present(Element obj)
    {
        if (obj is not null)
        {
            WriteLine("Chemistry");
            WriteLine("--------------------------");
            WriteLine("Symbol:        " + obj.Symbol);
            WriteLine($"Atomic Number: {obj.AtomicNumber}");
            WriteLine("Element Name:  " + obj.ElementName);
            WriteLine($"Atomic Weight: " + obj.AtomicWeight);
            WriteLine("Phase/State:   " + obj.Phase);
            WriteLine("==========================");
        }
        else
        {
            WriteLine("The chemical element is not available.");
            WriteLine("=======================================");
        }
    }
    
    Element Prepare()
    {
        Element h  = new Element(1, "H", "Hydrogen", 1.008, Phase.Gas);
        Element he = new Element(2, "He", "Helium", 4.002602, Phase.Gas);
        Element li = new Element(3, "Li", "Lithium", 6.94, Phase.Solid);
        Element be = new Element(4, "Be", "Beryllium", 9.0121831, Phase.Solid);
        Element b  = new Element(5, "B", "Boron", 10.81, Phase.Solid);
        Element c  = new(name: "Carbon", mass: 12.011, symbol: "C", number: 6, phase: Phase.Solid);
        Element n  = new(7, "N", "Nitrogen", 14.007, Phase.Gas);
        Element o  = new(8, "O", "Oxygen", 15.999, Phase.Gas);
        Element f  = new(9, "F", "Fluorine", 18.998, Phase.Gas);
        Element ne = new(10, "Ne", "Neon", 20.180, Phase.Gas);
        Element na = new(11, "Na", "Sodium", 22.990, Phase.Solid);
        Element mg = new(12, "Mg", "Magnesium", 24.305, Phase.Solid);
        Element al = new(phase : Phase.Solid, symbol : "Al", number : 13, name : "Aluminium", mass : 26.982);
        Element si = new(14, "Si", "Silicon", 28.085, Phase.Solid);
        Element p  = new(symbol : "P", mass : 30.974, number : 15, name : "Phosphorus", phase : Phase.Solid);
        Element s  = new(16, "S", "Sulfur", 32.06, Phase.Solid);
        Element cl = new(17, "Cl", "Chlorine", 35.45, Phase.Gas);
        Element ar = new Element(18, "Ar", "Argon", 39.948, Phase.Gas);
        Element k  = new Element(phase: Phase.Solid, number: 19, symbol: "K", name: "Potassium", mass: 39.098);
        Element ca = new(20, "Ca", "Calcium", 40.078, Phase.Solid);
        Element sc = new(number: 21, symbol: "Sc", phase: Phase.Solid, name: "Scandium", mass: 44.956);
        Element ti = new(name: "Titanium", number: 22, symbol: "Ti", mass: 47.867, phase: Phase.Solid);
        Element v  = new Element(number : 23, symbol : "V", phase : Phase.Solid, mass : 50.942, name : "Vanadium");
        Element cr = new(number: 24, symbol: "Cr", phase: Phase.Solid, name: "Chromium", mass: 51.996);
        Element mn = new Element(name: "Manganese", number: 25, symbol: "Mn", mass: 54.938, phase: Phase.Solid);
        Element fe = new Element(mass: 55.845, symbol: "Fe", name: "Iron", number: 26, phase: Phase.Solid);
        Element co = new Element(symbol: "Co", number: 27, name: "Cobalt", mass: 58.933, phase: Phase.Solid);
        Element ni = new Element(number: 28, symbol: "Ni", mass: 58.6934, phase: Phase.Solid, name: "Nickel");
        Element cu = new Element(phase: Phase.Solid, number: 29, name: "Copper", symbol: "Cu", mass: 63.546);
        Element zn = new Element(30, "Zn", "Zinc", 65.38, Phase.Solid);
        Element ga = new Element(31, "Ga", "Gallium", 69.723, Phase.Solid);
        Element ge = new Element(32, "Ge", "Germanium", 72.630, Phase.Solid);
        Element @as = new Element(33, "As", "Arsenic", 74.921_595, Phase.Solid);
    
        return @as;
    }
    
    Element? elm = null;
    
    WriteLine("Presenting a chemical element...");
    Present(elm!);
    
    elm = Prepare();
    
    WriteLine("Presenting a chemical element...");
    Present(elm);
  2. To execute, on the main menu, click Debug -> Start Without Debugging:
    Presenting a chemical element...
    The chemical element is not available.
    =======================================
    Presenting a chemical element...
    Chemistry
    --------------------------
    Symbol:        As
    Atomic Number: 33
    Element Name:  Arsenic
    Atomic Weight: 74.921595
    Phase/State:   Solid
    ==========================
    
    Press any key to close this window . . .
  3. Press Enter to close the window and return to your programming environment
  4. Create a new Console App that supports .NET 8.0 (Long-Term support) and named StellarWaterPoint4
  5. Change the Program.cs document as follows:
    using static System.Console;
    
    (Classification iClass, string name) IdentifyAccount(string aType)
    {
        if (aType == "1")
            return (Classification.Residential, "Residential Household");
        else if (aType == "2")
            return (Classification.NonProfit, "Social/Non-Profit Organization");
        else if (aType == "3")
            return (Classification.WaterIntensive, "Water Intensive Business (Laudromat, Hair Salon, etc)");
        else if (aType == "4")
            return (Classification.GeneralUse, "General Business, Government");
        else
            return (Classification.Other, "Other");
    }
    
    double Subtract(in double a, in double b)
    {
        return a - b;
    }
    
    double CalculateHCFTotal(double gallons)
    {
        return gallons / 748.00;
    }
    
    (double a, double b, double c) EvaluateTherms(in double hcf)
    {
        double first;
        double next;
        double above;
    
        if (hcf is > 40.00)
        {
            first = 25.00 * 1.5573;
            next = 15.00 * 1.2264;
            above = (hcf - 40.00) * 0.9624;
        }
        else if (hcf > 25.00)
        {
            first = 25.00 * 1.5573;
            next = (hcf - 25.00) * 1.2264;
            above = 0.00;
        }
        else
        {
            first = hcf * 1.5573;
            next = 0.00;
            above = 0.00;
        }
    
        return (first, next, above);
    }
    
    double EvaluateSewerCharges(in double charges, in Classification @class)
    {
        double result;
    
        switch (@class)
        {
            case Classification.Residential:
                result = charges * 0.2528;
                break;
            case Classification.NonProfit:
                result = charges * 0.0915;
                break;
            case Classification.WaterIntensive:
                result = charges * 1.5865;
                break;
            case Classification.GeneralUse:
                result = charges * 0.6405;
                break;
            default:
                result = charges * 1.2125;
                break;
        }
    
        return result;
    }
    
    double EvaluateEnvironmentCharges(in double charges, in Classification @class)
    {
        double result;
    
        switch (@class)
        {
            case Classification.Residential:
                result = charges * 0.0025;
                break;
            case Classification.NonProfit:
                result = charges * 0.0015;
                break;
            case Classification.WaterIntensive:
                result = charges * 0.0105;
                break;
            case Classification.GeneralUse:
                result = charges * 0.0045;
                break;
            default:
                result = charges * 0.0125;
                break;
        }
    
        return result;
    }
    
    double EvaluateServiceCharges(in double charges, in Classification @class)
    {
        double result;
    
        switch (@class)
        {
            case Classification.Residential:
                result = charges * 0.0328;
                break;
            case Classification.NonProfit:
                result = charges * 0.0115;
                break;
            case Classification.WaterIntensive:
                result = charges * 0.1015;
                break;
            case Classification.GeneralUse:
                result = charges * 0.0808;
                break;
            default:
                result = charges * 0.1164;
                break;
        }
    
        return result;
    }
    
    double AddTriple(in (double a, double b, double c) triple)
    {
        return triple.a + triple.b + triple.c;
    }
    
    double Add3(in double a, in double b, in double c)
    {
        return a + b + c;
    }
    
    double Add4(in double a, in double b, in double c, in double d)
    {
        return a + b + c + d;
    }
    
    double CalculateLocalTaxes(in double charges, in Classification @class)
    {
        double result;
    
        switch (@class)
        {
            case Classification.Residential:
                result = charges * 0.115;
                break;
            case Classification.NonProfit:
                result = charges * 0.085;
                break;
            case Classification.WaterIntensive:
                result = charges * 0.825;
                break;
            case Classification.GeneralUse:
                result = charges * 0.315;
                break;
            default:
                result = charges * 0.625;
                break;
        }
    
        return result;
    }
    
    double CalculateStateTaxes(in double charges, in Classification @class)
    {
        double result;
    
        switch (@class)
        {
            case Classification.Residential:
                result = charges * 0.035;
                break;
            case Classification.NonProfit:
                result = charges * 0.015;
                break;
            case Classification.WaterIntensive:
                result = charges * 0.105;
                break;
            case Classification.GeneralUse:
                result = charges * 0.065;
                break;
            default:
                result = charges * 0.815;
                break;
        }
    
        return result;
    }
    
    string GetCustomerType()
    {
        WriteLine("Stellar Water Point");
        WriteLine("===========================================================");
        WriteLine("To prepare an invoice, enter the following information");
        WriteLine("Types of Accounts");
        WriteLine("1. Residential Household");
        WriteLine("2. Social/Non-Profit Organization");
        WriteLine("3. Water Intensive Business (Laudromat, etc)");
        WriteLine("4. General Business");
        WriteLine("0. Other");
        Write("Enter Type of Account:         ");
        string answer = ReadLine()!;
        WriteLine("-----------------------------------------------------------");
    
        return answer;
    }
    
    int ReadCounter(out int start, out int end)
    {
        end   = 0;
        start = 0;
    
        try
        {
            Write("Counter Reading Start:         ");
            start = int.Parse(ReadLine()!);
        }
        catch(Exception ex) when (ex is FormatException fex)
        {
            WriteLine("The counter reading start value you entered is not valid.");
            WriteLine("The error produced is: " + fex.Message);
        }
    
        try
        {
            Write("Counter Reading End:           ");
            end = int.Parse(ReadLine()!);
        }
        catch (Exception ex) when (ex is FormatException fex)
        {
            WriteLine("The counter reading end value you entered is not valid.");
            WriteLine("The error produced is: " + fex.Message);
        }
    
        return 1_000;
    }
    
    int readingEnd;
    int readingStart;
    string acntType = GetCustomerType();
    
    _ = ReadCounter(out readingStart, out readingEnd);
    
    var account = IdentifyAccount(acntType);
    double gallons = Subtract(readingEnd, readingStart);
    double HCFValue = CalculateHCFTotal(gallons);
    (double First25Therms, double Next15Therms, double Above40Therms) therms = EvaluateTherms(HCFValue);
    double water = AddTriple(therms);
    double sewer = EvaluateSewerCharges(water, account.iClass);
    double environmentCharges = EvaluateEnvironmentCharges(water, account.iClass);
    double serviceCharges = EvaluateServiceCharges(water, account.iClass);
    double totalCharges = Add4(water, sewer, environmentCharges, serviceCharges);
    double localTaxes = CalculateLocalTaxes(totalCharges, account.iClass);
    double stateTaxes = CalculateStateTaxes(totalCharges, account.iClass);
    double amountDue = Add3(totalCharges, localTaxes, stateTaxes);
    
    WriteLine("===========================================================");
    WriteLine("Stellar Water Point - Customer Invoice");
    WriteLine("Account Type:               {0}", account.name);
    WriteLine("===========================================================");
    WriteLine("Meter Reading");
    WriteLine("-----------------------------------------------------------");
    WriteLine("Counter Reading Start:      {0,10}", readingStart);
    WriteLine("Counter Reading End:        {0,10}", readingEnd);
    WriteLine("Total Gallons Consumed:     {0,10}", gallons);
    WriteLine("===========================================================");
    WriteLine("Therms Evaluation");
    WriteLine("-----------------------------------------------------------");
    WriteLine("HCF Total:                  {0,10:n}", HCFValue);
    WriteLine("First 35 Therms (* 1.5573): {0,10:n}", therms.First25Therms);
    WriteLine("First 20 Therms (* 1.2264): {0,10:n}", therms.Next15Therms);
    WriteLine("Above 40 Therms (* 0.9624): {0,10:n}", therms.Above40Therms);
    WriteLine("-----------------------------------------------------------");
    WriteLine("Water Usage Charges:        {0,10:n}", water);
    WriteLine("Sewer Charges:              {0,10:n}", sewer);
    WriteLine("===========================================================");
    WriteLine("Bill Values");
    WriteLine("-----------------------------------------------------------");
    WriteLine("Environment Charges:        {0,10:n}", environmentCharges);
    WriteLine("Service Charges:            {0,10:n}", serviceCharges);
    WriteLine("Total Charges:              {0,10:n}", totalCharges);
    WriteLine("Local Taxes:                {0,10:n}", localTaxes);
    WriteLine("State Taxes:                {0,10:n}", stateTaxes);
    WriteLine("-----------------------------------------------------------");
    WriteLine("Amount Due:                 {0,10:n}", amountDue);
    WriteLine("===========================================================");
    
    internal enum Classification { Residential, NonProfit, WaterIntensive, GeneralUse, Other }
  6. To execute, on the main menu, click Debug -> Start Without Debugging
  7. When requested, for the type of account, type 1 and press Enter
  8. For the Counter Reading Start, type 9622 and press Enter
  9. For the Counter Reading End, type 28927 and press Enter
    Stellar Water Point
    ===========================================================
    To prepare an invoice, enter the following information
    Types of Accounts
    1. Residential Household
    2. Social/Non-Profit Organization
    3. Water Intensive Business (Laudromat, etc)
    4. General Business
    0. Other
    Enter Type of Account:         1
    -----------------------------------------------------------
    Counter Reading Start:         9622
    Counter Reading End:           28927
    ===========================================================
    Stellar Water Point - Customer Invoice
    Account Type:               Residential Household
    ===========================================================
    Meter Reading
    -----------------------------------------------------------
    Counter Reading Start:            9622
    Counter Reading End:             28927
    Total Gallons Consumed:          19305
    ===========================================================
    Therms Evaluation
    -----------------------------------------------------------
    HCF Total:                       25.81
    First 35 Therms (* 1.5573):      38.93
    First 20 Therms (* 1.2264):       0.99
    Above 40 Therms (* 0.9624):       0.00
    -----------------------------------------------------------
    Water Usage Charges:             39.92
    Sewer Charges:                   10.09
    ===========================================================
    Bill Values
    -----------------------------------------------------------
    Environment Charges:              0.10
    Service Charges:                  1.31
    Total Charges:                   51.43
    Local Taxes:                      5.91
    State Taxes:                      1.80
    -----------------------------------------------------------
    Amount Due:                      59.14
    ===========================================================
    
    Press any key to close this window . . .
  10. Return to your programming environment

Primitive Patterns

Introduction

A pattern is a group of things that are copies or imitations of one after another. This characteristic can be replicated in application programming in various ways.

A Single Type Pattern

When you declare a variable in a C# application, you have many options. As we have seen so far, you can use a data type (int, double, bool, etc), a name of a class (string, etc), the var keyword, the object type, etc. You can also get a value by other means, such as a value from a function or a method (or an event, a delegate, etc; things we haven't studied yet). In some cases, before performing an operation on a variable, you may want to first check the type of that variable. To assist you with this, the C# language provides the is operator. This operator is used with a conditional statement. The primary formula to follow is:

condition-keyword(existing-variable is type new-variable)
    statement(s);

The condition-keyword is one of the conditional keywords used in C#. Normally, the primary keyword candidate here is if. The condition-keyword is followed by the usual parentheses. In the parentheses, access the variable that holds the value for which you want to check the type. This is followed by the required is keyword. After that, specify the data type that you are checking, followed by a name for a new variable. After the parentheses, you can create one or more statements that use the variable. In the statement(s), you can use the existing variable. Normally, the goal of using a type pattern is to check the type of a variable. Here is an example:

using static System.Console;

WriteLine("Provide the following pieces of information.");
WriteLine("--------------------------------------------");
Write("Employee Name: ");
object strEmployeeName = ReadLine()!;
Write("Hourly Salary: ");
double hSalary = double.Parse(ReadLine()!);

if(hSalary is double sal)
{
    WriteLine("============================================");
    WriteLine("Payroll Summary");
    WriteLine("--------------------------------------------");
    WriteLine("Employee Name:      {0}", strEmployeeName);
    WriteLine("Hourly Salary:      {0}", hSalary);
    WriteLine("============================================");
}

Here is an example of running the application:

Provide the following pieces of information.
--------------------------------------------
Employee Name:   Jules Kenmoe
Hourly Salary:   22.48
============================================
Employee Record
--------------------------------------------
Employee Name:   Jules Kenmoe
Hourly Salary:   22.48
============================================

Press any key to close this window . . .

If you use the existing-variable in the statement(s), you can use it only for its name as a variable. If the variable is holding a numeric value, you cannot involve it in a numeric operation. Normally, the type pattern is used to check a type you are expecting. If that type is the one you want, the new-variable then holds the value of the existing-variable. As a result, you can use the new-variable in your statement(s) and you can involve it in any operation appropriate for its type. Here is an example:

using static System.Console;

WriteLine("Provide the following pieces of information.");
WriteLine("--------------------------------------------");
Write("Employee Name: ");
object strEmployeeName = ReadLine()!;
Write("Hourly Salary: ");
object hSalary = double.Parse(ReadLine()!);

if (hSalary is double sal)
{
    WriteLine("============================================");
    WriteLine("Payroll Summary");
    WriteLine("--------------------------------------------");
    WriteLine("Employee Name:      {0}", strEmployeeName);
    WriteLine("Hourly Salary:      {0}", sal);
    WriteLine("Base Weekly Salary: {0:f}", sal * 40.00);
    WriteLine("============================================");
}

Here is an example of running the application:

Provide the following pieces of information.
--------------------------------------------
Employee Name:   Jules Kenmoe
Hourly Salary:   22.48
============================================
Employee Record
--------------------------------------------
Employee Name:   Jules Kenmoe
Hourly Salary:   22.48
Base Weekly Salary: 899.20
============================================

Press any key to close this window . . .

Multi-Type Patterns

In the above code, we checked the type of only one variable. You can check the types of more than one variable. To do this, in the parentheses of the conditional statement, use a conjunction between two conditions. For the conjunction, use the && operator instead of a single &. Here is an example:

using static System.Console;

WriteLine("Provide the following pieces of information.");
WriteLine("--------------------------------------------");
Write("Employee Name: ");
object strEmployeeName = ReadLine()!;
Write("Hourly Salary: ");
object hSalary = double.Parse(ReadLine()!);
WriteLine("--------------------------------------------");
WriteLine("Employment Status");
WriteLine("1. Full Time");
WriteLine("2. Part Time");
WriteLine("--------------------------------------------");
Write("Enter Employment Status: ");
object selection = int.Parse(ReadLine()!);

if(hSalary is double sal && selection is int emplStatus)
{
    WriteLine("============================================");
    WriteLine("Payroll Summary");
    WriteLine("--------------------------------------------");
    WriteLine("Employee Name:      {0}", strEmployeeName);
    if(emplStatus == 1)
        WriteLine("Employment Status:  Full-Time");
    else
        WriteLine("Employment Status:  Part-Time");
    WriteLine("Hourly Salary       {0}", sal);
    WriteLine("Base Weekly Salary: {0:f}", sal * 40.00);
    WriteLine("============================================");
}

Here is an example of running the application:

Provide the following pieces of information.
--------------------------------------------
Employee Name: Raymond Keller
Hourly Salary: 18.27
--------------------------------------------
Employment Status
1. Full Time
2. Part Time
--------------------------------------------
Enter Employment Status: 6
============================================
Payroll Summary
--------------------------------------------
Employee Name:      Raymond Keller
Employment Status:  Part-Time
Hourly Salary       18.27
Base Weekly Salary: 730.80
============================================

Press any key to close this window . . .

Practical LearningPractical Learning: Ending the Lesson


Previous Copyright © 2001-2024, FunctionX Thursday 07 December 2024, 08:06 Next