Modeling the Walls of a House

In this project, we will model a medium house. We will model only the exterior of the house.

The building of a house starts with a foundation, followed by walls. In this section, we will start by creating the main walls of the house.

Practical LearningPractical Learning: Introducing the Model

  1. Start Blender
  2. Click the default cube to select it (it should be selected already)
  3. On the left toolbar, click the Move button Move
  4. In the Properties window, click the Object button Object (it should be selected already) to access the primary properties of the cube.
    Change the following values:
    Name:     Wall-External-Front
    Location: X: 0
              Y: 0.025
              Z: 1.355
    Rotation: X: 90
              Y: 0
              Z: 0
    Scale:    X: 2.6
              Y: 1.35
              Z: 0.025

    Modeling the Walls of a House

  5. To duplicate the wall, position the mouse in the 3D Viewport. Press Shift + D and press Enter
  6. In the Object section of the Properties window, change the Y location to 3.975
    Name:       Wall-External-Back
    Location Y: 3.975

    Modeling the Walls of a House

  7. To add a new object, on the top menu, click Add -> Mesh -> Cube
  8. In the Object section of the Properties window, change the following values:
    Name:         Wall-External-Left
    Location: X: -2.58
              Y:  2
              Z:  1.355
    Rotation: X:  90
              Y:  0
              Z:  90
    Scale:    X:  2
              Y:  1.35
              Z:  0.025

    Modeling the Walls of a House

  9. To duplicate the wall, position the mouse in the 3D Viewport. Press Shift + D and press Enter
  10. In the Object section of the Properties window, remove the - sign in the value of the Y location
    Name:       Wall-External-Right
    Location X: 2.58

    Modeling the Walls of a House

Adding the Entrance Door

Obviously, a house should have doors. In this section, we will add the necessary doors to our house.

Practical LearningPractical Learning: Adding an Entrance Door

  1. In a previous lesson, we modeled a simple door. Start another instance of Blender and open that file
  2. Draw a rectangle that touches the door frame and its inside face (the door leaf)
  3. Press Ctrl + C to copy the door to the clipboard
  4. Return to the instance of Blender where we are modeling the house
  5. Position the mouse in the 3D Viewport and press Ctrl + V to paste the door
  6. While the door is still selected, right-click anywhere in the 3D Viewport and click Move to Collection
  7. In the window that comes up, click New Collection
  8. Type Door-External as the new name and press Enter
  9. On the Move to New Collection dialog box, click Create
  10. On the top menu, click View -> Viewpoint -> Front
  11. Use the red arrow of the X axis to move the door to the left as follow:

    Modeling the Walls of a House

    Modeling the Walls of a House

  12. In a previous lesson, we modeled a simple door lock. In the other instance of Blender you had started, open the file of that door lock
  13. Click the door lock to select it
  14. Press Ctrl + C to copy it
  15. Return to the instance of Blender where we are modeling the house
  16. Position the mouse in the 3D Viewport and press Ctrl + V to paste the door
  17. While the door lock is still selected, right-click anywhere in the 3D Viewport and click Move to Collection
  18. On the Move to Collection dialog box, click Door-External
  19. On the top menu, click View -> Viewpoint -> Front
  20. Use the red arrow of the X axis to move the door to the left as follow:

    Modeling the Walls of a House

  21. In a previous lesson, we modeled a door handle. In the other instance of Blender you had started, open the file of that door lock
  22. Draw a rectangle that touches the parts of the door handle to select
  23. Press Ctrl + C to copy the selection
  24. Return to the instance of Blender where we are modeling the house
  25. Position the mouse in the 3D Viewport and press Ctrl + V to paste the door
  26. While the door handle is still selected, use the blue arrow of the Z axis to move the door handle and its lock down as follow:

    Modeling the Walls of a House

    Modeling the Walls of a House

Adding a Kitchen Door

The house has a kichen and that kitchen will use an external door. In this section, we will add that door.

Practical LearningPractical Learning: Adding a Kitchen Door

  1. In the Outliner, click one of the objects in the Door-External node
  2. Press and hold Shift
  3. Still in the Outliner, click each one of the object objects in the Door-External node to select them
  4. On the top menu, click View -> Viewpoint -> Top
  5. Press Alt + D and press Enter
  6. To rotate the duplicated door, first press the R key
  7. Press Z to rotate horizontally
  8. Type 180 and press Enter
  9. While the door is still selected, move it to the back wall of the house as follows:

    Modeling the Walls of a House

    Modeling the Walls of a House

    Modeling the Walls of a House

  10. On the top menu, click View -> Viewpoint -> Back
  11. While the door and its locks are still selected, press Alt + D and press Enter
  12. Use the X arrow to move the duplicated door to the right side of the other
  13. Move the door lock and the handle to the right side of the dooe:

    Modeling the Walls of a House

Adding a Garage Door

The house has an indoor garage. That garage has a door. We will make it a two-door garage. In this section, we will add those doors.

Practical LearningPractical Learning: Adding Two Garage Doors

  1. In a previous lesson, we modeled a garage door. In the other instance of Blender, open that file
  2. Click the garage door to select it
  3. Press Ctrl + C to copy the garage door to the clipboard
  4. Return to the instance of Blender where we are modeling the house
  5. Position the mouse in the 3D Viewport and press Ctrl + V to paste the door
  6. While the garage door is still selected, press Alt + D to duplicate and press Enter
  7. Position the garage doors to the right side of the house:

    Adding Garage Doors

    Adding Garage Doors

Adding a Large Window

Obviously, the house has windows. Now, we will add some windows. We start with a large window because the house uses only one of them.

Practical LearningPractical Learning: Adding a Large Window

  1. In a previous lesson, we modeled a large window. In the other instance of Blender you had started, open the file of that door lock
  2. Draw a rectangle that touches the parts of the window to select

    Modeling a Medium House - Adding a Window

  3. Press Ctrl + C to copy the selection
  4. Return to the instance of Blender where we are modeling the house
  5. Position the mouse in the 3D Viewport and press Ctrl + V to paste the door
  6. While the window is still selected, use the visual arrows to move the window to the right side of the from door as follow:

    Modeling the Walls of a House

    Modeling the Walls of a House

Adding Medium Windows

The house must use various windows for different rooms of the house. In this section, we will add one of those windows, and then duplicate it for the rooms that need such a window.

Practical LearningPractical Learning: Adding Windows

  1. In a previous lesson, we modeled a simple window. In the other instance of Blender, open that file
  2. Draw a rectangle to touches the parts of the window to select it

    Modeling a Medium House - Adding a Window

  3. Press Ctrl + C to copy it
  4. Return to the instance of Blender where we are modeling the house
  5. Position the mouse in the 3D Viewport and press Ctrl + V to paste the door
  6. While the window (and its parts) is still selected, right-click anywhere in the 3D Viewport and click Move to Collection
  7. On the Move to Collection dialog box, click New Collection
  8. Type Window-Front-BL
  9. Click Create
  10. On the top menu, click View -> Viewpoint -> Front
  11. Use the visual arrows in the 3D Viewport to move the window to the left of the front door as follow:

    Modeling the Walls of a House

  12. Make sure the window is selected (if it is not, in the Outliner window, click the first item below Window-Front-BL; press and hold Shift; click the last item below Window-Front-BL to select the whole window; then release Shift).
    Press Alt + D to duplicate the window
  13. While the window and all its parts are still selected, to change the orientation of the window, press R (to rotate)
  14. Press Z to rotate vertically
  15. Type -90 and press Enter
  16. Use the visual X (red) and Y (green) but not the Z (blue) arrows on the screen to position the window to the right section of the left wall

    Modeling the Walls of a House

    Modeling the Walls of a House

  17. While the window (and its parts) is still selected, right-click anywhere in the 3D Viewport and click Move to Collection
  18. On the Move to Collection dialog box, click New Collection
  19. Type Window-Left-Wall-BR
  20. Click Create
  21. Make sure the new window is selected (if it is not, in the Outliner, select all objects below the Window-Left-Wall-BR collection).
    Position the mouse in the 3D Viewport, press Alt + D to duplicate and move the duplicate
  22. While the window (and its parts) is still selected, right-click anywhere in the 3D Viewport and click Move to Collection
  23. On the Move to Collection dialog box, click New Collection
  24. Type Window-Left-Wall-BL
  25. Click Create
  26. Move the new window to the left side of the back wall as follows:

    Dividing the House Stories

  27. To select both left wall windows, in the Outliner, select all objects in the Window-Front-BL section and those in the Window-Left-Wall-BL section:

    Modeling a Simple Door

  28. To duplicate the windows, press Alt + D and press Enter
  29. Move the selected window up as follows:

    Modeling the Walls of a House

    Modeling the Walls of a House

  30. Use any of the above techniques to add four new windows to the front wall of the house as follows:

    Modeling the Walls of a House

  31. In the same way, add two windows to the bottom-right section of the right was as follows:

    Modeling the Walls of a House

  32. In the same way, add four windows to the back door as follows:

    Modeling the Walls of a House

    Modeling the Walls of a House

    Modeling the Walls of a House

Doors and Windows Openings

In previous sections, we add windows by their positions. Now we will create some openings on the walls where the windows and doos are positioned.

Practical LearningPractical Learning: Creating Openings for Doors and Windows

  1. To display the front view of the house, on the top menu, click View -> Viewpoint -> Front
  2. Click an empty area of house to select the front wall
  3. On the top-main menu of Blender, click Modeling
  4. To display the front view of the house, on the top menu, click View -> Viewpoint -> Front
  5. Position the mouse as close as possible to the left (or the right) border of the wall. Press Ctrl + R to create a horizontal cut and click once
  6. Move the cut to position it within the top border of the top windows:

    Modeling a Medium House - Creating a Loop Cut

  7. Click to confirm the loop cut
  8. In the same way, create horzontal cuts within the bottom borders of the top window, within the top borders of the bottom window, within the bottom borders of the bottom window, and within the top borders of the garage window:

    Modeling a Medium House - Creating Loop Cuts

  9. Position the mouse as close as possible to the top (or the bottom) border of the wall of the house. Press Ctrl + R to create a vertical cut and click once
  10. Move the cut to position it within the left border of the left windows:

    Modeling a Medium House - Creating a Loop Cut

  11. Click to confirm the loop cut
  12. In the same way, create horzontal cuts within the vertical borders of the windows and walls:

    Modeling a Medium House - Creating Loop Cuts

  13. On the top bar, click the Face button Face
  14. Click within a window or a door to select its inside face:

    Modeling a Medium House - Selecting a Face

  15. Press Delete
  16. On the menu the comes up, click Faces. In the same way, delete the faces within windows and doors
  17. Rotate the house to access its inside. Delete the faces within windows and doors
  18. On the main menu of Blender, click Layout:

    A Wall Divided

  19. Follow the above steps to create openings in the left wall:

    Dividing the House Stories

  20. And delete the face(s) within each window:

    Dividing the House Stories

  21. In the same way, Follow the above steps to create openings in the back wall and delete the face(s) within each window
  22. On the top menu of Blender, click Layout
  23. To save the model, on the main menu of Blender, click File and click Save
  24. In the top combo box, specify a folder of your choice
  25. Change the file name to House-Medium
  26. Click Save Blender File

Modeling the Ceiling Frame

A house must be covered. That cover starts with a frame made of different parts, usually in wood. Since this is not an extended design, we will keep the ceiling frame to a minimum.

Practical LearningPractical Learning: Modeling the Ceiling Frame

Setting the Materials for Walls

We are planning to create some variances of the same house. Those variances will have different colors or wall appearances.

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. On the top menu, click View -> Viewpoint -> Front
  3. Click somewhere on the wall to select it
  4. Rotate the house to view it in perspective
  5. On the bottom bar, click Material to select it
  6. Type Large-Wall and press Enter
  7. On the Shading toolbar, click Add -> Texture -> Brick Texture
  8. From the Brick Texture window, drag Color and drop it to the Base Color of Principled BSDF
  9. In the Brick Texture window, change the following values:
    Color 1:       #E79059FF
    Color 2:       #5C2E05FF
    Scale:         15
    Mortar Size:   0.015
    Mortar Smooth: 0.1
    Brick Width:   0.25
    Row Height:    0.205

    Materials - Bricks

  10. Click one of the smaller walls of the house to select it
  11. On the Shading toolbar, click the New Material button New Material
  12. Type Small-Wall and press Enter
  13. On the Shading toolbar, click Add -> Texture -> Brick Texture
  14. From the Brick Texture window, drag Color and drop it to the Base Color of Principled BSDF
  15. In the Brick Texture window, change the following values:
    Color 1:       #E79059FF
    Color 2:       #5C2E05FF
    Scale:         15
    Mortar Size:   0.015
    Mortar Smooth: 0.1
    Brick Width:   0.295
    Row Height:    0.205
  16. Click the other small wall to select it
  17. On the Shading toolbar, click the arrow of the Material button and select Small-Wall
  18. On the top-main menu of Blender, click Layout
  19. On the bottom bar, click the arrow of the Browse Material combo box Object and select Short-Wall
  20. On the left bar, click the Loop Cut button Loop Cut
  21. On the top-main menu of Blender, click Modeling
  22. Rotate the shape to access the other wallClick that wall to create a horizontal cut

    Dividing the House Stories

  23. On the top menu of Blender, click Layout
  24. In the Object section of the Properties window, change the following values:
    Name:     Door-Frame
    Location: X: -0.925
              Y:  2.265
              Z:  0.5
    Scale:    X:  0.285
              Y:  0.0315
              Z:  0.5
  25. To hide the walls of the house, in the numeric pad of the keyboard, press /

    Modeling a Simple Door

  26. On the left bar, click the Loop Cut button Loop Cut
  27. Change the Number of Cuts value to 2:
    Number of Cuts: 2

    Modeling a Simple Door

  28. In the Resize window in the bottom-left section of the 3D Viewport window, change the following values:
    Move: X: 2.55
          Y: 1
          Z: 1

    Modeling a Simple Door

  29. In the Loop Cut and Move window, change the factor to -0.915:
    Factor: -0.915

    Modeling a Simple Door

  30. On the top bar, click the Face button Face
  31. On your keyboard, press X
  32. On the menu that appears, click Faces
  33. Click the bottom-central face of the shape to select it
  34. On your keyboard, press Delete
  35. On the menu that appears, click Faces
  36. Right-click anywhere in the 3D Viewport workspace window -> Separate -> Selection

    Modeling a Simple Door

  37. To display the hidden objects, in the numeric pad of the keyboard, press /
  38. To confirm the separated parts, on the top-main menu of Blender, click Layout
  39. Click the wide face in the middle of the shape to select it

    Modeling a Simple Door

  40. In the Object section of the Properties window, click Door-Frame.001 to select the name
  41. Position the mouse on the 3D Viewport. On your keyboard, press H to hide the face
  42. Click any border of the window to select it

    Modeling a Simple Door

  43. To continue editing the door, on the main menu of Blender, click Modeling
  44. On the top bar, click the Edge button Edge
  45. On the window, click one of the interior edges of the opening to select it
  46. On your keyboard, press and hold Shift
  47. Click each of the interior edges of both openings of the door but don't select the bottom edge of each side

    Modeling a Simple Door

    Modeling a Simple Door

  48. On the menu bar, click Edge -> Bridge Edge Loops

    Modeling a Simple Door

    Modeling a Simple Door

  49. On the top-main menu of Blender, click Layout

    Modeling a Simple Door

  50. In the Outliner, click the Hide In Viewport button of Door-Leaf to unhide it
  51. In the 3D Viewport, click that interior face (Door-Leaf) to select it
  52. To edit the face, on the main menu of Blender, click Modeling
  53. On the top menu bar, click the Face button Face
  54. Click the large face on the door

    Modeling a Simple Door

  55. Press E to extrude and press Enter
  56. On the main menu of Blender, click Layout

    Modeling a Simple Door

    Modeling a Simple Door

  57. In the bottom-left section of the 3D Viewport, click Add Cube to expand its window
  58. In the Add Cube window, change the following characteristics:
    Radius:     0.015
    Radius 2:    0.01
    Depth:       0.01
    Location X: -1.135
             Y:  2.25
             Z:  0.55
    Rotation X:  90

    Modeling a Simple Door Lock

  59. Type Door-Lock-Round as the name and press Enter
  60. To edit the cone, on the top-main menu of Blender, click Modeling
  61. To hide the other objects, in the numeric pad of the keyboard, press /
  62. Click and drag the Rotate button to see the large back face of the cone

    Modeling a Simple Door Lock

  63. On the top menu, click the Face button Face
  64. Click the large vertical face to select it

    Modeling a Simple Door Lock

  65. Press E to extrude and press Enter
  66. In the Extrude Region and Move window, change the Z value to 0.45:
    Move Z: 0.035

    Modeling a Simple Door Lock

  67. Position the mouse on an empty area of the 3D Viewport window. Press E to extrude and press Enter
  68. In the Extrude Region and Move window, change the Z value to 0.1:
    Move Z: 0.1

    Modeling a Simple Door Lock

  69. Position the mouse in the 3D Viewport. Press S to resize the face. Pull the mouse slightly and click
  70. In the Resize window, change the X and the Z values to 0.95 each:
    Scale: X: 0.95
           Z: 0.95
  71. Position the mouse in the 3D Viewport. Press E to extrude, then press S to resize, then press Enter
  72. In the Extrude Region and Move window, change the X and the Z values to 0.5:
    Scale: X: 0.95
           Z: 0.95

    Modeling a Simple Door Lock

  73. Rotate the cone to see the front face (you can use the middle button of the mouse)
  74. Click the front vertical face to select it

    Modeling a Simple Door Lock

  75. -----------------------------------------------------------------------------------------------------------
  76. Press E to extrude and press Enter
  77. In the Extrude Region and Move window, change the Z value to 0.025:
    Move Z: 0.025

    Modeling a Simple Door Lock

  78. Position the mouse in the 3D Viewport. Press S to resize. Slightly move the mouse and click
  79. In the Resize window, change the X and the Z values to 0.95 each:
    Scale: X: 0.95
           Z: 0.95

    Modeling a Simple Door Lock

  80. While the face is still selected, position the mouse in the 3D Viewport. Press E to extrude, then press S to resize, then press Enter
  81. In the Extrude Region and Move window, change the X and the Z values to 0.875:
    Scale: X: 0.875
           Z: 0.875

    Modeling a Simple Door Lock

  82. While the face is still selected, position the mouse in the 3D Viewport. Press E to extrude, then press S to resize, then press Enter
  83. In the Extrude Region and Move window, change the X and the Z values to 0.95:
    Scale X: 0.95
           Z: 0.95

    Modeling a Simple Door Lock

  84. While the face is still selected, position the mouse on the 3D Viewport window. Press E to extrude, then press S to resize, then press Enter
  85. In the Extrude Region and Move window, change the X and the Z values to 0.95 each:
    Scale: X: 0.985
           Z: 0.985

    Modeling a Simple Door Lock

  86. While the face is still selected, position the mouse on an empty area of the 3D Viewport window. Press E to extrude, then press S to resize, then press Enter
  87. In the Extrude Region and Move window, change the X and the Z values to 0.95 each:
    Scale: X: 0.985
           Z: 0.985

    Modeling a Simple Door Lock

  88. While the face is still selected, position the mouse on an empty area of the 3D Viewport window. Press E to extrude, then press S to resize, then press Enter
  89. In the Extrude Region and Move window, change the X and the Z values to 0.95 each:
    Scale: X: 0.95
           Z: 0.95

    Modeling a Simple Door Lock

  90. On the top tool menu, click the Edge button Edge
  91. Press and hold Alt
  92. On the shape in the 3D Viewport, click one of the edges of the 5th circle from the external circle
  93. Release Alt

    Modeling a Simple Door Lock

  94. Press G slightly and click
  95. In the Move window, change the values as follows:
    Move: X: 0
          Y: 0.25
          Z: 0

    Modeling a Simple Door Lock

  96. Press and hold Alt
  97. Click one of the edges of the 4th circle from the external circle
  98. Release Alt

    Modeling a Simple Door Lock

  99. Press Ctrl + B and press Enter
  100. In the Bevel window, change the following values:
    Width:    0.0125
    Segments: 2

    Modeling a Simple Door Lock

  101. While the Edge option is still selected, press and hold Alt
  102. Click one of the edges outside the inner circle
  103. Release Alt

    Modeling a Simple Door Lock

  104. Position the mouse in the 3D Viewport window. Press Ctrl + B and press Enter
  105. In the Bevel window, change the following values:
    Width:    0.0125
    Segments: 2

    Modeling a Simple Door Lock

  106. On the top bar, click the Face button Face
  107. Click the large central face to select it
  108. Press E to extrude
  109. Then press S to resize, then press Enter
  110. In the Extrude Region and Move window, change the X and Y values as follows:
    Scale X: 0.65
          Z: 0.65

    Modeling a Simple Door Lock

  111. Position the mouse in the 3D Viewport window. Press G to move the face
  112. Move the mouse slightly and click
  113. In the Move window, change the following values:
    Move X: 0
              Y: 0
              Z: -0.105

    Modeling a Simple Door Lock

  114. While the face is still selected, press E to extrude
  115. Then press S, then press Enter
  116. In the Extrude Region and Move window, change the following values:
    Move X: 0.975
         Z: 0.975
  117. While the face is still selected, press E to extrude
  118. Tthen press S, then press Enter
  119. In the Extrude Region and Move window, change the following values:
    Move X: 0.975
         Z: 0.975
  120. While the face is still selected, press E to extrude
  121. Then press S, then press Enter
  122. In the Extrude Region and Move window, change the following values:
    Move X: 0.205
         Z: 0.85

    Modeling a Simple Door Lock

  123. On the top tool menu, click the Edge button Edge
  124. Press and hold Alt
  125. Click one of the edges of the circle between the three circles that were just drawn
  126. Release Alt

    Modeling a Simple Door Lock

  127. Press G slightly and click
  128. In the Move window, change the values as follows:
    Move: X: 0
           Y: 0.025
           Z: 0

    Modeling a Simple Door Lock

  129. On the top toolbar, click the Face button Face
  130. Click the large oval face on the lock to select it

    Modeling a Simple Door Lock

  131. Press E to extrude and press Enter
  132. In the Extrude Region and Move window, change the following values:
    Move X:  0
         Y:  0
         Z: -0.75

    Modeling a Simple Door Lock

  133. To save the lock, on the main menu, click File -> Save
  134. Click Save
  135. While the lock is still selected, in the Properties window, click the Modifiers button Modifiers
  136. In the Properties window, set the Levels Viewport to 2:
    Levels Viewport: 2
  137. Right-click the lock and click Shade Smooth:

    Modeling a Simple Door Lock

  138. To save, on the main menu, click File -> Save As...
  139. Set the document name Door-Lock-Simple-2
  140. Click Save As

Adding Windows

Obviously, a house should have walls and window. In this section, we will add them to the model.

Finalizing the Interior Door

Practical LearningPractical Learning: Finalizing the Interior Door

  1. Click the default cube to select it (it should be selected already)
  2. In the Properties window, click the Object button Object (it should be selected already) to access the primary properties of the cube
  3. To add a window, position the mouse on the top-right side of the window until the mouse cursor appears as +

    Window Maker

  4. Click and drag in the left direction and click

    Windows Panels

  5. Click the button in the top-left section of the right panel and select Asset Browser

    Dividing the House Stories

    Dividing the House Stories

  6. From the Asset Library window, drag Door-Frame-Simple to the 3D Viewport. In the 3D Viewport, drop the object in an occupied area, not on a wall of the house
  7. On the left toolbar, click the Move button Move
  8. From the Asset Library window, drag Door-Leaf-Simple to the 3D Viewport. In the 3D Viewport, drop the object in an occupied area, not on a wall of the house
  9. In the Asset Library window, click Door-Leaf-Simple to add it to the selection
  10. Release Ctrl
  11. In the Asset Library window, drag the selected items and drop the group in the 3D Viewport

    Blender Panels

  12. Close Blender
  13. When asked whether you want to save, click Don't Save
  14. In a previous lesso 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.
  15. 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:

This would produce:

Method Overloading and Tuples

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
{
}

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

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:

        
    }
}

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:

mbr.WriteLine("===============================");.fee}");

This would produce:

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:

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 (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:



                

This would produce:

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:

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:



                

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

Trapezoid isosceles = new Circle();

(Trapezoid shape

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; }
}

Practical LearningPractical Learning: Ending the Lesson


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