30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

636 Strings, Characters and Regular Expressions Chapter 15<br />

22 string3 = New String(characterArray, 6, 3)<br />

23 string4 = New String("C"c, 5)<br />

24<br />

25 output = "string1 = " & quotes & string1 & quotes & _<br />

26 vbCrLf & "string2 = " & quotes & string2 & quotes & _<br />

27 vbCrLf & "string3 = " & quotes & string3 & quotes & _<br />

28 vbCrLf & "string4 = " & quotes & string4 & quotes<br />

29<br />

30 MessageBox.Show(output, "String Class Construc<strong>to</strong>rs", _<br />

31 MessageBoxBut<strong>to</strong>ns.OK, MessageBoxIcon.Information)<br />

32 End Sub ' Main<br />

33<br />

34 End Module ' modStringConstruc<strong>to</strong>r<br />

Fig. 15.1 String construc<strong>to</strong>rs (part 2 of 2).<br />

In line 11, we declare variable quotes and give it the value returned by function<br />

ChrW when ChrW is passed a value of 34. The value passed <strong>to</strong> function ChrW is a Unicode<br />

character code. Function ChrW returns as a Char data type the character that corresponds<br />

<strong>to</strong> the specified Unicode character code. In this case, function ChrW returns a double quote<br />

character ("). (To learn more about character codes, see Appendix F, Unicode.)<br />

Lines 15–16 allocate Char array characterArray, which contains nine characters.<br />

The c suffix that follows each String converts it <strong>to</strong> a character literal. We do this because<br />

Option Strict prohibits the implicit conversion from type String <strong>to</strong> type Char.<br />

Line 19 assigns literal string "Welcome <strong>to</strong> VB.<strong>NET</strong> <strong>Program</strong>ming!" <strong>to</strong> String<br />

reference originalString. Line 20 sets string1 <strong>to</strong> reference String literal<br />

originalString.<br />

Software Engineering Observation 15.1<br />

In most cases, it is not necessary <strong>to</strong> make a copy of an existing String object. All String<br />

objects are immutable—their character contents cannot be changed after they are created.<br />

Also, if there are one or more references <strong>to</strong> a String object (or any object for that matter),<br />

the object cannot be reclaimed by the garbage collec<strong>to</strong>r. 15.1<br />

Line 21 assigns <strong>to</strong> string2 a new String object, using the String construc<strong>to</strong>r<br />

that takes a character array as an argument. The new String object contains a copy of the<br />

characters in array characterArray.<br />

Line 22 assigns <strong>to</strong> string3 a new String object, using the String construc<strong>to</strong>r<br />

that takes a Char array and two Integer arguments. The second argument specifies the<br />

starting index position (the offset) from which characters in the array are copied. The third<br />

argument specifies the number of characters (the count) <strong>to</strong> be copied from the specified<br />

starting position in the array. The new String object contains a copy of the specified<br />

characters in the array. If the specified offset or count indicates that the program should

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!