A singleton class assures that there is only one instance of the class that exists at any moment of time. There are many approachs of creating a singleton pattern but to make is safer from concurrent access from different processes or to make it thread safe here what you can do:

Public Class Singleton Private Shared myInstance As Singleton

Private Sub New() System.Console.WriteLine(“Instance of Singleton class created at : ” & Now())

End Sub

Public Shared Function GetInstance() As Singleton If myInstance Is Nothing Then

myInstance = New Singleton End If

Return myInstance End Function End Class

This code assures that only one instance of the class is created while you do GetInstance.

You’ll create the instance by getting the shared instance.

Dim lobjSingleton As Singleton = lobjSingleton.GetInstance()

Second and very old approach is two have a Shared count of instances and increment it inside the constructor, inside the constructor check if the count is 0 else throw a Exception. But this approach is not good as a .NET programmer.

-Bugs!

Advertisements

Heres a way to get icons from another exe or dll file. You can use Win32 apis to do this task.

Private Declare Function ExtractIconEx Lib “shell32.dll” Alias “ExtractIconExA” (ByVal lpszFile As String, ByVal nIconIndex As Integer, ByRef phiconLarge As Integer, ByRef phiconSmall As Integer, ByVal nIcons As Integer) As Integer

Private Declare Function ExtractIcon Lib “shell32.dll” Alias “ExtractIconA” (ByVal hInst As IntPtr, ByVal lpszExeFileName As String, ByVal nIconIndex As Integer) As Integer

Private Declare Function DrawIconEx Lib “user32” (ByVal hdc As Integer, ByVal xLeft As Integer, ByVal yTop As Integer, ByVal hIcon As IntPtr, ByVal cxWidth As Integer, ByVal cyWidth As Integer, ByVal istepIfAniCur As Integer, ByVal hbrFlickerFreeDraw As Integer, ByVal diFlags As Integer) As Integer

Private Declare Function DestroyIcon Lib “user32.dll” (ByVal hIcon As Integer) As Integer

These are the apis which would be needed.

Explanation and usage:

1. First use the ExtractIconEx to get the number of icons in the specified file.

Dim numOfIcons as Integer = ExtractIconEx(“filepath”, -1, 0&, 0&, 0&)

2. Using FOR loop get handles [i.e iconHandle] to all the icons using the ExtractIcon

3. Create the icon object from the handles

Dim myicon as System.Drawing.Icon = System.Drawing.Icon.FromHandle(iconHandle)

And you are all done. Now that you have the icon, you can use it in your application or write a bitmap out of it.

-Bugs!

Any of today vb programmer knows how to dynamically increase or decrease a array size. In vb its damn simple, Redim it.

Redim and Preserve looks cool but has performace issues. A ReDim actually creates a new array and copies the old contents to it to Preserve it. This happens seamlessly in vb and vb .net, until you have some kind of performance monitering tool.

Take a look for yourself by ReDim’ing a array up till 5000 elements one by one.

Solution to the problem is using System.Collections.ArrayList and do the simple add() operation.

 -Bugs!