320 likes | 530 Vues
Visual Basic.NET Programming. Chapter 6. Strings, Arrays and Collections. Lecturer: Dr. Nguyen Nam Hong Tel.: (04) 878 1437 Mob.: 091 231 2816 Email: nguyennamhong2003@yahoo.com.au. Chapter Contents. 6.1. Working with Strings. 6.2. Working with Arrays. 6.3. Working with Collections.
E N D
Visual Basic.NET Programming Chapter 6. Strings, Arrays and Collections. Lecturer: Dr. Nguyen Nam Hong Tel.: (04) 878 1437 Mob.: 091 231 2816 Email: nguyennamhong2003@yahoo.com.au Dr. Nguyen Nam Hong, Le Quy Don Technical University
Chapter Contents • 6.1. Working with Strings. • 6.2. Working with Arrays. • 6.3. Working with Collections. Dr. Nguyen Nam Hong, Le Quy Don Technical University
6.1. Working with Strings • 6.1.1. String Properties. • 6.1.2. Using static methods. • 6.1.3. Using Instance Methods. • 6.1.4. String Conversion. • 6.1.5. Formatting Numeric Values. • 6.1.6. Formatting DateTime Values. • 6.1.7. String Manipulation Functions. • 6.1.8. DateTime Functions. Dr. Nguyen Nam Hong, Le Quy Don Technical University
6.1.1. String Properties • Strings have two Properties: Length and Chars • Length: • Gives the total number of characters in the string • Chars: • Gives the array of characters in the string • Chars(i) gives the i-th character in the string Dr. Nguyen Nam Hong, Le Quy Don Technical University
6.1.2. String Static Methods • Static Methods are methods of System.String • Syntax: string.methodName[(ArgList)] • Some most useful Static Methods: 1. Compare(str1,index1, str2, index2,len) 2. Concat (use & instead). 3. Equals(str1,str2): True or False. 4. Join(sepaStr,strArray). 5. Format(“Text1 {pos1}”,value1) Dr. Nguyen Nam Hong, Le Quy Don Technical University
6.1.3. String Instance Methods (1/2) • Instance Methods are methods of Instances • Syntax: InstanceName.MethodName • Some useful Instance Methods: 1. CompareTo(str2): -1, 0 and 1. 2. EndsWith(match): True or False. 3. IndexOf(strFind [,FirstPos] [, Number]) 4. Insert(start, str2) 5. PadLeft(len, char). Dr. Nguyen Nam Hong, Le Quy Don Technical University
6.1.3. String Instance Methods (2/2) 6. PadRight(len,char) 7. Remove(start, count) 8. Replace(find, replace) 9. Split(delim(),pos) 10. StartsWith(match): True or False 11. SubString(start, len) 12. ToCharArray() Dr. Nguyen Nam Hong, Le Quy Don Technical University
6.1.4. String Conversion • Use StrConv function StrConv(Source, ConvType [, LCID]) • Some useful Conversion Type: 1. UpperCase 2. LowerCase 3. ProperCase 4. Unicode 5. FromUnicode Dr. Nguyen Nam Hong, Le Quy Don Technical University
6.1.5. Formatting Numeric Values • Use Instance Method ToString(FormatChar). • Some Useful Numeric Format Characters are C (currency), D (Decimal), F (Fixed-point), N (Number), P (Percent), R (Round-trip), X (Hexadecimal). • Example: MyDbl = 0.12345 msgbox(myDbl.ToString(“c4”) ’ 1.2345 msgbox(myDbl.ToString(“P”) ’ 12.345% Dr. Nguyen Nam Hong, Le Quy Don Technical University
6.1.6. Formatting DateTime Values • Use Instance Method ToString(FormatChar). • Some Useful DateTime Format Characters are d (Short Date), D (Long Date), t (Short Time), T (Long Time), s (Sortable DateTime), u (Sortable DateTime with universal time). • Examples: msgbox(now().ToString(“d”) msgbox(now().ToString(“u”) Dr. Nguyen Nam Hong, Le Quy Don Technical University
6.1.7. String Manipulation Functions (1/2) 1. Asc(char), AscW(str) 2. Chr(num), ChrW(num) 3. LCase(str), UCase(str) 4. InStr([start],source, search [,compare]) 5. InStrRev(source, search [,start] [,compare]) 6. StrComp(str1,str2 [,compare]): 1-, 0 or 1 7. Left(str,num), Right(str,num), Mid(str,start [,len]) 8. Len(str) Dr. Nguyen Nam Hong, Le Quy Don Technical University
6.1.7. String Manipulation Functions (2/2) 9. Ltrim(str), Rtrim(str), Trim(str) 10. Space(num), StrDup(num,char) 11. StrConv(str, conversion) 12. StrReverse(str) 13. Filter(str(), value [, include] [,compare]) 14. Replace(source, find, replace [,start] [,count] [,compare]) 15. Join(str() [, delim]) / Split(str [,delim] [,count] [,compare]) Dr. Nguyen Nam Hong, Le Quy Don Technical University
6.1.8. DateTime Functions (1/2) 1. Now() 2. Day(date) 3. WeekDay(date [,FirstDayOfWeek]) 4. WeekDayName(WeekDay [,abbreviate] [,FirstDayOfWeek]) 5. Month(date) 6. MonthName(date [,abbreviate]) 7. Year(date) 8. Hour(time) 9. Minute(time) Dr. Nguyen Nam Hong, Le Quy Don Technical University
6.1.8. DateTime Functions (2/2) 10. Second(time) 11. DateSerial(year, month,day) 12. DateValue(date) 13. TimeSerial(hours,minutes,seconds) 14. TimeValue(time) 15. DateAdd(interval,number,date) 16. DateDiff(interval, date1, date2 [,FirstDayOfWeek] [,FirstWeekOfYear]) 17. DatePart(interval, date [,FirstDayOfWeek] [,FirstWeekOfYear]) Dr. Nguyen Nam Hong, Le Quy Don Technical University
6.2. Working with Arrays 6.2.1. Declare an Array. 6.2.2. Array Properties. 6.2.3. Array Static Methods. 6.2.4. Array Instance Methods. 6.2.5. Multiple Dimension Arrays. Dr. Nguyen Nam Hong, Le Quy Don Technical University
6.2.1. Declare an Array • Using Dim or Private KeyWords • Syntax: Dim arrName(Size) As DataType Dim arrName() As DataType = {ElemList} Dim arrName() As DataType • Redim Syntax: ReDim arrName(NewSize) ReDim Preserve arrName(NewSize) Dr. Nguyen Nam Hong, Le Quy Don Technical University
6.2.2. Array Properties 1. IsFixedSize: True or False. 2. IsReadOnly: True or False. 3. IsSynchronized: True or False. 4. Length: the total number of elements. 5. Rank: the total number of dimentions. 6. SyncRoot: get an object used when synchronizing access to the array among threads. Dr. Nguyen Nam Hong, Le Quy Don Technical University
6.2.2. Array Static Methods 1. BinarySearch(arr, search) 2. Clear(arr, start, num) 3. Copy(source, dest, num) 4. CreateInstance(EleType, len) 5. IndexOf(arr,Value) 6. LastIndexOf(arr, Value) 7. Reverse(arr, start, num) 8. Sort(arr) Dr. Nguyen Nam Hong, Le Quy Don Technical University
6.2.3. Array Instance Methods (1/2) 1. CopyTo(dest, start) 2. Equals(arr2): True or False. 3. GetHashCode() 4. GetLength(dimen) 5. GetUpperBound(dimen) ‘LowerBound = 0. 6. SetValue(Value, Index) 7. GetValue(Index) 8. ToString() 9. Initialize(): Set initial values of elements. Dr. Nguyen Nam Hong, Le Quy Don Technical University
6.2.5. MultiDimensions Arrays 1. Declare a MultiDimensional Array. 2. Using MultiDimensional Arrays. 3. Changing Array Size. 4. Cloning Arrays. 5. Array of Arrays. Dr. Nguyen Nam Hong, Le Quy Don Technical University
1. Declare MultiDimensional Arrays • Arrays can have up to 32 dimensions. • In your declaration, add one pair of parentheses after the variable name and place commas inside the parentheses to separate the dimensions • Example: Dim My4DArray(5,7,9,12) As Decimal Dim My4DArray(,,,) As Integer Dr. Nguyen Nam Hong, Le Quy Don Technical University
2. Using MultiDimensional Arrays 1. Rank: the number of dimensions 2. GetUpperBound(dimen): the maximum index of elements of the dimension. 3. GetLength(dimen): the number of elements of the dimension. 4. Length: the total number of elements 5. By using nested For loops, You can efficiently process a multi-dimensional array. Dr. Nguyen Nam Hong, Le Quy Don Technical University
3. Changing Array Size • You can resize an array at any time by assigning a different array object to the same array variable, using ReDim statement or a standard assignment statement. • The new array object can have different dimensions, although it must retain the same rank. • In a multidimensional array, you can change only the last dimension when you use Preserve. Dr. Nguyen Nam Hong, Le Quy Don Technical University
4. Cloning Arrays • A shallow copy of an Array copies only the elements of the Array, whether they are reference types or value types, but it does not copy the objects that the references refer to. • The references in the new Array point to the same objects that the references in the original Array point to. • The clone is of the same Type as the original Array. Dr. Nguyen Nam Hong, Le Quy Don Technical University
5. Array of Arrays a jagged array variable • Sometimes the data in your application is multi-dimensional but not rectangular. • In your declaration, add as many pairs of parentheses after the variable name as there are levels of nested arrays • Example: Dim MyJaggedArray()()() As Byte Dr. Nguyen Nam Hong, Le Quy Don Technical University
6.3. Working with Collections 6.3.1. Collections in Visual Basic.NET. 6.3.2. Zero-Based and One-Based Collections. 6.3.3. Index and Key Values. 6.3.4. Adding and Removing Items. 6.3.5. Collections as an Alternative to Arrays. Dr. Nguyen Nam Hong, Le Quy Don Technical University
6.3.1. Collections in Visual Basic.NET • A collection is a way of grouping and managing related objects. • Visual Basic provides a Collection class, with which you can define and create your own collections. Dim myCollection As Collection • The Collection class provides you with the built-in functionality to loop through members using For Each...Next and to reference members by index. Dr. Nguyen Nam Hong, Le Quy Don Technical University
6.3.2. Zero-Based and One-Based Collections (1/2) • A collection is either zero-based or one-based, depending on what its starting index is. • The former means that the index of the first item in the collection is zero, and the latter means that it is one. • The Count property returns the number of items in a collection. • The Item(Index|key) get the Item at the Index or match the key. Dr. Nguyen Nam Hong, Le Quy Don Technical University
6.3.2. Zero-Based and One-Based Collections (2/2) • One-based collections are more intuitive to use, because the index ranges from one to Count. • The index of a zero-based collection ranges from zero to one less than the Count property. • The .NET Framework is standardizing collection as being zero-based. • The Visual Basic Collection class is one-based primarily for the purpose of compatibility with previous versions. Dr. Nguyen Nam Hong, Le Quy Don Technical University
6.3.3. Index and Key Values • Many collections in Visual Basic allow you to access an item using either a numeric index or a String key, as do instances of the Visual Basic Collection class. • You may add items to Collection objects without specifying a key. • Some collections allow only a numeric index. • These collections provide access to their members only through the index, and do not allow you to associate a key. Dr. Nguyen Nam Hong, Le Quy Don Technical University
6.3.4. Adding and Removing Items • the Visual Basic Collection object is a general-purpose programming tool, it's more flexible than other collections. • It has an Add method for putting items into the collection, and a Remove method for taking items out. • Examples: Add(Item [,key] [,Before|After Index|key]) Remove(key|Index) Dr. Nguyen Nam Hong, Le Quy Don Technical University
6.3.5. Collections as an Alternative to Arrays • Although collections are most often used for working with objects, you can use a collection to work with any data type. • In some circumstances, it can be more efficient to store items in a collection than in an array. • You might want to use a collection if you are working with a small, dynamic set of items. Dr. Nguyen Nam Hong, Le Quy Don Technical University