VB2005 Tips and Tricks – part 10

June 22nd, 2007

The Wonderful World of Snippets

Type ? and hit your TAB key and you will get them.

If you know the first letters of your snippet, like for example “property”, type “prop?” and then hit the TAB key. The snippet that creates a property in a class will be launched.

Here are the most used snippets:

Application – Compiling, Resources and Settings -> Load an Assembly at Run Time; appLoad
Application – Compiling, Resources and Settings -> Write to a Text File; appLog
Collections and Arrays -> Iterate through a collection; colIter
Collections and Arrays -> Locate an Element in an Array; arrLoc
Connectivity and Networking -> Create an E-Mail Message; conEmail
Data – Designer features and ADO.NET -> Create a Server based SQLConnection to SQL Server; adoCreateSqlConn
Data – Designer features and ADO.NET -> Create a Parameterized SELECT Command; adoSelectwParams
Data Types – defined by Visual Basic -> Convert an Array of Bytes into a String; typeByteStr
Data Types – defined by Visual Basic -> Parse an E-Mail Address; typeParseEmail
File System – Processing Drives, Folders and Files -> Copy a File; filCopyFile
File System – Processing Drives, Folders and Files -> Create a File; filCreateFile
File System – Processing Drives, Folders and Files -> Read Text from a File; filReadText
File System – Processing Drives, Folders and Files -> Write Text to a File; filWriteText
Math -> Get a Random Number using the Random Class; mathRand
Security -> Encrypt a String; secEncrypt
Security -> Find the Current User’s Name; secUser
Windows Forms Applications -> Forms -> Determine which Control on a Windows Form is selected; formSelect
Windows Forms Applications -> Forms -> Display a Windows Form; formNew
Windows Forms Applications -> Controls and Components -> Add Menu Items to Windows Forms; menuAddItems
Windows Forms Applications -> Sound -> Play a Sound; sndPlay
Windows Operation System -> System Information -> Determine Desktop Display Resolution; sysRes
Windows Operation System -> System Information -> Determine the Windows System Directory; sysDir
XML -> Iterate named nodes in XML Document; xmlIter
XML -> Read XML from a File Using XmlTextReader; xmlReadTxt

VB2005 Tips and Tricks – part9

June 22nd, 2007

XML Comments

To use NDoc v. 1.3 with .NET Framework 2.0 do the following:

  1. Copy the bin\net\1.1 and rename it to 2.0
  2. In 2.0 folder, create a NDocGui.exe.config with:

<configuration>
   <startup>
      <supportedRuntime version=”v2.0.50727″ />
   </startup>
</configuration>

That’s all!. If you want to run the NDocConsole.exe just create another NDocConsole.exe.config with the same txt and you are done.

If you want to use the full power of XML Comments in VB, check Recommended XML Tags for Documentation Comments

VB2005 Tips and Tricks – part 8

December 18th, 2006

Use Operators with Custom Objects – Overloading

 

Public Shared Operator + (objA as myClass, objB as myClass ) as MyClass
 'code
End Operator
ObjC = ObjA + ObjB
 

VB2005 Tips and Tricks – part 7

December 14th, 2006

Make Simple Data Types Nullable

 

dim i as Nullable(Of Integer)
If not i.HasValue then
 Console.WriteLine("i is a null value")
End If

i=100
If i.HasValue then
 Console.WriteLine("Nullable integer i = " & i.Value)
End If

VB2005 Tips and Tricks – part 6

December 13th, 2006

Build Typesafe Generic Classes

Example 1
Public Class GenericList(Of ItemType)
 'Some Code
End Class

Example 2
Public Class GenericList(Of Item Type)
Inherits CollectionBase

Public Function Add(ByVal value as ItemType) as Integer
 Return List.Add(value)
End Function

Public Sub Remove(ByVal value as ItemValue)
 List.Remove(value)
End Sub

Public ReadOnly Property Item(ByVal index as Integer) as ItemType
 Get
  Return CType(List.Item(index),ItemType)
 End Get
End Property

End Class
Program Code
Dim List as New GenericList(Of String)
List.Add("blue")
List.Add("red")

List.Add(Guid.NewGuid()) ' Will be detected by the compiler
Example 3 - More than one type parameter in the class
Public Class GenerichashTable(Of ItemType, KeyType)
 Inherits DictionaryBase
 'Code
End Class
 
Example 4 - Define Constraints to Parameters
Public Class SerializableList(Of ItemType as ISerializable)
 Inherits CollectionBase
 'Code
End Class

Public Class ControlCollection(Of ItemType as Control)
 Inherits CollectionBase
 'Code
End Class

If you want your class to create an instance of the Item
Public Class GenericList(Of ItemType as New)
 Inherits CollectionBase
 'Code
End Class

Example 5 - Define more than one constraint
Public Class GenericList(Of  ItemType as {ISerializable, New})
 Inherits CollectionBase
 'Code
End Class

Generics also works with structures, interfaces, deletates and methods.
 

VB2005 Tips and Tricks – part 5

December 12th, 2006

Use Strongly Typed Configuration Settings

Double Click the My Project node in the Solution Explorer and select the Settings tab. Add the Name, Type, Scope and Value of your setting.

From your code, type:

dim aSetting as string
aSetting=My.Settings.YourSettingName

Updating User Settings

If you select User from the Scope drop-down list, you can modify that setting in runtime. The modify your setting and save it like this:

MySettings.YourSettingName = “NewValue”
MySettings.Save()

This will create a file under Documents and Settings\[UserName]\Local Settings\Application Data\[Application Name]\[Unique Directory] for the current user only with the selected value.

VB2005 Tips and Tricks – part 4

December 5th, 2006

Use Strongly Typed Resources

  1. Double click My Project, Resources, select type images, and add an image. Change the resource name to MyImage
  2. Add a picture box or any other control that supports images and type: picturebox1.Image = My.Resources.MyImage

This will embed the image in your assembly. If you want to modify the image, replace the image inside the Resource Directory and recompile. There is no need to modify the code.

This approach is better than the ImageList because ImageList does not support typed resources. However, there are controls that do no support it and you have to fill the imagelist with the resource images in your code if you want to use it in this way.

VB2005 Tips and Tricks – part 3

December 4th, 2006

Use the My Objects to Program Common Tasks

Type My. and select:

My.Computer

Information about the current computer, network, mouse and keyboard, printer, screen and clock. Also sounds, files, registry and clipboard

My.Application

Information about current application and context, assembly version, current folder, culture, command line arguments and log events

My.User

Information about current user

My.Forms

Default instances to any form in the app and to communicate between forms without needing to track from references in another class.

My.WebServices

Provides a default proxy-class instance for every web service

My.Settings

Allows you to retrieve custom settings from your application XML config file

My.Resources

Allows you to retrieve resources-blocks of binary or text data that are compiled intro your application assembly

VB2005 Tips and Tricks – part 2

December 4th, 2006

Create XML Documentation for Your Code

Add ”’ before a sub or function, complete the fields, compile and see the xml file in your bin folder. Use NDoc or any other similar program to consume that xml file and create the xml documentation

VB2005 Tips and Tricks – part 1

December 4th, 2006

Symbolic Rename

Right-click on any local variable and select Rename. This will rename all appropriate instances in your code of that variable.

Symbolic Rename works with any property, class or method.

Right click on a form, and select rename. You will see the same result.