Friday, October 10, 2008

Get the Product Code of the Installed Product

[Not Sure but can be useful]
Private Function getInstalledProductCode(ByVal InstalledProductNameStr As String) As String
Dim retStr As String = "-1"
Try
Dim installer As Object
installer = CreateObject("WindowsInstaller.Installer")
Dim products As Object = installer.Products
For Each strProductCode As String In products
Dim strProductName As String = installer.ProductInfo(strProductCode, "InstalledProductName")
If strProductName.Contains(InstalledProductNameStr) = True Then
retStr = strProductCode
End If
Next
Catch ex As Exception

End Try
Return retStr
End Function

Generate Globally Unique Identifier (GUID)

Private Function GUID() As String

Return (System.GUID.NewGuid().ToString())

End Function

Monday, October 6, 2008

Attributes with VB.NET

1. Creating and Using Custom Attributes with VB.NET

The common language runtime allows programmers to add descriptive declarations to programming elements called Attributes. Attributes are like annotations that can be applied to assemblies, types, methods, or properties. Attributes are emitted into the assembly metadata and they can be extracted by using Reflection services of the .NET framework.

2. Attributes Programming in VB.NET

An attribute is a new code level language construct in all major .NET languages. It provides integration of declarative information to assemblies, classes, interfaces, members, etc. at the code level. The information can then be used to change the runtime behavior or collect organizational information. . . . . . .

Method Synchronization [MethodImplAttribute]

Code attributes in the Dot Net Framework can sometimes make programming easier. The MethodImplAttribute is one example of the hundreds of different attributes that you can use. It is in the System.Runtime.CompilerServices namespace. This attribute is particularly interesting to synchronization because it can synchronize an entire method with one simple command.

If you place the attribute before a function and supply the MethodImplOptions. Synchronized enumeration in the constructor, the entire method will be synchronized when called. The compiler will create output that wraps the whole function, SynchronizedMethodName, in a Monitor. Enter and Monitor. Exit block. When a thread starts to enter the method it will acquire a lock. Upon exiting the method, it will release the lock. Here is an example of using the attribute.

"<" METHODIMPLATTRIBUTE(METHODIMPLOPTIONS.SYNCHRONIZED) ">"

Private Sub SynchronizedMethodName()

End Sub

This attribute should only be used when an entire function needs to be synchronized, so it is rarely used. If you can exit the synchronized block of code before the end of the method or wait to enter it to the middle of the method, Monitor should be used, as the attribute would waste processing cycles by locking the whole method and not just what needs to be synchronized.


Note: There are two SynchronizationAttribute classes. One is in
System.EnterpriseServices and the other is in
System.Runtime.Remoting.Contexts. The first can only be used on
classes derived from ServicedComponent. The second can only be used on
classes derived from ContextBoundObject.

Syntax for a class

Class Statement [ Public | Private | Protected |
Friend | Protected Friend ]
[ Shadows ][ MustInherit | NotInheritable ] _
Class name [ Inherits classname ][ Implements interfacenames ]
[ statements ]
End Class

Modifiers Brief explanation

Public
There are no restrictions on the use of a class declared as Public.

Private
A Private class is accessible only within its "declaration context" including any nested entities. A nested procedure is an example of a nested entity. A Public variable in a Private class is accessible from inside the class, but not from outside that class.

Protected
Protected means that elements are accessible only from within their own class or from a derived class. Protected can also be used with the Friend keyword. When they're used together, elements are accessible from the same assembly, from their own class, and from any derived classes.

Protected, Friend and Protected Friend

[These Definitions may not be exact definitions but can give a brief
idea]
Protected --> Accessible ONLY by 1.Derived classes 2.Within
the same class
Or
1. Code within the class
2. Code in derived classes within the same assemblies
3. Code in derived classes in other assemblies

A Protected procedure may only be declared within a class.
Procedures declared within the same class can call it, as
can other methods in inherited classes.[From MSDN]

methods in inherited classes.
Protected variables can be used within the class as well as
the classes that inherites this class.

Friend --> Accessible ONLY by 1.Derived classes 2.Classes
in the same assembly 3.Within the same class
Or
1. Code within the class
2. Code in derived classes within the same assembly
3. Other code in the same assembly

Friends can be accessed by all classes within assembely but
not from outside the assembely.

A variable declared with the Friend access specifier can only
be seen by other classes within the same program. This means
that if a class has a Friend variable and that class resides
in a DLL, then only other classes within the same DLL can see
that Friend variable. Any other program that uses that DLL
cannot see that variable.[From MSDN]


Protected Friend --> Accessible ONLY by 1.Derived classes
2.Classes in the same assembly 3.Within the same class
Or
1. Code within the class
2. Code in derived classes within the same assembly
3. Other code in the same assembly
4. Code in derived classes in other assemblies

The Protected Friend can be accessed by Members of the
Assembely (Friend) or the inheriting Assembely class
(Protected).