String Constants
If you ever have to produce output based on a string you'll quickly find yourself needing to embed certain constant values. For example, it's always useful to be able to add a carriage-return linefeed combination to trigger a new line in a message box. One way to do this is to learn the underlying ASCII codes and then embed these control characters directly into your String or StringBuilder object.
Visual Basic provides an easier solution for working with these: the Microsoft.VisualBasic.Constants class. The Constants class, which you can tell by its namespace is specific to Visual Basic, contains definitions for several standard string values that you might want to embed. The most common, of course, is Constants.vbCrLf, which represents the carriage-return linefeed combination. Feel free to explore this class for additional constants that you might need to manipulate string output.
xML literals
One of the main new features in Visual Basic 2008 was the introduction of XML literals. It is possible within Visual Basic to create a new variable and assign a block of well-formatted XML code to that string. This is being introduced here because it demonstrates a great example of a declaration that leverages Option Infer. Start by adding a new form to the VBPro_VS2010 project, accepting the default name of Form2. This new form will be called from the click event for the ButtonTest on Forml using the code shown in the Sub xmlLiteral that follows.
Private Sub XmlLiteral()
Form2.ShowDialog() End Sub
Code snippet from Forml
Within the designer for Form2, drag a RichTextBox control onto the display area and set the control to dock within the parent container. This can be done from the Properties display or by using the Tasks context menu in the upper-right corner of the control. Next, double-click on the form to create an event handler for the window Load event. Within this event you will place the code to demonstrate XML literals. A separate window is being used in order to demonstrate the string formatting capabilities of XML literals, which do not work within a TextBox control.
This code starts by declaring a string variable called myString and setting this to a value such as "Hello World". In the code block that follows, notice that the first Dim statement used does not include the "As" clause that is typically used in such declarations. The declaration of the myString variable relies on type inference:
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load Dim myString = "Embedded string variable data."
Dim myXmlElement = <AnXmlNode attribute="1">This is formatted text. Embedded carriage returns will be kept.
These lines will print separately. Whitespace will also be maintained. <%= myString %>
</AnXmlNode> RichTextBox1.Text = myXmlElement.ToString() & Environment.NewLine & Environment.NewLine RichTextBox1.Text &= myXmlElement.Value.ToString() End Sub
Code snippet from Form2
Running this XmlLiteral Sub results in the output shown in Figure 2-9. Within this code, the compiler recognizes that this newly declared variable is being assigned a string, so the variable is automatically defined as a string. After the first variable is declared on the first line of the code block, the second line of code makes up the remainder of the code block, which you may notice spans multiple lines without any line-continuation characters.
The second Dim statement declares another new variable, but in this case the variable is set equal to raw XML. Note that the "<" is not preceded by any quotes in the code. Instead, that angle bracket indicates that what follows will be a well-formed XML statement. At this point the Visual Basic compiler stops treating what you have typed as Visual Basic code and instead reads this text as XML. Thus, the top-level node can be named, attributes associated with that node can be defined, and text can be assigned to the value of the node. The only
Code snippet from Form2
figure 2-9
requirement is that the XML be well formed, which means you need to have a closing declaration, the last line in the preceding code block, to end that XML statement.
By default, because this is just an XML node and not a full document, Visual Basic infers that you are defining an XMLElement and will define the mXMLElement variable as an instance of that class. Beyond this, however, there is the behavior of your static XML. Note that the text itself contains comments about being formatted. That's because within your static XML, Visual Basic automatically recognizes and embeds literally everything.
Thus, the name XML literal. The text is captured as is, with any embedded white space or carriage returns/ linefeeds captured. The other interesting capability is shown on the line that reads as follows:
This is a shorthand declaration that enables you to insert the value of the variable mystring into your literal XML. In this case, mystring is set on the preceding line, but it could easily be an input parameter to a method that returns an XML element. When you run this code, the current value of mystring will be inserted into your XML declaration.
Two statements display the output shown in Figure 2-9 from your XML element. Two different statements displaying the contents of the XML element as a string appear, because each results in slightly different output.
The first statement on the second line instructs the XML element object to return a string representing itself. As such, the XML element will return all of the content of that object, including the raw XML itself. The second output has output the XML element to a string that only reflects the value of the data defined for that element. Note that if the basic XML element you defined in the previous code block had any nested XML elements, then these would be considered part of the contents of your XML element, and their definitions and attributes would be output as part of this statement.
As shown in Figure 2-9, the result of this output is that the first block of text includes your custom XML node and its attribute. Not only do you see the text that identifies the value of the XML, you also see that actual XML structure. However, when you instead print only the value from the XML block, what you see is in fact just that text. Note that XML has embedded the carriage returns and left-hand white space that was part of your XML literal so that your text appears formatted. With the use of XML literals, you "literally" have the capability to replace the somewhat cryptic string.Format method call with a very explicit means of formatting an output string.
Post a comment