Multiple-Dimension Arrays

 

Common Conversions

Listed below are the common data conversions you will encounter in programming with the Visual Studio C# compiler. The following examples are in executable form in project DataConversion at Visual Studio 2005\Projects\DemosSourceCode so you can confirm that the data conversions work as advertised.

  1. ANSIString to:

    1. Character string:

       string strString = "Mary had a little lamb."; charString = strString.ToCharArray(); // Do not attempt to terminate this 'charString' with the null char. // The statement 'charString[strString.Length] = '
         string strString = "Mary had a little lamb."; charString = strString.ToCharArray(); // Do not attempt to terminate this 'charString' with the null char. // The statement 'charString[strString.Length] = '\0'' won't work.   
      '' won't work.
    2. Int32 (integer):

       // 'strString' must be an integer! No float allowed here. strString = "457"; int intNumber = Convert.ToInt32(strString); 

      Example:

       strTB1 = textBox1.Text; // (data taken from a text box) string strShortYear = strTB1.Substring(2,2); intYear = Convert.ToInt32(strShortYear); 
    3. Int64 (long):

       // 'strNumber' must be a long! No float allowed here. string strNumber = "444444444"; long lgNumber = Convert.ToInt64(strNumber); 
    4. Double (double-precision floating-point number):

       strString = "433.8395"; double dblNumber = Double.Parse(strString); 

      or

       double dblNumber2 = Convert.ToDouble(strString); 
    5. Decimal (base 10 arithmetic; two digits to right of decimal point):

       strString = "445.33"; decimal decNumber = Decimal.Parse(strString); MessageBox.Show("Item #1E-1 response is '" + decNumber.ToString() + "' ."); 

      or

       decimal decNumber2 = Convert.ToDecimal(strString); 

      Example:

       dBuyNOS = Decimal.Parse(strNOS); dBuyTP = Decimal.Parse(strTP); 
  1. Character string to:

    1. ANSIString:

       charString = "Mary had a little lamb".ToCharArray(); // Create an example string. strString = new string(charString); 
    2. Int32 (integer):

       // 'charString' must be in integer format for 'Convert.ToInt32' to work. No float. charString = "3376".ToCharArray(); strString = new string(charString); // This next statement works ONLY with 'strString'. Won't work with 'charString'. intNumber = Convert.ToInt32(strString); 

      or

       intNumber = Int32.Parse(strString); // Only 'strString' works -- no 'charString'. 
    3. Int64 (long):

       charString = "3394".ToCharArray(); // This 'strString' must be in integer format. No float. strString = new string(charString); lgNumber = Convert.ToInt64(strString); 

      or

       lgNumber = Int64.Parse(strString); // Only 'strString' works -- no 'charString'. 
    4. Double (double-precision floating-point number):

       strString = "1935.006"; charString = strString.ToCharArray(); // charString[strString.Length] = '
         strString = "1935.006"; charString = strString.ToCharArray(); // charString[strString.Length] = '\0'; Not needed. Terminates the program. strString = new string(charString); dblNumber = Convert.ToDouble(strString);   
      '; Not needed. Terminates the program. strString = new string(charString); dblNumber = Convert.ToDouble(strString);
    5. Decimal (base 10 arithmetic; two digits to right of decimal point):

       strString = "144.144"; charString = strString.ToCharArray(); //charString[strString.Length] = '
         strString = "144.144"; charString = strString.ToCharArray(); //charString[strString.Length] = '\0'; Not needed. Terminates the program. strString = new string(charString); decNumber = Convert.ToDecimal(strString);   
      '; Not needed. Terminates the program. strString = new string(charString); decNumber = Convert.ToDecimal(strString);
  1. Int32 (integer) to:

    1. ANSIString:

       intNumber = 565758; strString = Convert.ToString(intNumber); 

      Example:

       string strYearTemp = Convert.ToString(iYear); string strWeekTemp = Convert.ToString(iWeek); 
    2. Character string:

       intNumber = 343536; strString = intNumber.ToString(); charString = strString.ToCharArray(); 
    3. Int64:

       intNumber = 7788; lgNumber = (long) intNumber; 
    4. Double (double-precision floating-point number):

       intNumber = 345; double dblNumber9 = Convert.ToDouble(intNumber); 
    5. Decimal (base 10 arithmetic; two digits to right of decimal point):

       intNumber = 722; decimal decNumber17 = Convert.ToDecimal(intNumber); 
  1. Int64 (long) to:

    1. ANSIString:

       lgNumber = 247902; strNumber = lgNumber.ToString(); 
    2. Character string:

       lgNumber = 1356992; strNumber = lgNumber.ToString(); charString = strNumber.ToCharArray(); 
    3. Int32:

       lgNumber = 456744; intNumber = (int) lgNumber; 
    4. Double (double-precision floating-point number):

       lgNumber = 3478902; double dblNumber14 = (double) lgNumber; 
    5. Decimal (base 10 arithmetic; two digits to right of decimal point):

       long lgNumber3 = 353552; decimal decNumber4 = Convert.ToDecimal(lgNumber3); 
  1. Double (double-precision floating-point number) to:

    1. ANSIString:

       dblNumber = 123456789; strString = Convert.ToString(dblNumber); 
    2. Character string:

       dblNumber = 987654321; strString = Convert.ToString(dblNumber); charString = strString.ToCharArray(); 
    3. Int32:

       double dblStart = 74738.22d; int intFinish = (int)dblStart; 
    4. Int64:

       double dblNow = 834.228; long lgLater = (long)dblNow; 
    5. Decimal (base 10 arithmetic; two digits to right of decimal point):

       dblNumber2 = 77.33456d; decNumber = Convert.ToDecimal(dblNumber2); // This conversion (5E) does not currently work! 
  1. Decimal (base 10 arithmetic; two digits to right of decimal point) to:

    1. ANSIString:

       decNumber = 1024.67m; strString = decNumber.ToString(); 

      Example:

       // Convert all numbers to 'string' and write them on Form2. strPricePerShare = dPricePerShare.ToString(); strNetNOS = dNetNOS.ToString(); 
    2. Character string:

       decNumber = 677.34m; strString = decNumber.ToString(); charString = strString.ToCharArray(); // charString[strString.Length] = '
         decNumber = 677.34m; strString = decNumber.ToString(); charString = strString.ToCharArray(); // charString[strString.Length] = '\0'; Not needed.   
      '; Not needed.
    3. Int32:

       decimal decRowboat = 1375.22m; int intMerrily = Convert.ToInt32(decRowboat); 

      or

       int intMerrily2 = decRowboat.ToInt32(); // This won't work. 
    4. Int64:

       decimal decRowboat2 = 136.207m; // long lgMerrily3 = decRowboat2.ToInt64(); This scheme won't work! 

      or

       long lgMerrily4 = Convert.ToInt64(decRowboat2); // This works. 
    5. Double (double-precision floating-point number):

       decimal decFlow = 3555.35m; //double dblGently = decFlow.ToDouble(); Won't work. 

      or

       double dblGently2 = Convert.ToDouble(decFlow); // This works. 
  1. Char string to integer example:

     charString = "3376.44".ToCharArray(); // Convert to a true number, like 'double'. strString = new string(charString);             // Convert char to string. dblNumber = Convert.ToDouble(strString);        // Convert string to double. // Round the 'double' to NO digits after the decimal point. Math.Round(dblNumber,0);                        // Round off the double number. // Cast the 'double' to an 'integer'. int intAnswer = (int)dblNumber; 

1.  

With FileStream (which only reads bytes of data), is there a way I can read blocks of data?

Answers

1.  

Yes, once FileStream has opened a file, you can read bytes of data and convert them to chars and later strings. Example:

  // Begin file opening with 'FileStream'.   FileStream fs; try {      fs = new FileStream("KTWeeklyNetWorth.dta", FileMode.OpenOrCreate,                                                  FileAccess.ReadWrite);      // Read from the file.      byte[] byteBlockData = new byte[47];      char[] charBlockData = new char[47];      fs.Read(byteBlockData,0,47);      // Convert 'byte' to 'char'.      for(int mm = 0; mm < 47; mm++)      charBlockData[mm] = Convert.ToChar(byteBlockData[mm]);      string strBlockData = new string(charBlockData);      MessageBox.Show("BlockData read is '" + strBlockData + "' ."); } catch {      MessageBox.Show("Cannot open file 'KTWeeklyNetWorth.dta'.");        return; }

If you examine all the conversions shown above, you must conclude that the days of working with character strings are just about over. The ANSIString has every attribute that a character string has, and it keeps track of its length in a subitem named .Length. Below is a list of the queries that can be performed on an ANSIString (a string). Each of these queries returns a bool response, true or false:

  •  Char.IsControl(string str, int Index); 

    Is the character named in Index a control character?

     Char.IsSeparator(string str, int Index); 
  • Is the character named in Index a separator character? (Used in database applications only.)

     Char.IsWhiteSpace(string str, int Index); 
  • Is the character named in Index a blank space?

     Char.IsPunctuation(string str, int Index); 
  • Is the character a comma, colon , semicolon, period, etc.?

     Char.IsSymbol(string str, int Index); 
  • Is the character a symbol (such as the Greek letter alpha)?

     Char.IsDigit(string str, int Index); 
  • Is the character between 0 and 9?

     Char.IsNumber(string str, int Index); 
  • Is the character between 0 and 9?

     Char.IsLetter(string str, int Index); 
  • Is the character between A and Z or a and z?

     Char.IsUpper(string str, int Index); 
  • Is the character uppercase?

     Char.IsLower(string str, int Index); 
  • Is the character lowercase?

     Char.IsLetterOrDigit(string str, int Index); 
  • Is the character a letter or a digit?

For each of the above there is an alternative formulation. For example:

 if(IsDigit(strTB[5])) [One argument]....... 

is the same as:

 if(IsDigit(strTB, 5)) [Two arguments] 
 


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