Archive for December, 2006

VB2005 Tips and Tricks – part 8

Monday, 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

Thursday, 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

Wednesday, 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

Tuesday, 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

Tuesday, 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

Monday, 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

Monday, 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

Monday, 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.