Fundamentals of Variables

Introduction to Values

A value is a piece of information (called data) you need to use in your program. One way to use a value is to display it to the user. To do this, you can include the value in the parentheses of System.Console.Write(). Here are two examples:

class Exercise
{
    static void Main()
    {
        System.Console.Write(248);
    }
}

Practical LearningPractical Learning: Introducing Variables

  1. Start Microsoft Visual Studio
  2. To create a new application, on the main menu, click File -> New -> Project...
  3. In the middle list, click Empty Project (.NET Framework)
  4. Change the Name to GeorgetownDryCleaningServices1
  5. Click OK
  6. To create a file for the code, on the main menu, click Project -> Add New Item...
  7. In the left list of the Add New Item dialog box, click Code
  8. In the middle list, click Code File
  9. Change the Name to CleaningOrder
  10. Click Add
  11. In the empty document, type the following:
    class Exercise
    {
        static void Main()
        {
        }
    }

Intropduction to Variables

A variable is a value that is stored in the computer memory (the random access memory, also called RAM). Such a value can be retrieved from the computer memory and displayed to the user.

Declaring a Variable

Before using a variable in your application, you must let the compiler. Letting the compiler know is referred to as declaring a variable.

To declare a variable, you must provide at least two pieces of information. Actually you have various options. We will start with two of them:

Initializing a Variable

When declaring a variable, you can give it a primiary value. This is referred to as initializing the variable. You can use the following formula:

data-type variable-name = value;

This would be done as follows:

class Exercise
{
    static void Main()
    {
		data-type variable-name = desired-value;
    }
}

The Name of a Variable

A variable must have a name. There are rules you must follow to specify the name of a variable. To start, you must avoid reserved words, also called keywords. These keywords are:

abstract as async await base bool  
continue break byte case catch char checked
class const   is override stackalloc ulong
fixed delegate for lock params static unchecked
internal finally interface float out (generic)    
out (methods) do foreach long private string unsafe
sizeof double goto namespace protected struct ushort
uint else if new (generic) public switch using
decimal enum implicit new (LINQ) readonly this virtual
default event in (foreach) new (variable) ref throw void
  explicit in (generic) null return true volatile
  extern int object sbyte try while
  false   operator sealed short typeof

There are other names that are not considered C# keywords but should be avoided because they may cause a conflict in your code. They are referred to as contextual keywords. They are:

add descending global let remove var
alias dynamic group orderby select where (generic)
ascending from into partial (method) set where (query)
async get join partial (type) value yield
await          

There are some rules we will follow to name things:

Besides these rules, you can also create your own rules but that follow the above restrictions. For example:

C# is case-sensitive. This means that the names Case, case, and CASE are completely different. For example, main is always written Main.

Introduction to Data Types: Strings

Introduction to Strings

A string one or a group of symbols, readable or not. The symbols can be letters (a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, and Z), digits (0, 1, 2, 3, 4, 5, 6, 7, 8, and 9) are non-readable characters (` ~ ! @ # $ % ^ & * ( ) - _ = + [ { ] } \ | ; : ' < ? . / , > "). To create a string, include its symbol, symbols, or combination inside double-quotes.

Displaying a String

To display a string to the user, include its value in the parentheses of System.Console.Write(). Here is an example:

class Exercise
{
    static void Main()
    {
		System.Console.Write("Welcome to the World of C# Programming!");
    }
}

This would produce:

Welcome to the World of C# Programming!Press any key to continue . . .

Creating a New Line

If you use System.Console.Write(), everything displays on the same line. As an alternative, you can use System.Console.WriteLine(). In this case, after the string is displayed, whatever item comes next would display in the next line. Consider the following example:

class Exercise
{
    static void Main()
    {
		System.Console.WriteLine("Welcome to the World of C# Programming!");
    }
}

This would produce:

Welcome to the World of C# Programming!
Press any key to continue . . .

In the same way, you can use any combination of System.Console.Write() or of System.Console.WriteLine() in your code. Here are examples:

class CountryStatistics
{
    static void Main()
    {
        System.Console.Write("Country Name:");
        System.Console.Write(" ");
        System.Console.WriteLine("The People's Republic of China (PRC)");
    }
}

This would produce:

Country Name: The People's Republic of China (PRC)
Press any key to continue . . .

A String Variable

You may want to store the value of a string in a variable. To let you do this, the C# language provides a data type named string. Use it to declare a string-based variable. To provide the value of the variable, specify its value in double-quotes. To display the value, put the name of the variable in the parentheses of System.Console.Write() or of System.Console.WriteLine(). Here are two examples:

class Exercise
{
    static void Main()
    {
        string team = "Real Madrid";
        string country = "Guinée Equatoriale";

        System.Console.WriteLine("Welcome to the World of C# Programming!");
        System.Console.Write("Team:    ");
        System.Console.WriteLine(team);
        System.Console.Write("Country: ");
        System.Console.WriteLine(country);
        System.Console.WriteLine();
    }
}

This would produce:

Welcome to the World of C# Programming!
Team:    Real Madrid
Country: Guinée Equatoriale

Press any key to continue . . .

Introduction to Natural Numbers

Overview

A natural number is a value that contains only digits. To recognize such values, the C# language provides a data type named int. Use it to declare a variable that would hold a natural number. Here is an example:

class Exercise
{
    static void Main()
    {
        int age;
    }
}

The Value of an Integral Variable

To specify the variable of an int type, provide a value that contains only digits. Here is an example:

class Order
{
    static void Main()
    {
        int age = 15;
    }
}

After specifying the value of the variable, to display that value to the user, you can include the name of the variable in the parentheses of System.Console.Write() or System.Console.WriteLine(). Here are two examples:

class Order
{
    static void Main()
    {
        int monthlySalary = 3288;
        
        System.Console.Write("Monthly Salary: ");
        System.Console.WriteLine(monthlySalary);

    }
}

This would produce:

Monthly Salary: 3288
Press any key to continue . . .

The value of an integer can be between -2147483648 and 2147484647 (or -2,147,483,648 and 2,147,484,647). If you need to use a large value, to make it easier to humanly read, you can separate the thousands by underscores. Here are examples:

class CountriesStatistics
{
    static void Main()
    {
        int areaChina = 9_596_961;
        int areaCanada = 9_984_670;
        int areaBurkinaFaso = 275_200;
        int areaDjibouti = 23_200;

        System.Console.WriteLine("Countries Areas");
        System.Console.WriteLine("---------------------");
        System.Console.Write("Djibouti: ");
        System.Console.WriteLine(areaDjibouti);
        System.Console.Write("Burkina Faso: ");
        System.Console.WriteLine(areaBurkinaFaso);
        System.Console.Write("China: ");
        System.Console.WriteLine(areaChina);
        System.Console.Write("Canada: ");
        System.Console.WriteLine(areaCanada);
    }
}

This would produce:

Countries Areas
---------------------
Djibouti: 23200
Burkina Faso: 275200
China: 9596961
Canada: 9984670
Press any key to continue . . .

Introduction to Floating-Point Numbers

Introduction to Decimal Numbers

A floating-point number is a number made of either only digits or two digit parts separated by a number referred to as a decimal separator. To support floating-point numbers, the C# language provides a data type named double. Use it to declare a variable for a number. To provide a value for the value, you can use the same types of numbers we saw for integers. Here are examples:

class StatesStatistics
{
    static void Main()
    {
        double areaMaine = 35385;
        double areaAlaska = 1_723_337;

        System.Console.WriteLine("States Areas");
        System.Console.Write("Maine: ");
        System.Console.WriteLine(areaMaine);
        System.Console.Write("Alaska: ");
        System.Console.WriteLine(areaAlaska);

    }
}

This would produce:

Monthly Salary: 3288
Press any key to continue . . .

A Floating-Point Number with Precision

The primary difference between an integer and a floating-point number is that a decimal number can include a second part referred to as a precision. To specify the precision of a number, after the natural part, add the symbol used as the decimal separator. In US English, this is the period. Here is an example:

class Employment
{
    static void Main()
    {
        double hourlySalary = 25.85;

        System.Console.Write("Hourly Salary: ");
        System.Console.WriteLine(hourlySalary);
    }
}

This would produce:

Hourly Salary: 25.85
Press any key to continue . . .

If the integral part is large, you can use it "as is" or you can separate its thousansds with underscores. Here are examples:

class StatesStatistics
{
    static void Main()
    {
        double areaGuam = 570.62;
        double areaAlaska = 665_384.04;
        double areaSouthDakota = 77115.68;
        double areaTennessee = 42_144.25;

        System.Console.WriteLine("States Areas");
        System.Console.WriteLine("=======================");
        System.Console.Write("Guam: ");
        System.Console.WriteLine(areaGuam);
        System.Console.Write("Alaska: ");
        System.Console.WriteLine(areaAlaska);
        System.Console.Write("Tennessee: ");
        System.Console.WriteLine(areaTennessee);
        System.Console.Write("South Dakota: ");
        System.Console.WriteLine(areaSouthDakota);
    }
}

This would produce:

States Areas
=======================
Guam: 570.62
Alaska: 665384.04
Tennessee: 42144.25
South Dakota: 77115.68
Press any key to continue . . .

Techniques of Declaring and Using Variables

Updating a Variable

When declaring a variable, you don't have to initialize it. This means that you can declare a variable, perform other operations, and then on another line, specify the value of the variable. The rule to observe is that, if you decide to display the value of the variable, you must first specify its value. This can be done as follow:

class Exercise
{
    static void Main()
    {
	data-type variable-name;

	variable-name = desired-value;
    }
}

Here are examples:

class Employment
{
    static void Main()
    {
        string fullName;
        double hourlySalary;

        System.Console.WriteLine("Employee Details");
        System.Console.WriteLine("=======================");

        hourlySalary = 22.27;
        fullName = "Martial Engolo";
        
        System.Console.Write("Employee Name: ");
        System.Console.WriteLine(fullName);
        System.Console.Write("Houly Rate: ");
        System.Console.WriteLine(hourlySalary);
    }
}

This would produce:

Employee Details
=======================
Employee Name: Martial Engolo
Houly Rate: 22.27
Press any key to continue . . .

Still, you can initialize a variable, perform other operations, and then specify a new value for the variable. This means that, at any time, you can change the value of a variable. This is also referred to as updating the variable. When you update a variable, its previously value is lost and it assumes the new value. Here are examples:

class EmployeesRecords
{
    static void Main()
    {
        string fullName = "Martial Engolo";
        double hourlySalary = 22.27;

        System.Console.WriteLine("Employee Details");
        System.Console.WriteLine("=======================");
        System.Console.Write("Employee Name: ");
        System.Console.WriteLine(fullName);
        System.Console.Write("Hourly Rate: ");
        System.Console.WriteLine(hourlySalary);

        hourlySalary = 35.08;
        fullName = "Annette Sandt";

        System.Console.WriteLine("----------------------");
        System.Console.Write("Employee Name: ");
        System.Console.WriteLine(fullName);
        System.Console.Write("Hourly Rate: ");
        System.Console.WriteLine(hourlySalary);
    }
}

This would produce:

Employee Details
=======================
Employee Name: Martial Engolo
Hourly Rate: 22.27
----------------------
Employee Name: Annette Sandt
Hourly Rate: 35.08
Press any key to continue . . .

Declaring Many Variables

As we have seen so far, you can declare as many variables as you want. Here are examples:

class EmployeeDetails
{
    static void Main()
    {
        string status = "Full Time";
        string firstName = "Martial";
        double hourlySalary = 22.27;
        string lastName = "Engolo";
        double timeWorked = 42.50;

        System.Console.WriteLine("Employee Details");
        System.Console.WriteLine("============================");
        System.Console.Write("Employee Name: ");
        System.Console.Write(firstName);
        System.Console.Write(" ");
        System.Console.WriteLine(lastName);
        System.Console.Write("Status: ");
        System.Console.WriteLine(status);
        System.Console.Write("Hourly Rate: ");
        System.Console.WriteLine(hourlySalary);
        System.Console.Write("Time Worked: ");
        System.Console.WriteLine(timeWorked);
    }
}

This would produce:

Employee Details
============================
Employee Name: Martial Engolo
Status: Full Time
Hourly Rate: 22.27
Time Worked: 42.5
Press any key to continue . . .

If you want to declare variables of the same type, you can use their common data type, followed by names separated by commas, and ending with a semicolon. Here is an example:

int width, height, depth;

You can initialize each variable with its own value. You don't have to initialize all variables, only those whose values you want to specify. This can be done as follows:

int width = 228, height, depth = 39;

Other than that, you can use each variable as we have done so far. Here are examples:

class Employment
{
    static void Main()
    {
        string firstName = "Martial", lastName = "Engolo";
        string status = "Full Time";
        double hourlySalary = 22.27, timeWorked = 42.50;

        System.Console.WriteLine("Employee Details");
        System.Console.WriteLine("============================");
        System.Console.Write("Employee Name: ");
        System.Console.Write(firstName);
        System.Console.Write(" ");
        System.Console.WriteLine(lastName);
        System.Console.Write("Status: ");
        System.Console.WriteLine(status);
        System.Console.Write("Hourly Rate: ");
        System.Console.WriteLine(hourlySalary);
        System.Console.Write("Time Worked: ");
        System.Console.WriteLine(timeWorked);
    }
}

Inferring the Value of a Variable

To make it easy for you to declare variables, the C# language provides a keywork named var. You can use it to declare a variable of any type. If you use this keyword, you must immmediately initialize the variable. Unlike the actual data types we have used so far, you must initial a var variable when you declare it. This allows the compiler to figure out the type of value of the variable. The formula to follow is:

var variable-name = value;

Here is an example:

class Employment
{
    static void Main()
    {
        var fullName = "Martial Engolo";
        double hourlySalary;

        System.Console.WriteLine("Employee Details");
        System.Console.WriteLine("=======================");

        hourlySalary = 22.27;
        
        System.Console.Write("Employee Name: ");
        System.Console.WriteLine(fullName);
        System.Console.Write("Houly Rate: ");
        System.Console.WriteLine(hourlySalary);
    }
}

An Object Value

To further increase its flexibility, the C# language provides the object data type that allows you to declare a variable of any type. You can use a common object keyword to declare many variables. Here are examples:

class Employment
{
    static void Main()
    {
        object status = "Full Time";
        object firstName = "Martial", lastName = "Engolo";
        object hourlySalary = 22.27, timeWorked = 42.50;

        System.Console.WriteLine("Employee Details");
        System.Console.WriteLine("============================");
        System.Console.Write("Employee Name: ");
        System.Console.Write(firstName);
        System.Console.Write(" ");
        System.Console.WriteLine(lastName);
        System.Console.Write("Status: ");
        System.Console.WriteLine(status);
        System.Console.Write("Hourly Rate: ");
        System.Console.WriteLine(hourlySalary);
        System.Console.Write("Time Worked: ");
        System.Console.WriteLine(timeWorked);
    }
}

A Dynamic Variable

C# also provides the dynamic keyword that you can use the declare a variable of any type. This can be done as follows:

class Exercise
{
    static void Main()
    {
		dynamic variable-name = desired-value;
    }
}

A Date/Time Value

To use a date value, a time value, or a combination of date and time values, you can use a data type named DateTime. You can use it to declare a variable. Its value must follow the rules of date and time of the computer on which you are writing your code.

The Nullity of a Variable

Introduction

When you declare a variable, you ask the compiler to reserve a certain area and amount of memory to eventually hold a value for that variable. At that time, no clear value is stored in that reserved area of memory. A value is referred to as null when it cannot be clearly determined. A null value is not 0 because 0 is a an actual value. When declaring a variable, if you don't have a clear value for it, you can ask the compiler to treat its value as null.

A Null String

To support null values, the C# language provides a keyword named null. To apply it, initialize the variable with it. The main rule is that you must assign the value before the variable can be used.

When declaring a string variable, if you don't have a value for it, set it as null. Here is an example:

class Employment
{
    static void Main()
    {
        string status = null;
        string firstName = null, lastName = null;
        double hourlySalary = 22.27, timeWorked = 42.50;

        status = "Full Time";
        firstName = "Martial";
        lastName = "Engolo";

        System.Console.WriteLine("Employee Details");
        System.Console.WriteLine("============================");
        System.Console.Write("Employee Name: ");
        System.Console.Write(firstName);
        System.Console.Write(" ");
        System.Console.WriteLine(lastName);
        System.Console.Write("Status: ");
        System.Console.WriteLine(status);
        System.Console.Write("Hourly Rate: ");
        System.Console.WriteLine(hourlySalary);
        System.Console.Write("Time Worked: ");
        System.Console.WriteLine(timeWorked);
    }
}

A Nullable Primitive Type

In C#, normally, not all variables can hold null values. When declaring a variable, to indicate that it can hold either an actual value or it can be null, after its data type, add the question mark. You have two options.

You can assign null when declaring the variable. This would be done as follows:

class Exercise
{
    static void Main()
    {
	data-type? variable-name = null;
	
	// You can use the variable
    }
}

Once again, if you plan to to display the value of the variable, you must first specify its value. Here are examples:

class Employment
{
    static void Main()
    {
        string status = null;
        string firstName = null, lastName = null;
        double?  hourlySalary = null;
        double ? timeWorked = null;
        int     ?category = null;

        status = "Full Time";
        firstName = "Martial";
        lastName = "Engolo";
        category = 3;
        timeWorked = 42.50;
        hourlySalary = 22.27;

        System.Console.WriteLine("Employee Details");
        System.Console.WriteLine("============================");
        System.Console.Write("Employee Name: ");
        System.Console.Write(firstName);
        System.Console.Write(" ");
        System.Console.WriteLine(lastName);
        System.Console.Write("Status: ");
        System.Console.WriteLine(status);
        System.Console.Write("Hourly Rate: ");
        System.Console.WriteLine(hourlySalary);
        System.Console.Write("Time Worked: ");
        System.Console.WriteLine(timeWorked);
        System.Console.Write("Pay Category: ");
        System.Console.WriteLine(category);
    }
}

As another option, you can assign null after the variable has been declared. This would be done as follows:

class Exercise
{
    static void Main()
    {
	data-type? variable-name;
	
	variable-name = null;
	
	// You can use the variable
    }
}

Practical LearningPractical Learning: Using Data Types and Values

  1. To use strings, change the file as follows:
    class Exercise
    {
        static void Main()
        {
            string customerName;
            string customerHomePhone;
    
            int shirts = 0;
            double priceOneShirt = 0;
            int pants = 0;
            double priceAPairOfPants = 0;
            int otherItems = 0;
            double priceOtherItems = 0;
    
            int orderDay = 0;
            int orderMonth = 0;
            int orderYear = 0;
    
            double mondayDiscount = 0;
    
            customerName = "Gregory Almas";
            customerHomePhone = "(301) 723-4425";
            shirts = 5;
            priceOneShirt = 0.95;
            pants = 2;
            priceAPairOfPants = 1.95;
            otherItems = 3;
            priceOtherItems = 4.55;
            orderDay = 15;
            orderMonth = 7;
            orderYear = 2002;
            mondayDiscount = 0.25; // 25%
    
            System.Console.WriteLine("-/- Georgetown Cleaning Services -/-");
            System.Console.WriteLine("========================");
            System.Console.Write("Customer:   ");
            System.Console.WriteLine(customerName);
            System.Console.Write("Home Phone: ");
            System.Console.WriteLine(customerHomePhone);
            System.Console.Write("Order Date: ");
            System.Console.Write(orderMonth);
            System.Console.Write('/');
            System.Console.Write(orderDay);
            System.Console.Write('/');
            System.Console.WriteLine(orderYear);
            System.Console.WriteLine("------------------------");
            System.Console.WriteLine("Item Type  Qty Unit Price");
            System.Console.WriteLine("------------------------");
            System.Console.Write("Shirts      ");
            System.Console.Write(shirts);
            System.Console.Write("     ");
            System.Console.WriteLine(priceOneShirt);
            System.Console.Write("Pants       ");
            System.Console.Write(pants);
            System.Console.Write("     ");
            System.Console.WriteLine(priceAPairOfPants);
            System.Console.Write("Other Items ");
            System.Console.Write(otherItems);
            System.Console.Write("     ");
            System.Console.WriteLine(priceOtherItems);
            System.Console.WriteLine("------------------------");
            System.Console.Write("Monday Discount: ");
            System.Console.Write(mondayDiscount);
            System.Console.WriteLine('%');
            System.Console.WriteLine("========================");
        }
    }
  2. To execute the program, on the main menu, click Debug -> Start Without Debugging. This would produce:
    -/- Georgetown Cleaning Services -/-
    ========================
    Customer:   Gregory Almas
    Home Phone: (301) 723-4425
    Order Date: 7/15/2002
    ------------------------
    Item Type  Qty Unit Price
    ------------------------
    Shirts      5     0.95
    Pants       2     1.95
    Other Items 3     4.55
    ------------------------
    Monday Discount: 0.25%
    ========================
  3. Press Enter to close the DOS window and return to your programming environment

Constants

Introduction

Suppose you intend to use a number such as 39.37 over and over again. Here is an example:

class Exercise
{
    static void Main()
    {
	double meter, inch;
		
	meter = 12.52;
	inch = Meter * 39.37;

        System.Console.Write(meter);
        System.Console.Write(" = ");
        System.Console.Write(inch);
        System.Console.WriteLine("in\n");
    }
}

Here is an example of running the program:

12.52 = 492.9124in

A constant is a value that never changes such as 244, "ASEC Mimosa", or 39.37. These are constant values you can use in your program any time. You can also declare a variable and make it a constant; that is, use it so that its value is always the same.

To let you create a constant, C# provides the const keyword. Type it to the left of the data type of a variable. When declaring a constant, you must initialize it with an appropriate value. Here is an example:

const double ConversionFactor = 39.37;

Once a constant has been created and it has been appropriately initialized, you can use its name where the desired constant would be used. Here is an example of a constant variable used various times:

class Exercise
{
    static void Main()
    {
        const double conversionFactor = 39.37;
        double meter, inch;

        meter = 12.52;
        inch = Meter * ConversionFactor;

        System.Console.Write(meter);
        System.Console.Write(" = ");
        System.Console.Write(inch);
        System.Console.WriteLine("in\n");

        meter = 12.52;
        inch = Meter * ConversionFactor;

        System.Console.Write(meter);
        System.Console.Write(" = ");
        System.Console.Write(inch);
        System.Console.WriteLine("in\n");

        meter = 12.52;
        inch = Meter * ConversionFactor;

        System.Console.Write(meter);
        System.Console.Write(" = ");
        System.Console.Write(inch);
        System.Console.WriteLine("in\n");
    }
}

This would produce:

12.52 = 492.9124in

12.52 = 492.9124in

12.52 = 492.9124in

To initialize a constant variable, the value on the right side of "=" must be a constant or a value that the compiler can determine as constant. Instead of using a known constant, you can also assign it another variable that has already been declared as constant.

Built-in Constants

There are two main categories of constants you will use in your programs. You can create your own constant as we saw above. The C# language also provides various constants. Before using a constant, you must first know that it exists. Second, you must know how to access it.

null: The null keyword is a constant used to indicate that a variable doesn't hold a known value

PI: PI is a constant used as the ratio of the circumference of a circle to its diameter. PI is defined in Math. To use it, you would type Math.PI.

Managing Variables

Introduction

Microsoft Visual Studio provides many tools you can use to manage code and manage your variables. The Code Editor of Microsoft Visual Studio provides many tools to assist you in managing your code.

If you are using Microsoft Visual Studio and if you want to find different occurrences of a known character, symbol, word, or group of words, first select that item. Then:

In the same way, if you have a variable that is used more than once in your code and you want to see all places where that variable is used, simply click the name (and wait a second) and all of its occurrences would be highlighted:

Name Selection

To get a list of all sections where the variable is used, if you are using Microsoft Visual Studio:

Find All References

This would produce a list of all sections where the variable is used and would display the list in the  Find Symbol Results window:

Find All References

To access a particular section where the variable is used, double-click its entry in the list.

Cutting, Copying, and Pasting Code

Normally, from your knowledge of using computers, you probably already know how to select, cut, and copy text. These two operations can be valuable to save code in Microsoft Visual Studio. This means that, if you have code you want to use in different sections, you can preserve it somewhere to access it whenever necessary.

To save code to use over and over again, first type the code in any text editor, whether in Notepad, Microsoft Word, or the Code Editor of Microsoft Visual Studio. You can use code from any document where text can be copied, including a web page. Select that code and copy it to the clipboard. To preserve it, in Microsoft Visual Studio, display the Toolbox (on the main menu, you can click View -> Toolbox). Right-click an empty area on the Toolbox and click Paste.

An alternative is to select the code, whether in the Code Editor or in a text editor. Then drag it and drop it on the Toolbox. In the same way, you can add different code items to the Toolbox. After pasting or adding the code to the Toolbox, it becomes available. To use that code, drag it from the Toolbox and drop it in the section of the Code Editor where you want to use it.

Renaming a Variable

As we will see throughout our lessons, there are many names you will use in your programs. After creating a name, in some cases you will have to change it. Microsoft Visual Studio makes it easy to find and change a name wherever it is used. Consider the following code:

class Order
{
    static void Main()
    {
        int nbr = 148;

        System.Console.WriteLine(nbr);
    }
}

To change the name of a variable, in the Code Editor:

Rename

This would display the Rename dialog box with the primary name. If you want to change it, type the new name:

Rename

If you want the studio to find and change the name inside the comments, click the Search in Comments check box. If the name is used in strings and you want to replace it, click the Search in Strings check box. When you click OK, the Preview Changes - Rename dialog box would come up. It shows the various parts where the name is used and will be replaced. If you still want to replace, click Apply.

Accessing a Variable's Declaration

If you create a long document that has many lines of code, in a certain section you may encounter a variable but you want to find out where it was declared. If you are using Microsoft Visual Studio, to access the place where a variable was declared:

Go To Definition

In both cases, the caret would jump to where the variable was declared.

Accessing a Line of Code by its Index

If you are using the Code Editor of Microsoft Visual Studio, if you create a long document that has many lines of code, if you want to jump to a certain line of code:

This would display a dialog box. Enter the line number and click OK or press Enter.

Practical LearningPractical Learning: Ending the Lesson


Previous Copyright © 2008-2018, FunctionX Next