Common Conversions

 

Strings

The formal name for a string is an ANSIString (ANSI stands for American National Standards Institute). The string is so well supported in the Visual Studio C# compiler that there is no reason to ever use the old, alternate method of grouping characters together (the char string of the C language era, circa 1970). If you have ever used the char string, you know that it had to be terminated with the NULL character, \0, and its length had to be declared in advance (because early compilers had to allocate space in sequential memory for each string; hence each string length had to be declared at startup). Not so with the modern ANSI-String; each string is placed in floating memory and each time the string is modified in any way, a new floating location is established. The length of an ANSIString is contained in a separate construct; for example, the length of a string named strTotal is located in strTotal.Length.

There is one notable case where the char array appears in the Visual Studio C# compiler ” when a file must be read in presized chunks of characters (statement ReadBlock()). In this case the data read from the file is placed in a character array and most programmers immediately transfer the data to a string in the next executable statement. For all practical purposes you will never use the char array again.

Case 1: An Individual String

The declaration is:

 string strTotal; 

or

 private string strTotal; 

To make this declaration global to the form, place it at the top of the class declaration. To make this declaration global to the form and allow this string to be used in other forms, the declaration is:

 public string strTotal 

Case 2: A Collection of Strings

If you declare a collection of strings, you must tell the compiler how many strings are in the collection:

 string[] strBunchOfStrings = new string[33]; 

In this case there are 33 strings and each string has an individual length. The length of string #12 is strBunchOfStrings[11].Length because all counting starts at 0. Most texts call this collection of strings jagged arrays since each array has its own length. To make this collection usable by other forms you must place the word public in front and list it in the class declaration at the top of the *.cs source code file.

 


Unlocking Microsoft C# V 2.0 Programming Secrets
Unlocking Microsoft C# V 2.0 Programming Secrets (Wordware Applications Library)
ISBN: 1556220979
EAN: 2147483647
Year: 2005
Pages: 129

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net