1 / 146

.NET Fundamentals

.NET Fundamentals. November 13, 2003 Week 2. Class Agenda – November 13, 2003. Questions / Homework? C# Windows Forms Simple Calculator Class Exercise (part 1) More Windows Controls Clock Class Exercise MessageBox / Exceptions in .NET Simple Calculator Class Exercise (part 2)

jihan
Télécharger la présentation

.NET Fundamentals

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. .NET Fundamentals November 13, 2003 Week 2

  2. Class Agenda – November 13, 2003 Questions / Homework? C# Windows Forms Simple Calculator Class Exercise (part 1) More Windows Controls Clock Class Exercise MessageBox / Exceptions in .NET Simple Calculator Class Exercise (part 2) Homework Assignment

  3. Questions? • Common Email List • Homework Packaging / winzip project

  4. Homework • Add name near or at the top of your code your name – Homework Week 1 – date • Zip entire project • I will acknowledge receipt of homework

  5. Class Goals

  6. Class Goal #1 Provide an overview of the .NET Architecture and it’s Major Components • Programming Languages • ADO.NET • ASP.NET • Web Services • XML Integration

  7. Class Goal #2 Understand the .NET Frameworks as an Object Oriented, Strongly Typed, Computing Environment

  8. Class Goals #3 Work with various .NET Framework components • Common Language Runtime (CLR) • .NET Framework Class Library (FCL) • Assemblies • Strong Names • The Global Assembly Cache (GAC) • ADO.NET • ASP.NET • Web Services

  9. Class Goals #4 Develop a basic fluency programming in C#

  10. Course Schedule

  11. C#

  12. Data Types

  13. A word on types • All types in .NET derive from System.Object • They all provide implementations of ToString() and GetType() • To get a string with the type of any variable, you can call <var>.GetType() • Whenever you call Console.WriteLine(obj) the ToString() method on obj is implicitly called. The default ToString implementation for classes simply returns the name of the class.

  14. Primitive Types

  15. Primitive Types (continued)

  16. Boolean bool bTmp; bTmp = True; bool bTmp = False; Boolean variables are stored as 16-bit (2-byte) numbers, but they can only be True or False. Use the keywords True and False to assign one of the two states to Boolean variables.

  17. Integer Data Types Integer variables are stored as signed 32-bit (4-byte) integers ranging in value from -2,147,483,648 through 2,147,483,647. The Integer data type provides optimal performance on a 32-bit processor, as the smaller integral types are slower to load and store from and to memory. (Int32) Short variables are stored as signed 16-bit (2-byte) integers ranging in value from -32,768 through 32,767. (Int16) Long variables are stored as signed 64-bit (8-byte) integers ranging in value from -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807. (Int64)

  18. Defining Integers int i; int i, j, k; int i = 12; j = i;  j is now equal to 12 i = 15; k = i + j;  k is equal to 27 To write an Integer, convert it to a String using: k.ToString();

  19. Floating Point Data Types Double variables are stored as signed IEEE 64-bit (8-byte) double-precision floating-point numbers ranging in value from -1.79769313486231570E+308 through -4.94065645841246544E-324 for negative values and from 4.94065645841246544E-324 through 1.79769313486231570E+308 for positive values. Single variables are stored as signed IEEE 32-bit (4-byte) single-precision floating-point numbers ranging in value from -3.4028235E+38 through -1.401298E-45 for negative values and from 1.401298E-45 through 3.4028235E+38 for positive values. Single-precision numbers store an approximation of a real number.

  20. Double .01, 12345.424, -10.0, 1235000.9999, 125.75 double dTmp; dTmp = 12.625; double dTmp = 12.625; Double variables are stored as signed IEEE 64-bit (8-byte) double-precision floating-point numbers ranging in value from -1.79769313486231570E+308 through -4.94065645841246544E-324 for negative values and from 4.94065645841246544E-324 through 1.79769313486231570E+308 for positive values.

  21. Number Formatting Returns a string formatted according to instructions contained in a format String expression. double aDbl; aDbl.ToString("..."); << insert formating

  22. Sample Double Formatting Code double aDbl = 1234.567; MyStr = aDbl("##,##0.000") ' Returns "1,234.567". MyStr = aDbl("###0.00") ' Returns "1234.57".

  23. Strings

  24. Fundamentals of Strings Strings • A series of characters treated as a single unit • String literals  “How are you?” • Objects of class String

  25. How do we define strings? string strTmp; strTmp = “time will tell”; string strTmp = “time will tell”; strTmp = Console.ReadLine(); string strTmp2; strTmp2 = strTmp; strTmp2  “time will tell”

  26. Concatenating Strings string strCity = “Calais”; string strState = “ME”; string strZip = “04270”; string strLoc; strLoc = strCity + “, ” + strState + “ ” + strZip; strLoc  “Calais, ME 04270”

  27. String Functions string strTmp; strTmp.Trim(); – removes leading and trailing spaces strTmp.ToUpper(); – converts string to all upper case strTmp.ToLower(); – converts string to all lower case strTmp.Length; – returns string length as an integer strTmp.SubString() – extracts a substring

  28. String Function Examples string strTmp = “ Hello World ”; strTmp.Trim(); strTmp  “Hello World” string strTmp = “Hello World”; strTmp.ToLower();  “hello world” strTmp.ToUpper();  “HELLO WORLD”

  29. String.Length Function string strTmp; strTmp = “in the beginning”; The value of strTmp.Length is 16. int i; i = strTmp.Length; The value of i is 16.

  30. String.SubString() Function String.Substring(startIndex , length ); Parameters (are Integers) startIndex – Where the substring starts. startIndex is zero-based. length – The number of characters in the substring.

  31. Substring Examples string strTmp; strTmp = “around the world”; strTmp.Substring(0,6);  “around” strTmp.Substring(11,5);  “world” strTmp.Substring(0,strTmp.Length);  “around the world”

  32. Strings and StringBuilder Class

  33. Strings and StringBuilder Class • String processing • Useful in a variety of applications • String classes (System) • General string processing, storage • StringBuilder class (System.Text) • Facilitates efficient construction of strings

  34. Using the StringBuilder Class  The String object is immutable. Every time you use one of the methods in the System.String class, you create a new string object in memory, which requires a new allocation of space for that new object. In situations where you need to perform repeated modifications to a string, the overhead associated with creating a new String object can be costly. The System.Text.StringBuilder class can be used when you want to modify a string without creating a new object. For example, using the StringBuilder class can boost performance when concatenating many strings together in a loop.

  35. Create a new instance of the StringBuilder class You can create a new instance of the StringBuilder class by initializing your variable with one of the overloaded constructor methods, as illustrated in the following example. StringBuilder MyStringBuilder = new StringBuilder("Hello World!");

  36. Setting the Capacity and Length Although the StringBuilder is a dynamic object that allows you to expand the number of characters in the string that it encapsulates, you can specify a value for the maximum number of characters that it can hold. This value is called the capacity of the object and should not be confused with the length of the string that the current StringBuilder holds. For example, you might create a new instance of the StringBuilder class with the string "Hello", which has a length of 5, and you might specify that the object has a maximum capacity of 25. When you modify the StringBuilder, it does not reallocate size for itself until the capacity is reached. When this occurs, the new space is allocated automatically and the capacity is doubled. You can specify the capacity of the StringBuilder class using one of the overloaded constructors. The following example specifies that the MyStringBuilder object can be expanded to a maximum of 25 spaces.

  37. StringBuilder MyStringBuilder = new StringBuilder("Hello World!",5); Additionally, you can use the read/write Capacity property to set the maximum length of your object. The following example uses the Capacity property to define the maximum object length. MyStringBuilder.Capacity = 5; The EnsureCapacity method can be used to check the capacity of the current StringBuilder. If the capacity is greater than the passed value, no change is made; however, if the capacity is smaller than the passed value, the current capacity is changed to match the passed value. The Length property can also be viewed or set. If you set the Length property to a value that is greater than the Capacity property, the Capacity property is automatically changed to the same value as the Length property. Setting the Length property to a value that is less than the length of the string within the current StringBuilder shortens the string.

  38. StringBuilder.methods

  39. Append • The Append method can be used to add text or a string representation of an object to the end of a string represented by the current StringBuilder. The following example initializes a StringBuilder to "Hello World" and then appends some text to the end of the object. Space is allocated automatically as needed. StringBuilder MyStringBuilder = new StringBuilder("Hello World!"); MyStringBuilder.Append(" What a beautiful day."); Console.WriteLine(MyStringBuilder); This example displays Hello World! What a beautiful day to the console.

  40. AppendFormat • The AppendFormat method adds text to the end of the StringBuilder, but also implements the IFormattable interface and therefore accepts the standard format strings described in the formatting section. You can use this method to customize the format of variables and append those values to a StringBuilder. The following example uses the AppendFormat method to place an integer value formatted as a currency value at the end of a StringBuilder. int MyInt = 25; StringBuilder MyStringBuilder = New StringBuilder("Your total is "); MyStringBuilder.AppendFormat("{0:C} ", MyInt); Console.WriteLine(MyStringBuilder); This example displays Your total is $25.00 to the console.

  41. Insert • The Insert method adds a string or object to a specified position in the current StringBuilder. The following example uses this method to insert a word into the sixth position of a StringBuilder. StringBuilderMyStringBuilder = New StringBuilder("Hello World!"); MyStringBuilder.Insert(6, "Beautiful "); Console.WriteLine(MyStringBuilder); This example displays Hello Beautiful World! to the console.

  42. Remove You can use the Remove method to remove a specified number of characters from the current StringBuilder, beginning at a specified zero-based index. The following example uses the Remove method to shorten a StringBuilder. StringBuilder MyStringBuilder = New StringBuilder("Hello World!"); MyStringBuilder.Remove(5, 7); Console.WriteLine(MyStringBuilder); This example displays Hello to the console.

  43. Replace The Replace method can be used to replace characters within the StringBuilder object with another specified character. The following example uses the Replace method to search a StringBuilder object for all instances of the exclamation point character (!) and replace them with the question mark character (?). StringBuilder MyStringBuilder = New StringBuilder("Hello World!"); MyStringBuilder.Replace("!"c, "?"c); Console.WriteLine(MyStringBuilder); This example displays Hello World? to the console.

  44. Iteration Statements

  45. Iteration statements • For loops • for([initializers]; [expression]; [iterators])statement e.g. for (int i = 1; i <= 5; i++) { … } • While loops • while(expression)statement e.g. while (i<=5) {…} • expression has to evaluate to boolean. • Other iteration statements: do, foreach

More Related