Basics

The following example represents a class definition that does several different types of conversions. The first conversion changes the data type from a number to a string. The second conversion demonstrates a conversion from a string to a number. Finally, the dates are converted into different formats.

Example:

using System;

public class ApplyConversion {

static void Main()

string strExample = "1.2"; string strDate;

// This is how to convert a string to number double dblValue = Double.Parse(strExample);

// This is how numerics are converted to strings string strDoubleValue = dblValue.ToString();

// This is conversion of full dates to other data types strDate = DateTime.Now.ToLocalTime().ToString(); strDate = DateTime.Now.ToLongTimeString(); strDate = DateTime.Now.ToShortDateString();

J|| ConvertVariables.cs - Notepad

File Edit Format Help using System, class ConvertVarlables {

static void Maln() {

byte bteValue = 3; nt IntValue - bteValue,

Console.WrlteLlneC'The value for the byte is {0} and the value for the Integer is {1}",bteValue,IntValue);

long InqMaxValue = lnt64.MaxValue;|

nt IntOverflowValue = (Int) IngMaxValue.l

Console.WrlteLlne("Convertlng a maximum long, {0}, to an Integer Is {1}", IngMaxValue, IntOverflowValue);

}

J

Note: This is an example of an explicit conversion.

Declare a long variable and initialize the value to the maximum value for a long variable.

—, Declare an integer variable and initialize the value to the byte variable created.

^]C:WIHNT\5ystem32licind.e«e

Microsoft Windows 2000 [Ue CO Copyright 1985-2000 Mi, on 5.00.2195] soft Corp.

C:\Documents and Settings\Adninistrator>cd CsSCSharp C:\Csharp>csc ConuertUariables.cs

Microsoft <R> Uisual Ctt Compiler Uersion 7.00.9148 IG 6151

Copyright CO Microsoft Corp 2000. All rights reseruei

9148 [CLR version wl.l

:SCsharp>ConyertUarîables.exe The value for the byte is 3 and the value for the integer is 3 Converting a maximum long, 9223372036854775807, to an integer is -1

Note: This is an example of an explicit conversion.

Note: See page 35 for more information on saving a file.

—E Compile and run the program.

■ The program displays the message about the byte and long variable conversions.

ENUMERATE A COLLECTION

Collections give you a sophisticated way to work with a group of items. One collection type is a single-dimension array. When programming with a single-dimension array, you can use the foreach statement to iterate through the collection.

Looping through a collection, you can use any of the iteration statements that are available in C#. If you use a while or a do statement, you will have to manually move through the collection using the MoveNext method and then check to see when you have reached the end of the collection. This will work, but you will find that using the foreach statement will eliminate the work required to move to the next member and to keep track of where you are in the collection.

The foreach statement automatically increments the position in the collection and automatically stops after the last member has been evaluated. The foreach statement will also put you at the first member of that collection when entering the foreach statement the first time.

Also, with the collection, the statement includes an iteration variable that holds an instance of the member at the current position. You are most likely to use this instance in your embedded statement.

ENUMERATE A COLLECTION

ffj Untitled - Notepad

ENUMERATE A COLLECTION

ffj Untitled - Notepad

^ Untitled - Notepad

File Edit Format Help using System, class EnumerateCollection

D Open your text editor.

Type using System; to import the System namespace and press Enter.

< Type the name of the class you want to create and press Enter.

^ Untitled - Notepad

File Edit Format Help

JSJJ

D Open your text editor.

Type using System; to import the System namespace and press Enter.

< Type the name of the class you want to create and press Enter.

^Q Type { }, placing the opening and closing curly braces on separate lines, to set off the body of the class.

using System, class EnumerateCollection static void Main()

foreach (int ¡Member in iNumberList) {

S Between the curly braces, create the Main function.

—0 Declare an integer variable and initialize the value to 0.

H Declare a collection of integers.

—° Add the foreach statement to display all the members over 5.

Was this article helpful?

0 0

Post a comment