Tuesday 7 February 2012

Some Usefull Code Samples For VB.NET Developers

Here I wrote some very useful code samples and some are in form of functions. Hope you like.

This function creates an empty Access database

 Public Shared Function CreateAccessDatabase(ByVal DatabaseFullPath As String) As Boolean
  Dim bAns As Boolean
  Dim cat As New ADOX.Catalog()
  Try
   Dim sCreateString As String
   sCreateString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DatabaseFullPath
   cat.Create(sCreateString)
   bAns = True
  Catch Excep As System.Runtime.InteropServices.COMException
   bAns = False
  Finally
   cat = Nothing
  End Try
  Return bAns
 End Function


Collection Class Sample

 Public Class DataPairCollection
  Inherits System.Collections.CollectionBase
  Public Sub Add(ByVal NewPair As DataPair)
   List.Add(NewPair)
  End Sub

  Public Sub Add(ByVal NDataName As Object, ByVal NScreenVal As Object)
   List.Add(New DataPair(NDataName, NScreenVal))
  End Sub

 End Class
 Public Class DataPair
  Public DataName As Object
  Public ScreenVal As Object

  Public Sub New(ByVal NDataName As Object, ByVal NScreenVal As Object)
   DataName = NDataName
   ScreenVal = NScreenVal
  End Sub
 End Class


Dynamically Create Object From String

 Public Function DynamicallyLoadedObject(ByVal objectName As String, Optional ByVal args() As Object = Nothing) As Object
  Dim returnObj As Object = Nothing
  Dim type As Type = Assembly.GetExecutingAssembly().GetType(Application.ProductName & "." & objectName)

  If Not type Is Nothing Then
   returnObj = Activator.CreateInstance(type, args)
  End If

  Return returnObj
 End Function

No comments:

Post a Comment