Visual Basic 2010 – Language Features Sample

0 comments

Posted on 4th May 2010 by Admin in Visual Basic 2010

, ,

This sample demonstrates these language features:

  • Operator overloading – TheValidatedStringclass represents a string with custom validation. The&operator returns the concatenation of twoValidatedStringobjects.
  • Generics - ThePairclass holds two values. When you declare an instance of thePairclass, you specify the types of the two values. TheMatchmethod tests to whether the pair types and values are the same.
  • UsingTheUsing 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 theUsingblock.
  • TryCast and IsNotTryCastattempts to cast an object to a particular type and returns the converted value. If the cast fails thenNothingis returned. The IsNotoperator is used to test the return result forNothing.

Download Project

Visual Basic 2010 – Exception-Handling Sample

0 comments

Posted on 4th May 2010 by Admin in Visual Basic 2010

, , ,

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 labeledNo Error Handling, uses various degrees of error handling usingTry,Catch, andFinallyblocks.

  • No error handlingTheFileStreamclass 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, theException Assistantis shown.
  • Basic error handlingThe call to open the file is wrapped in aTry…Catch…Finally Statement (Visual Basic)that catches all errors. The error message is displayed and program execution continues.
  • Detailed error handlingMultipleCatchclauses 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 MessageUsing multipleCatchclauses and the stack trace, the program provides a detailed error message about the exception.
  • Try, Catch, FinallyTheFinallyclause is used to close the file if it has been opened.

Download Project