The Objects on an Application
The Objects on an Application
The Objects of a Project
When it comes to a computer application, an object is anything that participates in a program. It can be a symbol, something to click, something to select, etc. An object is created from a type. The .NET Framework provides many options and Microsoft Visual Studio offers many tools.
An Interface
An interface is the most fundamental description of an object. An interface is usually small to perform one precise operation or a limited number of operations. An interface is used to serve other types but an interface can also implement one or more interfaces.
Practical Learning: Creating Interfaces
namespace Polygonals
{
internal interface IDescription
{
public abstract void Describe();
}
}namespace Polygonals
{
internal interface IPolygon : IDescription
{
abstract int Sides { get; }
abstract double Side { get; set; }
abstract int InternalAngle { get; }
abstract int SumOfInterriorAngles { get; }
abstract double CalculatePerimeter();
abstract double CalculateArea();
abstract double CalculateInscribedRadius();
abstract double CalculateCircumscribedRradius();
}
}The Type of an Object
The C# language provides various objects to let you create obfects. Options includde structures, records, and classes. A class is the most common type used to create an object.
Practical Learning: Creating Classes
namespace Polygonals
{
internal class Pentagon : IPolygon, IDescription
{
public int Sides
{
get
{
return 5;
}
}
public double Side { get; set; }
public int InternalAngle
{
get
{
return 108;
}
}
public virtual int SumOfInterriorAngles
{
get
{
return 540;
}
}
public virtual double CalculatePerimeter()
{
return Side * Sides;
}
public virtual double CalculateArea()
{
return Side * Side * Math.Sqrt(5.00 * (5.00 + (2.00 * Math.Sqrt(5.00)))) / 4.00;
}
public double CalculateInscribedRadius()
{
// http://mathworld.wolfram.com/Pentagon.html
return Side * Math.Sqrt(25.00 + (10.00 * Math.Sqrt(5.00))) / 10.00;
// https://en.wikipedia.org/wiki/Pentagon
// return Side / (2.00 * Math.Sqrt(5.00 - Math.Sqrt(20.00)));
}
public double CalculateCircumscribedRradius()
{
// http://mathworld.wolfram.com/Pentagon.html
return Side * Math.Sqrt(50.00 + (10.00 * Math.Sqrt(5.00))) / 10.00;
}
public virtual void Describe()
{
Console.WriteLine("Pentagon Characteristics");
Console.WriteLine("-------------------------------------------");
Console.WriteLine("Side: {0}", Side);
Console.WriteLine("Number of Sides: {0}", Sides);
Console.WriteLine("Internal Angle: {0}", InternalAngle);
Console.WriteLine("Sum of Interrior Angles: {0}", SumOfInterriorAngles);
Console.WriteLine("-------------------------------------------");
Console.WriteLine("Perimeter: {0}", CalculatePerimeter());
Console.WriteLine("Inscribed Radius: {0}", CalculateInscribedRadius());
Console.WriteLine("Circumscribed Rradius: {0}", CalculateCircumscribedRradius());
Console.WriteLine("Area: {0}", CalculateArea());
Console.WriteLine("===========================================");
}
}
public record Quadrilateral
{
}
}
namespace Polygonals
{
internal class EquilateralTriangle : IPolygon, IDescription
{
public int Sides
{
get
{
return 3;
}
}
public double Side { get; set; }
public virtual int InternalAngle
{
get
{
return 60;
}
}
public virtual int SumOfInterriorAngles
{
get
{
return 180;
}
}
public virtual double CalculatePerimeter()
{
return Side * Sides;
}
public virtual double CalculateArea()
{
return Side * Side * Math.Sqrt(3.00) / 4.00;
}
public virtual double CalculateInscribedRadius()
{
return Side * Math.Sqrt(3.00) / 6.00;
}
public virtual double CalculateCircumscribedRradius()
{
return Side * Math.Sqrt(3.00);
}
public double CalculateAltitude()
{
return (Math.Sqrt(3.00) / 2.00) *Side;
}
public virtual void Describe()
{
Console.WriteLine("Equilateral Triangle Characteristics");
Console.WriteLine("-------------------------------------------");
Console.WriteLine("Side: {0}", Side);
Console.WriteLine("Number of Sides: {0}", Sides);
Console.WriteLine("Internal Angle: {0}", InternalAngle);
Console.WriteLine("Sum of Interrior Angles: {0}", SumOfInterriorAngles);
Console.WriteLine("-------------------------------------------");
Console.WriteLine("Altitude: {0}", CalculateAltitude());
Console.WriteLine("Inscribed Radius: {0}", CalculateInscribedRadius());
Console.WriteLine("Circumscribed Rradius: {0}", CalculateCircumscribedRradius());
Console.WriteLine("Perimeter: {0}", CalculatePerimeter());
Console.WriteLine("Area: {0}", CalculateArea());
Console.WriteLine("===========================================");
}
}
}namespace Polygonals
{
internal record Square : IPolygon, IDescription
{
public double Side { get; set; }
public virtual int Sides
{
get
{
return 4;
}
}
public int InternalAngle
{
get
{
return 90;
}
}
public virtual int SumOfInterriorAngles
{
get
{
return 360;
}
}
public virtual double CalculatePerimeter()
{
return Side * Sides;
}
public virtual double CalculateArea()
{
return Side * Side;
}
public double CalculateInscribedRadius()
{
return Side / 2.00;
}
public double CalculateCircumscribedRradius()
{
return (1.00 / Math.Sqrt(2.00)) * Side;
}
public virtual void Describe()
{
Console.WriteLine("Square Characteristics");
Console.WriteLine("-------------------------------------------");
Console.WriteLine("Side: {0}", Side);
Console.WriteLine("Number of Sides: {0}", Sides);
Console.WriteLine("Internal Angle: {0}", InternalAngle);
Console.WriteLine("Sum of Interrior Angles: {0}", SumOfInterriorAngles);
Console.WriteLine("-------------------------------------------");
Console.WriteLine("Inscribed Radius: {0}", CalculateInscribedRadius());
Console.WriteLine("Circumscribed Rradius: {0}", CalculateCircumscribedRradius());
Console.WriteLine("Perimeter: {0}", CalculatePerimeter());
Console.WriteLine("Area: {0}", CalculateArea());
Console.WriteLine("===========================================");
}
}
public record Rhombus : Quadrilateral
{
public double Width { get; set; }
public double Height { get; set; }
}
}using Polygonals;
using static System.Console;
Square sqr = new Square();
Rhombus rho = new Rhombus();
IPolygon pent = new Pentagon();
Triangle tri = new Triangle();
sqr.Side = 227.93;
tri.Side = 227.93;
pent.Side = 227.93;
WriteLine("===========================================");
WriteLine("Geometry");
WriteLine("===========================================");
tri.Describe();
sqr.Describe();
pent.Describe();
rho.Width = 927.58;
rho.Height = 1377.93;
WriteLine("Rhombus Characteristics");
WriteLine("-------------------------------------------");
WriteLine("Width: {0}", rho.Width);
WriteLine("Height: {0}", rho.Height);
WriteLine("===========================================");=========================================== Geometry =========================================== Equilateral Triangle Characteristics ------------------------------------------- Side: 227.93 Number of Sides: 3 Internal Angle: 60 Sum of Interrior Angles: 180 ------------------------------------------- Altitude: 197.3931702845871 Inscribed Radius: 65.7977234281957 Circumscribed Rradius: 394.7863405691742 Perimeter: 683.79 Area: 22495.91265148297 =========================================== Square Characteristics ------------------------------------------- Side: 227.93 Number of Sides: 4 Internal Angle: 90 Sum of Interrior Angles: 360 ------------------------------------------- Inscribed Radius: 113.965 Circumscribed Rradius: 161.17084863584978 Perimeter: 911.72 Area: 51952.0849 =========================================== Pentagon Characteristics ------------------------------------------- Side: 227.93 Number of Sides: 5 Internal Angle: 108 Sum of Interrior Angles: 540 ------------------------------------------- Perimeter: 1139.65 Inscribed Radius: 156.85936556649727 Circumscribed Rradius: 193.8888387476805 Area: 89382.38798392932 =========================================== Rhombus Characteristics ------------------------------------------- Width: 927.58 Height: 1377.93 =========================================== Press any key to close this window . . .
Accessing the Code of a Project
Introduction
If you have a project with various files, objects accessed from different files, functions and methods called from various places, it can be overwhelming to find a piece or section of code you are looking for. Fortunately, Microsoft Visual Studio provides various tools to assist you.
Renaming a Variable
If you have a variable that uses a name you don't like, you can change the name of that variable. If the variable is used only once in your code, you can just highlight it and change its name. If the variable is used in various sections of your project, it is not practical to manually rename every instance of that variable. Fortunately, Microsoft Visual Studio can assist you with that operation. First find the variable. Right-click it and click Rename. A small window would come up. In that window, replace the desired name and press Enter.
Practical Learning: Renaming a Variable
Accessing the References to a Type
After creating a type (interface, class, record, or structure) and using it in various places (such as creating objects), at one time, you may want to know all the places where the object is used. Instead of performing that operation manually, Microsoft Visual Studio can assist you. To start, where the type is created, check the line above it. You can click that string and use the menu that would come up.
Practical Learning: Accessing the References to a Property or Method
Accessing the References to a Property or Method
After creating a property and accessing it in one or more places, after defining a function or a method and calling it in one or more places, you may want to get a list of all the places where the property is accessed or where the function or method is called. Microsoft Visual Studio can help you get that information.
Practical Learning: Accessing the References to a Property or Method
Accessing the Definition of an Item
Sometimes, when viewing a variable, an object, a property, a function call, or a method call in your code, you may not know or don't remember how that variable was declared, how that object was created, or how the function or method was defined. Instead of manually tracking such a definition, Microsoft Visual Studio can assist you. If you right-click a variable or an object in the Code Editor, you can then click Go To Definition. The studio would jump to the area where the variable is declared or where the object is created. If you right-click a function or a method call, the studio would present the area where the function or the method is defined.
Practical Learning: Accessing the References to a Property or Method
Square sqr = new Square();
Rhombus rho = new Rhombus();
IPolygon pent = new Pentagon();
Triangle tri = new Triangle();
sqr.Side = 227.93;
tri.Side = 227.93;
pent.Side = 227.93;Peeking a Definition of a Property or Method
As you might have noticed from the previous section, the Go To Definition requires that you access a property or a method in the file where it is defined. In fact, that technique displays the whole file, which is very useful sometimes. Sometimes, you just want to see, briefly, that piece of code. Microsoft Visual Studio can assist you with the Peek Definition option.
Practical Learning: Peeking a Definition of a Property or Method
Public Class Square
Public Overridable Property Length As Double
Public Overridable Function CalculatePerimeter() As Double
Return Length * 4.0
End Function
Public Overridable Function CalculateArea() As Double
Return Length * Length
End Function
Public Overridable Sub Describe()
Console.WriteLine("Square")
Console.WriteLine("----------------------------------")
Console.WriteLine("Side: {0}", Length)
Console.WriteLine("----------------------------------")
Console.WriteLine("Perimeter: {0}", CalculatePerimeter)
Console.WriteLine("Area: {0}", CalculateArea)
End Sub
End ClassPublic Class Cube
Inherits Square
Public Function CalculateVolume() As Double
Return Length * Length * Length
End Function
Public Overloads Sub Describe()
Console.WriteLine("Cube")
Console.WriteLine("==================================")
Console.WriteLine("Each Face")
Console.WriteLine("----------------------------------")
Console.WriteLine("Side: {0}", Length)
Console.WriteLine("Perimeter: {0}", CalculatePerimeter)
Console.WriteLine("Area: {0}", CalculateArea)
Console.WriteLine("----------------------------------")
Console.WriteLine("Cube Volume: {0}", CalculateVolume)
End Sub
End ClassPublic Class Rectangle
Inherits Square
Public Property Height As Double
Public Overloads Function CalculatePerimeter() As Double
Return (Length + Height) * 2.0
End Function
Public Overloads Function CalculateArea() As Double
Return Length * Height
End Function
Public Overloads Sub Describe()
Console.WriteLine("Rectangle")
Console.WriteLine("----------------------------------")
Console.WriteLine("Length: {0}", Length)
Console.WriteLine("Height: {0}", Height)
Console.WriteLine("----------------------------------")
Console.WriteLine("Perimeter: {0}", CalculatePerimeter)
Console.WriteLine("Area: {0}", CalculateArea)
End Sub
End ClassPublic Class Box
Inherits Rectangle
Public Property Width As Double
Public Function CalculateLengthHeightPerimeter() As Double
Return (Length + Height) * 2.0
End Function
Public Function CalculateLengthWidthPerimeter() As Double
Return (Length + Width) * 2.0
End Function
Public Function CalculateHeightWidthPerimeter() As Double
Return (Height + Width) * 2.0
End Function
Public Function CalculateLengthHeightArea() As Double
Return Length * Height
End Function
Public Function CalculateLengthWidthArea() As Double
Return Length * Width
End Function
Public Function CalculateHeightWidthArea() As Double
Return Height * Width
End Function
Public Function CalculateVolume() As Double
Return Length * Height * Width
End Function
Public Overloads Sub Describe()
Console.WriteLine("Box")
Console.WriteLine("==================================")
Console.WriteLine("Face One")
Console.WriteLine("----------------------------------")
Console.WriteLine("Length: {0}", Length)
Console.WriteLine("Height: {0}", Height)
Console.WriteLine("Perimeter: {0}", CalculateLengthHeightPerimeter)
Console.WriteLine("Area: {0}", CalculateLengthHeightArea)
Console.WriteLine("==================================")
Console.WriteLine("Face Two")
Console.WriteLine("----------------------------------")
Console.WriteLine("Length: {0}", Length)
Console.WriteLine("Width: {0}", Width)
Console.WriteLine("Perimeter: {0}", CalculateLengthWidthPerimeter)
Console.WriteLine("Area: {0}", CalculateLengthWidthArea)
Console.WriteLine("==================================")
Console.WriteLine("Face Three")
Console.WriteLine("----------------------------------")
Console.WriteLine("Height: {0}", Height)
Console.WriteLine("Width: {0}", Width)
Console.WriteLine("Perimeter: {0}", CalculateHeightWidthPerimeter)
Console.WriteLine("Area: {0}", CalculateHeightWidthArea)
Console.WriteLine("----------------------------------")
Console.WriteLine("Box Volume: {0}", CalculateVolume)
End Sub
End ClassImports System
Module Preparations
Sub Main(args As String())
Dim sqr As New Square
Dim cub As New Cube
Dim rect As New Rectangle
Dim bx As New Box
sqr.Length = 579.284
cub.Length = 579.284
rect.Length = 752.496
rect.Height = 495.874
bx.Length = 752.496
bx.Height = 495.874
bx.Width = 529.337
sqr.Describe()
Console.WriteLine("==================================")
cub.Describe()
Console.WriteLine("##################################")
rect.Describe()
Console.WriteLine("==================================")
bx.Describe()
Console.WriteLine("==================================")
End Sub
End ModuleSquare ---------------------------------- Length: 579.284 ---------------------------------- Perimeter: 2317.136 Area: 335569.952656 ================================== Cube ================================== Each Face ---------------------------------- Side: 579.284 Perimeter: 2317.136 Area: 335569.952656 ---------------------------------- Cube Volume: 194390304.45437828 ################################## Rectangle ---------------------------------- Length: 752.496 Height: 495.874 ---------------------------------- Perimeter: 2496.74 Area: 373143.201504 ================================== Box ================================== Face One ---------------------------------- Length: 752.496 Height: 495.874 Perimeter: 2496.74 Area: 373143.201504 ================================== Face Two ---------------------------------- Length: 752.496 Width: 529.337 Perimeter: 2563.666 Area: 398323.97515199997 ================================== Face Three ---------------------------------- Height: 495.874 Width: 529.337 Perimeter: 2050.422 Area: 262484.455538 ---------------------------------- Box Volume: 197518502.85452285 ================================== Press any key to close this window . . .
Opening a File
Introduction
As you should be aware now, a project is made of many code files, including C# files. A project can contain dozens, hundreds, or thousands of files. As a solution can contain one project, it can also contain dozens or hundreds of projects. When a solution and its project(s) contain dozens, hundreds, or thousands of files, it can be difficult to locate a file you are looking for. Fortunately, Microsoft Visual Studio provides many tools to assist you.
Opening any File
If you are working with Microsoft Visual Studio, you can open various types of files. The primary types of files you can easily open are text-based files, including those from programming languages: C#, C++, Visual Basic, Python, HTML, CSS, JavaScript, SQL, etc. You can also open graphics file. To start, on the main menu of Microsoft Visual Studio, click File -> Open -> File... Locate the folder or directory where the file exists. Click the desired file and click Open. If the file is easily recognizable, Microsoft Visual Studio would open it and display its contents. If you open a complex file, such as a graphics file, Microsoft Visual Studio would display a warning message box requesting your permission:

If you click Yes on the message box, Microsoft Visual Studio would open and display the file. If you click No, nothing else would happen.
If you select a file that Microsoft Visual Studio can't open or doesn't want to open, it would display an error message box:

Opening a Recent File
Microsoft Visual Studio keeps a list of files that were previously opened. To see that list, on the main menu of Microsoft Visual Studio, click File and position the mouse on Recent Files. By default, Microsoft Visual Studio keeps a list of the ten files that were recently opened. To manage this number, on the main menu of Microsoft Visual Studio, click Tools and click Options... If you want, change the value of the Items To Show In Recent Used File List:

Using the Solution Explorer
One of the easiest ways to open a code file is from the Solution Explorer. To open a file, you can double-click it. As another option, you can right-click the file and click Open. In this case, Microsoft Visual Studio would open the file in the normal Code Editor. Actually, Microsoft Visual Studio includes various editors for different types of files. For example, if you double-click a graphics file, an appropriate window would display it. You can also indicate to Microsoft Visual Studio the type of editor you want it to open a file. To do that, in the Solution Explorer, right-click the desired file and click Open with. A dialog box would display:

In the Open With dialog box, click the desired editor and click OK.
Opening a File from Window
The main menu of Microsoft Visual Studio is equipped with a menu category titled Window. Besides some items that are used to manage some window options, the Window menu item holds a list (or collection) of the files that are currently opened in Microsoft Visual Studio. Every time you open a file, the name (and path) of that file is added to the Window list. To see the list of files, click Window. Each entry displays with an ordered number and the path to the file.
By default, the Window menu item displays 10 items. This number is controlled in the Tools window. To change it, on the main menu of Microsoft Visual Studio, click Tools and click Options. Set the desired number in the Items To Show In Window Menu:

To allow you to see other files, the Window menu is equipped with an item titled Windows... As is a convention in Microsoft Windows operating systems, the three periods (...) indicate that if you click that menu item, a window, in this case a dialog box, would open. In this case, the dialog box would display two columns in a list view and a few buttons:

To open a file, click it and click Activate.
Navigating among the Files
Introduction
You are probably aware that, when you use a browser to access the Internet, you can access the page you recently visited by clicking the previous button. If you previously visited a page and moved back, you can visit the page that is considered the next one. Microsoft Visual Studio provides the same capability. To make this possible, Microsoft Visual Studio keeps an ordered list of documents that are accessed.
Navigating Backward
To access a document that was just recently used:
Navigating Forward
To access a document that is positioned ahead of the current document:
Navigating by Selection
As you may realize, navigating is limited to the document that was just previously accessed. If you want to navigate that was far accessed, you have to keep navigating backward or forwoard, one document at a time. Microsoft Visual Studio provides a better visual solution. To use it, press and hold Ctrl. Then press Tab and release while you are still holding Ctrl. This would present a window that contains a list of all the files that are currently opened:

While you are still holding Ctrl, keep pressing Tab until the desired file is selected. Then release both Ctrl and Tab. The file that was selected would then display.
Introduction to Documents and their Tabs
Overview
As you should be aware already, every time you create or open a file, Microsoft Visual Studio creates a tab for that file. Microsoft Visual Studio provides many options to manage those tabs.
To manage the tabs:
In both cases, refer to the options in the Tabs section:

Accessing a Tab
As you should know by now, to access a document that is represeted with a tab, you can simply click the tab. This action would cause the document to be accessed and displayed. You can then use that document such as simply reading it or editing it.
The Selected Tab and the Solution Explorer
You may already be aware that a project can contain many files, a solution can contain many projects that in turn contain many files, and two or more projects can have files with the same name. By default, when you click a tab to access its file, the Solution Explorer is not aware of the selection. In some (in fact many) cases, when you open a file, you want to know the project to which the file belongs. This can be important if the name of the file can be found in different projects.
When a file is accessed by its tab, you can ask the Solution Explorer to indicate the path to the file. To do this, open the Options window. On the left side, click Projects and Solution. In the right General section, make your decision on the Track Active Item in Solution Explorer.
The Rows of Tabs
When you start a project, Microsoft Visual Studio creates a tab for the first file(s) (some projects start with one file (Console App); some other projects immediately open many files when they start (MVC, Python, etc). Every time you create a file, a tab is added for it. The tabs are added from the left to the right above the Code Editor. The number of tabs depends on the monitor resolution you are using.
Using Multiple Rows of Tabs
When the area allocated to the tabs becomes full, the next new tabs would be added but would be hidden. A solution is to show the tabs in various rows. To make it happen, you have two options:


Practical Learning: Using Multiple Rows of Tabs
The Positions of Tabs
By default, the tabs display horizontally in the top section of the Code Editor. As an alternative, you can position them to the left or the right side of the Code Editor. You have two options:
Revealing or Hiding Multiple Rows of Tabs
After allowing the Code Editor to show multiple tab, you have the option of showing one row of tabs or multiple rows. To to that, position the mouse on the row of tabs, and roll the mouse wheel.
Accessing an Active File
As you should know now, to access a file, you can click its tab. If you have many files, it can be difficult to find the file you want. As an alternative, you can use the Active Files button
on the tabs above the Code Editor.
Practical Learning: Accessing a Tabbed File
Pinning the Order of Tabs
As mentioned already, if you start working on a project, at least one tab would be created for a file. We saw how to close some tabs. We also saw how to create or open file and get their tabs. If no tab is displaying, if you create a new file or open an existing file, the tab for that file is created. Every time you create a new file or open a file, Microsoft Visual Studio creates a tab for the new file and adds that tab to the right side of the existing tab(s). In other words, the tabs are created in the order their files are added. After opening the tabs, if you close the solution, when you re-open it, Microsoft Visual Studio would remember the files that were opened and the order of their tabs.
If you want, you can indicate the order of files and tabs you want. This is done by pining the tabs in the order of your choice. To pin a tab:
If you have no pinned tab, if you pin a tab, its Pin Tab button would become selected. If there is only one row of tabs, the pinned tab would be moved to the left. If there is more than one row of tabs, the new pinned tab would be moved to the left section of the top tab. If you pin another tab, it would be position to the right of the existing pinned tab(s). You can keep pining the tabs as you want. The tabs that are pinned are positioned from left to right in the order they are pinned. The other tabs are kept in the order they were added or opened.
Closing the Tabs
Closing a Tab
If you don't want a file to be represented anymore by its tab, you can remove it. To close a file by its tab, you have many options:
Closing Other Tabs
If you have more than one tab that are currently displaying, you have the option of closing all tabs except one. To proceed, click the tab you want to keep. Right-click that tab and click Close Other Tabs.
Closing all Tabs
If you don't want to display any tab at all, you can close all types but keep the solution and project opened. To close all tabs:
If you have more than one tab that are currently displaying, you have the option of closing all tabs except one. To proceed, click the tab you want to keep. Right-click that tab and click Close Other Tabs.
Practical Learning: Ending the Lesson
|
|
|||
| Previous | Copyright © 2001-2026, FunctionX | Friday 19 December 2025, 18:55 | Next |
|
|
|||