Home

Introduction to Variables: Accessory Data Types

 

Strings

A string is an empty space, a character, a word, or a group of words that you want the compiler to consider "as is", that is, not to pay too much attention to what the string is made of, unless you explicitly ask it to. This means that, in the strict sense, you can put in a string anything you want.

Primarily, the value of a string starts with a double quote and ends with a double-quote. An example of a string is "Welcome to the World of C# Programming!". You can include such a string in Console.Write() to display it on the console. Here is an example:

using System;

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

This would produce:

Welcome to the World of C# Programming!

Sometimes, you will need to use a string whose value is not known in advance. Therefore, you can first declare a string variable. To do this, use the string keyword (in fact, it is a class) followed by a name for the variable. The name will follow the rules we defined above. An example of a string declaration is:

string Msg;

After declaring a string, you can give it a primary value by assigning it an empty space, a character, a symbol, a word, or a group of words. The value given to a string must be included in double-quotes. Here are examples of string variables declared and initialized:

string Empty  = "";
string Gender = "F";
string FName  = "Nelson Mandela";
string Msg    = "Welcome to the World of C# Programming! ";

After initializing a string variable, you can use its name in any valid operation or expression. For example, you can display its value on the console using the parentheses of Console.Write() or Console.WriteLine(). Here is an example:

using System;

class BookClub
{
	static void Main()
	{
		string Msg = "Welcome to the World of C# Programming! ";
		Console.WriteLine(Msg);
	}
}

Practical Learning Practical Learning: Using Strings

  1. To use strings, change the file as follows:
     
    using System;
    
    class Program
    {
        static void Main()
        {
            string  customerName;
            string  customerHomePhone;
    
            byte    shirts;
            decimal priceOneShirt;
    
            byte    pants;
            decimal priceAPairOfPants;
    
            ushort  otherItems;
            decimal priceOtherItems;
    
            uint orderDay;
            uint orderMonth;
            uint orderYear;
    
            double mondayDiscount;
    
            customerName = "Gregory Almas";
            customerHomePhone = "(301) 723-4425";
            shirts = 5;
            priceOneShirt = 0.95M;
            pants = 2;
            priceAPairOfPants = 1.95M;
            otherItems = 3;
            priceOtherItems = 4.55M;
            orderDay = 15;
            orderMonth = 7;
            orderYear = 2002;
            mondayDiscount = 0.25; // 25%
    
            Console.WriteLine("-/- Georgetown Cleaning Services -/-");
            Console.WriteLine("========================");
            Console.Write("Customer:   ");
            Console.WriteLine(customerName);
            Console.Write("Home Phone: ");
            Console.WriteLine(customerHomePhone);
            Console.Write("Order Date: ");
            Console.Write(orderMonth);
            Console.Write('/');
            Console.Write(orderDay);
            Console.Write('/');
            Console.WriteLine(orderYear);
            Console.WriteLine("------------------------");
            Console.WriteLine("Item Type  Qty Unit Price");
            Console.WriteLine("------------------------");
            Console.Write("Shirts      ");
            Console.Write(shirts);
            Console.Write("     ");
            Console.WriteLine(priceOneShirt);
            Console.Write("Pants       ");
            Console.Write(pants);
            Console.Write("     ");
            Console.WriteLine(priceAPairOfPants);
            Console.Write("Other Items ");
            Console.Write(otherItems);
            Console.Write("     ");
            Console.WriteLine(priceOtherItems);
            Console.WriteLine("------------------------");
            Console.Write("Monday Discount: ");
            Console.Write(mondayDiscount);
            Console.WriteLine('%');
            Console.WriteLine("========================");
            Console.WriteLine();
        }
    }
  2. Execute the program. 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%
    ========================
    
    Press any key to continue . . .
  3. Close the DOS window

Dates and Times

A date is a unit that measures the number of years, months, or days elapsed in a specific period. A time is a unit that counts the number of seconds that have elapsed since midnight of the day considered. Although dates and times are large subjects that would require a detailed study, at this time, we will consider them in simple terms.

To declare a variable that would hold date or time values, use the DateTime data type.

using System;

class NumericRepresentation
{
	static void Main()
	{
		DateTime DateHired;
	}
}

The .NET Framework sets its starting periodic date to January 1, 0001 at midnight (12:00:00 or 0:00 AM). If not assigned a specific value (in future lessons, we will learn that this is equivalent to declaring a variable using the default constructor), the variable is initialized to 1/1/0001 at midnight.

Objects

The object data type (it is a class) is used to declare a variable whose type is not primarily defined and can be any of the other data types we have introduced. Here is an example:

using System;

class Exercise
{
	static void Main()
	{
		object Whatever;
	}
}

After creating an object variable, you can use its value as you see fit. For example, you can enter the variable in the parentheses of Console.Write() or Console.WriteLine() to display it in the console window.

Practical Learning Practical Learning: Using the Object Type

  1. To start another application, on the main menu, click File -> New Project...
  2. In the Templates list, click Console Application and change the Name to RealEstate2
  3. Click OK
  4. Change the file as follows:
     
    using System;
    class Program
    {
        static void Main()
        {
            object PropertyNumber = "293749";
            object PropertyType = 'S';
            object Stories = 3;
            object Bedrooms = 4;
            object Value = 425880;
    
            Console.WriteLine("=//= Altair Realty =//=");
            Console.WriteLine("Properties Inventory");
            Console.Write("Property #:    ");
            Console.WriteLine(PropertyNumber);
            Console.Write("Property Type:  ");
            Console.WriteLine(PropertyType);
            Console.Write("Stories:        ");
            Console.WriteLine(Stories);
            Console.Write("Bedrooms:       ");
            Console.WriteLine(Bedrooms);
            Console.Write("Market Value:   ");
            Console.WriteLine(Value);
        }
    }
  5. Press Ctrl + F5 to execute the application. This would produce:
     
    =//= Altair Realty =//=
    Properties Inventory
    Property #:    293749
    Property Type:  S
    Stories:        3
    Bedrooms:       4
    Market Value:   425880
    Press any key to continue . . .
  6. Close the DOS window
 

Previous Copyright © 2006-2007 FunctionX, Inc. Next