In general, there are two types of attributes in VB.NET: predefined and custom. A predefined attribute is defined and used by .NET itself. For example, the Common Language Runtime (CLR) uses an attribute - AssemblyVersionAttribute - to use the right features supported by an assembly at runtime. VB.NET uses the DefaultValueAttibute to set the value of a type - for example, an Integer variable will default to 0 - at design time.
One of the most commonly used attributes is the SerializableAttribute. This lets programmers tell the CLR that instances of a class can be serialized, that is, converted into a bit stream or XML characters so it can be saved in a file or transmitted in a network. As a starting example, this is what SerializableAttribute looks like in a code example:
' Instances of this class need to be serialized
<Serializable()> Public Class theClass
' Code in the class
End Class
For example, suppose this class is a "CustomerAccounts" class. Instances of this class will probably contain information (properties) like the customer's name and account number. To send the whole instance of a CustomerAccounts class over a network, it has to be declared as <Serializable()>.
' Instances CustomerAccounts
' must be sent over a network
<Serializable()> Public Class CustomerAccount
Public CustomerName As String
Public CustomerAccountNumber As Integer
Sub New(ByVal CustName, ByVal CustAcct)
' Code to create a new instance
End Sub
End Class
' Example code using this class
Dim theCustName As String
Dim theCustAcct As Integer
theCustName = "George"
theCustAcct = 12345
Dim theCustomer
As New CustomerAccount(
theCustName, theCustAcct)
' Code to send theCustomer over a network,
' write it to a file, or some other action
' that requires a bit stream rather than an
' object would be placed here.
There are predefined attributes for different entities from the top to the bottom in .NET. You can use an attribute to modify assemblies, types, methods, properties, classes, and structures. The syntax is the same (shown below) except for assemblies and assembly modules (an assembly module is not a VB.NET module). In those cases, you have to specify a target for the attribute as shown below (extracted from Microsoft's documentation):
Imports System.Reflection
<Assembly: AssemblyTitleAttribute(
"Production assembly 4"),
Module: CLSCompliant(True)>
Public Class Whatever
' The class code continues ....
Again, Microsoft's documentation can be confusing. A chart on the MSDN page shows "Not supported" for VB for everything except assemblies and assembly modules. That only means that the target parameter syntax shown above isn't used in VB, not that you can't use an attribute for that target.
