May 04

Visual Basic 2010 – XML Comments Sample

This sample demonstrates creating XML comments, viewing them in the editor, and accessing them programmatically.

The Automobile class defines the following type elements, each of which demonstrates a different aspect of XML comments:

  • Types   Automobile and EngineOption
  • Properties   Model
  • Methods   Constructors, Upgrade, CalculateMaxTowing, WillItTow
  • Events   NeedsTuneUp
  • Delegates   RotateTires, ReplaceSparkPlugs
  • Fields   car, modelValue

The Click event handler for the Create Car button calls several of the Automobile members. When using IntelliSense in the Code Editor, the XML comments are displayed as ToolTips.

The Click event handler for the List Summaries button uses the XmlDocument class to query the XML file that contains the XML comments. When the project is compiled, this XML file is created and stored with the project assembly. The Compile tab of the Project Designer enables you to specify the location and name of this file.

Download Project

May 04

Visual Basic 2010 – Object-Oriented Programming Sample

The project includes several class definitions. Customer is the base class, and contains only three properties. Four other classes are derived from the Customer class. Each inherits the properties of Customer, but adds another category of class member.

  • CustomerPropertySyntax is a modification of the Customer class that sets the AccountNumber property during construction. Once created, the AccountNumbercannot be changed.
  • CustomerWithConstructor is a modification of Customer that includes a constructor that sets all three properties during instantiation.
  • CustomerWithParameterizedProperty is a customization of Customer that adds a property that takes a parameter.
  • CustomerWithSharedMembers adds a shared property and a shared method to the Customer class.

Download Project

May 04

Visual Basic 2010 – Language Features Sample

This sample demonstrates these language features:

  • Operator overloading   – The ValidatedString class represents a string with custom validation. The & operator returns the concatenation of two ValidatedStringobjects.
  • Generics  -  The Pair class holds two values. When you declare an instance of the Pair class, you specify the types of the two values. The Match method tests to whether the pair types and values are the same.
  • Using   The Using Statement (Visual Basic) references a stream resource opened by a request from a Web client. The stream instance is disposed of automatically at the end of the Using block.
  • TryCast and IsNot   TryCast attempts to cast an object to a particular type and returns the converted value. If the cast fails then Nothing is returned. The IsNot operator is used to test the return result for Nothing.

Download Project

May 04

Visual Basic 2010 – Exception-Handling Sample

Five variations on opening a file are demonstrated in the code. There are five command buttons to test. Each one tries to open the file specified in the text box labeledText File To Open. Each button, except the one labeled No Error Handling, uses various degrees of error handling using Try, Catch, and Finally blocks.

  • No error handling   The FileStream class is used to open the file specified in the form. If the file does not exist, an exception is thrown. In release mode, program execution stops. In debug mode, the Exception Assistant is shown.
  • Basic error handling   The call to open the file is wrapped in a Try…Catch…Finally Statement (Visual Basic) that catches all errors. The error message is displayed and program execution continues.
  • Detailed error handling   Multiple Catch clauses are used to provide more detail about the error. By catching specific errors, the program can determine whether the file did not exist, the folder did not exist, or some other I/O error occurred.
  • Custom Message   Using multiple Catch clauses and the stack trace, the program provides a detailed error message about the exception.
  • Try, Catch, Finally   The Finally clause is used to close the file if it has been opened.

Download Project

May 04

Visual Basic 2010 – String Methods Sample

The main form contains a TabControl with three tab pages that demonstrate String member methods, String shared methods, and StringWriter methods. Each tab page enables the user to enter string values and then execute String methods by clicking buttons. The underlying design contains a Method class and a Parameterclass. Each instance of the Method class represents a different String method. This design makes it easy to pass the values entered on the form to the appropriateString method.

Download Project

Method Description
String..::.Insert

String..::.Remove

These methods create and return new String objects. Many of these methods are overloaded and take one, two, or three parameters. The code may ignore some of the input fields in the form.
String..::.IndexOf

String..::.StartsWith

String..::.EndsWith

These methods return information about an existing string, but do not create or modify String objects.
String..::.Format

String..::.Join

These methods often need two Strings to complete a task, or create new strings, and are therefore implemented as Shared methods.
StringBuilder..::.ToString The StringBuilder class enables you to manipulate the characters in the string. The ToString method retrieves the text that is contained by theStringBuilder object.
StringWriter..::.Write

TextWriter..::.WriteLine

StringWriter..::.ToString

The StringWriter class is useful when you have to append text to an output string. The StringWriterclass provides an internal buffer to which you can write text as if you were writing to a file. The Writeand WriteLine methods append text to the buffer. The ToString method retrieves the text that is contained by the StringWriter object.

The buttons listing String class methods are actually RadioButton controls. The button appearance is obtained by setting the Appearance property to Button. The controls resemble buttons, but stay selected when clicked.

The buttons used to select the String class methods all call into the same event handler, HandleCheckedChanged. This procedure uses many Handles clauses. Inside the procedure, an If…Then statement uses the sender parameter to determine which button was selected and acts appropriately.

There is no way to float controls on top of a tab control, so that a single instance of a group of controls appears on each page. To provide that feature in this sample, selecting a page on the tab control sets the Parent property of a Panel control that contains all the “common” controls to be the selected page, like this:

pnlDemo.Parent = tabStringDemo.SelectedTab

To trigger a breakpoint so that you can walk through the StringBuilder and StringWriter code, the sample uses the Debugger..::.Break method. This method is called if the CheckBox control labeled Step through code is selected.