Modeling the Walls of a House

Introduction

We saw how to declare tuple variables. In the same way, you can declare a tuple variable in the body of a class, in which case the variable is treated as a field. Here is an example:

Practical LearningPractical Learning: Introducing the Model

  1. Start Blender
  2. Click the default cube to select it (it should be selected already)
  3. In the Properties window, click the Object button Object (it should be selected already) to access the primary properties of the cube
  4. Change the following values:
    Location: X: 0
              Y: 4.25
              Z: 1.245
    Scale:    X: 2.5
              Y: 2
              Z: 1.245
  5. To edit the cube, on the top-main menu of Blender, click Modeling
  6. On the top bar, click the Face button Object
  7. Click the top face of the cube to select it
  8. On your keyboard, press X
  9. On the menu that appears, click Faces
  10. Rotate the cube to access the bottom face and click it to select it
  11. On your keyboard, press Delete
  12. On the menu that appears, click Faces
  13. On the remaining shapes, click one of the faces that is parallel to the green Y axis to select it
  14. Press and hold Shift
  15. Click the face that is opposing that one to select it
  16. Right-click anywhere in the 3D Viewport workspace window -> Separate -> Selection
  17. To confirm the separated parts, on the top-main menu of Blender, click Layout
  18. Click one of the remaining faces of the original cube

Setting the Materials for Walls

As you may know already, a constructor is a special method that is used to initialize a variable or a field. Therefore, if you create a regular tuple field, you can use a constructor to initialize it. Here is an example:

Practical LearningPractical Learning: Setting the Materials for Walls

  1. To access the materials of the faces, on the top-main menu of Blender, click Shading
  2. In the Shading window, click one of the faces that are longer (parallel to the X axis)
  3. On the bottom bar, click Material to select it
  4. Type Long-Wall and press Enter
  5. On the bottom bar, click the New Material button Object
  6. On the bottom bar, click Long-Wall.001 to select it
  7. Type Short-Wall and press Enter
  8. Click one of the other walls to select them
  9. On the bottom bar, click the arrow of the Browse Material combo box Object and select Short-Wall
  10. On the top menu of Blender, click Layout

Dividing the House Stories

You can create a constant tuple but whose value depends on specific objects. This is the case for a read-only tuple. You can create it in the body of a class and initialize it in a constructor. Here is an example:

Practical LearningPractical Learning: Dividing the House Stories

  1. Click one of the sections of the walls
  2. On the top-main menu of Blender, click Modeling
  3. On the left bar, click the Loop Cut button
  4. Click one of the walls to create a horizontal cut
  5. Rotate the shape to access the other wall
  6. Click that wall to create a horizontal cut
  7. On the top-main menu of Blender, click Layout
  8. Click one of the other walls you did not select
  9. On the top-main menu of Blender, click Modeling
  10. While the Loop Cut button is still selected, click one of the walls to create a horizontal cut
  11. Rotate the shape to access the other wall
  12. Click that wall to create a horizontal cut
  13. On the left bar, click the Select Box button
  14. WriteLine($"Full Name: {mbr.Identification.name}");
  15. While the new faces are selected, in the Properties window, change the Z-location as follows:

Initializing the Properties of a Class

Consider a class as follows:

This would produce:

Method Overloading and Tuples

In our introduction to method overloading, we saw that a method can get overloaded if you create more than one version in the same class. We also saw that the versions of the method must differ by their syntax or signature. Remember that the signature of a method doesn't include its return type. Based on this, you cannot overload a method based on the fact that one version returns a tuple and another does not.

To overload a method that involves tuples, you will rely on the parameters. You can create a method that has different versions. Two or more versions of a method can take one tuple parameter each; each tuple-parameter can have the same number of elements but the elements can be different. Consider the following example:

On the other hand, you can overload a method by passing a mixture of primitive types and tuples.

Tuples and Properties

Introduction

You can create a property whose type is a tuple. To start, in the body of the class, you can declare a field variable that is a tuple type. Here is an example:

public class Member
{
    public (int, string, double) id;
}

To get the property, create a property that has a return type as a tuple. Here is an example:

public class Member
{
    private (int, string, double) id;

    
}

A Read-Only Tuple Property

A read-only property is a a property with only a get clause. For a tuple property, make the get clause return a private field that has the same return type as the property. Here is an example:

public class Member
{
    private (int, string, double) id;

    public (int, string, double) Identification
    {
        
    }
}

If you want to use the property outside its class, it must have a value, which it cannot get on its own because it is a read-only property. The most common way you can initialize this property is by using a constructor that uses a parameter of the same tuple type as the property. After initializing the property, you can get its value and use it. Here is an example:

WriteLine($"Account Fee: {mbr.Identification.fee}");
WriteLine("===============================");

public class Member
{
    private (int, string, double) id;

    public Member((int, string, double) identifier, int amt)
    {
        id = identifier;
    }

    public (int number, string name, double fee) Identification
    {
        get
        {
            return id;
        }
    }
}

This would produce:

Membership Details
-------------------------------
Member #:    295380
Full Name:   Elisabeth Merlins
Account Fee: 98.5
===============================

Press any key to close this window . . .

A Fully-Implemented Tuple Property

As seen in our introduction to properties, if you want to control the details of processing a property, you can create a private field that is the same tuple type as the property. Then add either or both a get and a set clauses. Here is an example:

public class Employee
{
    

    public (int, string, bool) Identification
    {
        get
        {
            return id;
        }
        set
        {
            id = value;
        }
    }
}

An Automatic Tuple Property

If you are not planning to validate, accept, or reject the values of the property, you can create the property as an automatic one. Here is an example:

public enum PayFrequency { Daily, Monthly, Yearly }

public class Employee
{
    private (int, string, bool) id;

    public (int, string, bool) Identification
    {
        get
        {
            return id;
        }
        set
        {
            id = value;
        }
    }

    public (bool, string, PayFrequency, double) Salary { get; set; }
}

When creating the property, it is a good idea, although not a requirement, to name the elements of the tuple. It may also be a good idea to add comments that explain the roles of the elements of the tuple. Here is an example:

    
}

As mentioned in the previous lesson, if you don't name the elements of a tuple, the compiler gives them some default names as Item1, Item2, etc.

When using the property, you may need to access the elements of its type. You will access them by their names. From inside the class, such as in the body of a clause of another property or in the body of a method of the same name, type the name of the desired property, type a period, and select the element of your choice. Here is an example:

When using the property outside the class, if you have declared a variable of the class that owns the property, type the name of the object, a period, the name of the property, a period, and the desired element. Here is an example:

Tuples and Properties

Once you have accessed the property or any of its elements, you can use it like any property as we have done in previous lessons.

If you create an automatic tuple property, you cannot individually initialize the elements of the tuple property. Still, you can access the elements to present to the user. Here is an example:

using static System.Console;

Processor proc = new();

public class Processor
{
    /* We are combining these pieces of information of the 
     * processor because processors specifications are 
     * related by generation and tied to a manufacturer. */
    public (string make, string model, string socket) Manufacture { get; set; }

    public (int, int) Count { get; set; }
    public double Speed { get; set; }

    public Processor()
    {
        Count = (6, 12);
        Manufacture = ("AMD", "RYZEN 5", "AM4");

        Present();
    }

    private void Present()
    {
        Write("Make:      ");
        WriteLine(Manufacture.make);
        Write("Model:     ");
        WriteLine(Manufacture.model);
        Write("Socket:    ");
        WriteLine(Manufacture.socket);
        Write("Processor: ");
        Write(Count.Item1);
        Write("-Core, ");
        Write(Count.Item2);
        WriteLine("-Thread.");
        WriteLine("==============================");
    }
}

This would produce:

Make:      AMD
Model:     RYZEN 5
Socket:    AM4
Processor: 6-Core, 12-Thread.
==============================
Press any key to continue . . .

Topics on Tuples

Methods and Tuples

We saw how to involve tuples with functions. Everything we saw about passing a tuple as argument and returning a tuple can be applied exactly the same way to the methods of a class. Normally, methods deal with tuples exactly as we described for functions, with just minor adjustments. It is important to remember (as stated in our introductory lesson on functions) that a function is a section of code that behaves as if it written outside of a class. Otherwise, everything we studied about involving tuples and functions also applies to methods. This means that you can pass a tuple to a method and you can return a tuple from a method.

As you know already, a method is a function created inside a class. As you know already, if you create a method, it has direct access to other members of the same class. In a method, you have direct access to the names of the tuples. To access an element of a tuple, type the name of the member, a period, and the desired member. That way, you can initialize a tuple. Here is an example:

public class Processor
{
    /* We are combining these pieces of information of the 
     * processor because processors specifications are 
     * related by generation and tied to a manufacturer. */
    private (string make, string model, string socket) identification;

    private void Create()
    {
        identification = ("AMD", "RYZEN 5", "AM4");
    }
}

An Object in a Tuple

All the elements we used so far in tuples were of regular types. In reality, each element is a placeholder for practically any type you want. Based on this, an element of a tuple can be an object of a structure or class type. Of course, you must have a class. You can create and use your own class. Here is an example of a class named Trapezoid created in a Windows Forms application named Geometry:

namespace Geometry
{
    public class Trapezoid
    {
        public double TopBase    { get; set; }
        public double BottomBase { get; set; }
        public double Height     { get; set; }

        public double Area
        {
            get
            {
                return Height * (TopBase + BottomBase) / 2.00;
            }
        }
    }
}

When creating the tuple, specify the desired element using the name of the class. Here is an example:

Trapezoid isosceles = new Circle();

(Trapezoid shape, int value) definition;

public class Trapezoid
{
    public double TopBase { get; set; }
    public double BottomBase { get; set; }
    public double Height { get; set; }

    public double Area
    {
        get
        {
            return Height * (TopBase + BottomBase) / 2.00;
        }
    }
}

When initializing the tuple or when specifying its value, you must define the object used as element. You have various options:

In the above example, we used a tuple that has one element that is a class type. In the same way, you can create a tuple with more than one element that are of class or structure type. The elements can be of the same class (or structure) or different classes (or structures).

In the above example, we used our own class. On the other hand, we know that the .NET Framework provides a large collection of classes and structures. You can use any of most of the many classes of the .NET Framework for an element of a tuple.

The Name Involving a Tuple

We already know that, to help you identify something in your code, the C# language provides an operator named nameof. You can use this property on anything that involves a tuple, including a variable of a tuple type, a property of a tuple type, a function or method that returns a tuple, or a function or property that takes a tuple as argument. Here are examples:

using static System.Console;

(int nbr, string status, double salary) employee = (937_842, "Full Time", 22.48);

(string fname, string lname) Identify()
{
    return ("Frank", "Lanson");
}

House house = new House()
{
    PropertyNumber = 938_448,
    Rooms = (5, 3.5),
    MarketValue = 545_760
};

void Present(int nbr, (string title, House prop) listing)
{
    WriteLine(listing.title);
    WriteLine("----------------------------------");
    WriteLine("Listing #:   {0}", nbr);
    WriteLine("Property #:   {0}", listing.prop.PropertyNumber);
    WriteLine("Bedrooms:     {0}", listing.prop.Rooms.beds);
    WriteLine("Bathrooms:    {0}", listing.prop.Rooms.baths);
    WriteLine("Market Value: {0}", listing.prop.MarketValue);
}

WriteLine("Employee Record");
WriteLine("----------------------------------");
WriteLine("Employee #:    {0}", employee.nbr);
WriteLine("Employee Name: {0} {1}", Identify().fname, Identify().lname);
WriteLine("Employment Status: {0}", employee.status);
WriteLine("Hourly Salary: {0}", employee.salary);
WriteLine("==================================");

Present(1001, ("Property Listing", house));

WriteLine("==================================");
WriteLine("Name of Tuple Variable: {0}", nameof(employee));
WriteLine("Name of Tuple Function: {0}", nameof(Identify));
WriteLine("Name of Object:         {0}", nameof(house));
WriteLine("Name of Tuple Property: {0}", nameof(house.Rooms));
WriteLine("Name of Function:       {0}", nameof(Present));
WriteLine("==================================");

internal class House
{
    public int PropertyNumber { get; set; }
    public (int beds, double baths) Rooms { get; set; }
    public double MarketValue { get; set; }
}

This would produce:

Employee Record
----------------------------------
Employee #:    937842
Employee Name: Frank Lanson
Employment Status: Full Time
Hourly Salary: 22.48
==================================
Property Listing
----------------------------------
Listing #:   1001
Property #:   938448
Bedrooms:     5
Bathrooms:    3.5
Market Value: 545760
==================================
Name of Tuple Variable: employee
Name of Tuple Function: Identify
Name of Object:         house
Name of Tuple Property: Rooms
Name of Function:       Present
==================================

Press any key to close this window . . .

Practical LearningPractical Learning: Ending the Lesson


Previous Copyright © 2001-2024, FunctionX Saturday 29 April 2023, 22:44 Next