Fundamentals of Classes

Introduction

A class is a list of the characteristics that describe an object and/or the behaviors of an object.

Creating a Class

To create a class for an ASP.NET Core Web application, you have many options. To create a simple class, you can start a document. In the document, type the class keyword followed by a name and curly brackets. Here is an example:

class House
{

}

To make sure the class can be accessed throughout the project, start it with the public keyword. Here is an example:

public class House
{

}

Specify the name of the file. This would also be the name of the class. The name of the class follows the rules established in C#. When you have specified the name, click Add.

Managing Classes

If you are using Microsoft Visual Studio when developing your projects, you can use the studio's many tools to manage your classes. This management includes the Types combo box, the Members combo box, the Window menu, the options in the Solution Explorer, the options available when right-clicking the Code Editor, etc

A Class in a Razor Page

You can create a class in a Razor Page. To start a class in a Razor Page, create a section that starts with @functions{ and ends with }. In the section between { and }, create your class. This can be done as follows:

@functions{
    class House { }
}

In the same way, you can create as many classes as you want in your project. You can create many classes in the same document or in an @functions{} section. Here is an example:

@functions{
    class House {}

    class Person {}

    class MortgageInformation {}
}

Variables in a Class

You can declare a variable in a class. The declaration is done the same way it is done outside a class: a data type, a name, and a semi-colon. A variable in a class is called a field. Here is an example:

@functions{
    public class WaterMeter
    {
        int meterId; 
    }
}

In the same way, you can declare as many variables as you need in a class.

Using an Object

Creating an Object

To use a class, you must create an object, which is the same as declaring a variable of the class type. To create an object, type the name of a class followed by a name for the object. Here is an example:

Vehicle sedan;

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 another class.

An Object in a Razor Page

You can declare a variable of a class 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
    {
        string price;
    }
}

Options on Declaring a Variable of a Class

We have just seen that, to declare a variable of a class, you can use the name of the class, a name for the variable, the == operator, the new keyword, the name of the class, parentheses, and a semicolon. As one way to shorten the declaration, after the new keyword, you can omit the name of the class. Here is an example:

@{
    WaterMeter meter = new();
}

@functions{
    public class WaterMeter
    {
        int meterId;
    }
}

As another option, you can start the declaration with the var or the dynamic keyword instead of the name of the class but you must use the name of the class after the new keyword. Here is an example:

@{
    var meter = new WaterMeter();
}

@functions{
    public class WaterMeter
    {
        int meterId;
    }
}

Accessing the Variables in a Class

As mentioned in our introduction, a class is a list of the characteristics of an object. Those characteristics are created in the class as fields. When declaring a variable of a class, you must access those fields to actually describe the object.

To access a field of a class outside the class, type the name of the object, a period, and the desired member of the class. By default, when you create a field by declaring a variable in the body of a class, that field is automatically private and cannot be accessed outside the class. If you want to access a member outside the class, you must mark that member with the public (or the internal) keyword. Here is an example:

@functions{
    public class Vehicle
    {
        public double price;
    }
}

Initializing a Field

If you have a variable in a class, you must give it a value before using. One way to do this is to immediately assign a value to the field when declaring the variable. This is referred to as initializing it. Here is an example:

@functions{
    public class Exercise
    {
        public int number = 10_000;
    }
}

Displaying the Value of a Field

To display the value of a field in a webpage, in an HTML tag, type the @ symbol, the name of the object, a period, and the name of the field. Here is an example:

@page
@model Variables.Pages.WaterMeters.CreateModel
@{
    WaterMeter waterMeter = new();
}

@functions{
    public class WaterMeter
    {
        public int    meterId     = 2;
        public string meterNumber = "938-75-869";
        public string make        = "Stanford Trend";
        public string model       = "266G";
        public string meterSize   = "1 1/2 Inches";
    }
}

<h1 class="ta-center fw-bold common-font">Water Meter Setup</h1>

<hr />

<table>
    <tr>
        <td class="fw-bold">Meter Id:</td>
        <td>@waterMeter.meterId</td>
    </tr>
    <tr>
        <td class="fw-bold">Meter #:</td>
        <td>@waterMeter.meterNumber</td>
    </tr>
    <tr>
        <td class="fw-bold">Make:</td>
        <td>@waterMeter.make</td>
    </tr>
    <tr>
        <td class="fw-bold">Model:</td>
        <td>@waterMeter.model</td>
    </tr>
    <tr>
        <td class="fw-bold">Meter Size:</td>
        <td>@waterMeter.meterSize</td>
    </tr>
</table></p>

To display the value of a field in a Web form, you can use the value attribute of an HTML tag. In the double-quotes assigned to that attribute, type the @ symbol, the name of the object, a period, and the name of the field. Here is an example:

@page
@model Variables.Pages.WaterMeters.CreateModel
@{
    WaterMeter waterMeter = new();
}

@functions{
    public class WaterMeter
    {
        public int    meterId     = 2;
        public string meterNumber = "938-75-869";
        public string make        = "Stanford Trend";
        public string Model       = "266G";
        public string meterSize   = "1 1/2 Inches";
    }
}

<h1 class="ta-center fw-bold common-font">Water Meter Setup</h1>

<hr />

@using (Html.BeginForm())
{
    <div class="form-holder common-font">
        <div class="row g-3 align-items-center">
            <div class="col-md-4">
                <label class="col-form-label">Meter Id:</label>
            </div>
            <div class="col-md-8">
                <input class="form-control" value="@waterMeter.meterId" />
            </div>
        </div>

        <hr />
        <div class="row g-3 align-items-center">
            <div class="col-md-4">
                <label class="col-form-label">Meter #:</label>
            </div>
            <div class="col-md-8">
                <input class="form-control" value="@waterMeter.meterNumber" />
            </div>
        </div>

        <hr />
        <div class="row g-3 align-items-center">
            <div class="col-md-4">
                <label class="col-form-label">Make:</label>
            </div>
            <div class="col-md-8">
                <input class="form-control" value="@waterMeter.make" />
            </div>
        </div>

        <hr />
        <div class="row g-3 align-items-center">
            <div class="col-md-4">
                <label class="col-form-label">Model:</label>
            </div>
            <div class="col-md-8">
                <input class="form-control" value="@waterMeter.Model" />
            </div>
        </div>

        <hr />
        <div class="row g-3 align-items-center">
            <div class="col-md-4">
                <label class="col-form-label">Meter Size:</label>
            </div>
            <div class="col-md-8">
                <input class="form-control" value="@waterMeter.meterSize" />
            </div>
        </div>
    </div>
}

Introduction to Namespaces

Introduction

A namespace is a virtual container in which some things, such as classes, are created.

Creating a Namespace

To create a namespace, start a code file. Type namespace Follow by a name, followed by curly brackets {} that represent the body of the namespace. Here is an example:

namespace Business
{
}

In the body of the namespace, that is, between the curly brackets of the namespace, you can type the code that creates the item(s) you want. Here is an example of a class created inside a namespace. :

namespace Business
{
    public class House
    {
    }
}

In the same way, you can create other items, such as classes, in the body of the namespace. The items you create in a namespace are referred to as members of the namespace.

Accessing the Members of a Namespace

After creating a member in a namespace, you can access that member. If you are writing code from another member of the namespace, you can use simply use the name of that member. If you are working outside that namespace, you can use the period operator to access the member.

Creating and Using Many Namespaces

You can create one or many namespaces in the same document. You can also create namespaces in various documents. You can create one or more namespaces inside a namespace (which is referred to as nesting the namespace). Here is an example:

namespace Business
{
    public class House
    {
        
    }

    namespace Dealership
    {
    }
}

You nest a namespace by separating the nesting and the nested namespaces with a period. Here is an example:

namespace Geometry.Quadrilaterals
{
    public class Square
    {
    
    }
}

Other than that, the techniques of creating, nesting, and using namespaces are the same as done in C#.

Removing a Namespace from a Document

If a file has many "using namespace" lines you don't need, you can delete those lines manually or, in the Code Editor of Microsoft Visual Studio, right-click anywhere inside the document and click Remove and Sort Usings.

A Models Namespace for Classes

In your applications, although you can create your classes in the root project or in any folder you want, it is a good idea and good practice to create your classes in a folder named Models. Therefore, first create that folder. Then create your class in it. After creating the folder:

In the middle list of the Add New Item dialog box, make sure Class is selected. Accept or change the Name of the class. Click Add or press Enter. If you use any of these approaches, Microsoft Visual Studio would create a namespace using the name of the project and would add the new class to it.

Using a Namespace in a Razor Page

If you had created a namesapce in a certain code file and you need to access the contents of that namespace in a Razor Page, you can create reference to that namespace in the desired Razor Page. To do this, in the Razor Page, below the @model... line, type @using followed by the complete namespace that contains the item you want to access. Here is an example:

@page
@model Exercises.Pages.RainDropModel
@using Exercises.Models
@{
}

Introduction to Libraries

Overview

A library is a program that contains classes, structures, instructions, and/or other resources that some programs can use. A library can be used to assist a computer language, a Web application, a Web server, a browser, etc, to perform one or various types of operations such as arithmetic calculations, scientific manipulations, medical tests, simulations, chemistry, artificial intelligence, drawing, etc.

Introduction to Built-In Libraries

To assist you in creating and managing your projects, you can create libraries or use some already created libraries available. The most fundamental library you will use is the .NET Framework, and it is completely available when you start an ASP.NET Core Web App project. While the .NET Framework is a huge library, it contains sub-libraries in it. We will refer to them as assemblies.


Previous Copyright © 2001-2023, FunctionX Friday 17 December 2021 Next