To illustrate the general idea behind the use of attributes in VB.NET, let's look at how VBFixedString is used in a real program. The problem that this attribute solves is created by the fact that in VB.NET, a string has the characteristics of an array of Char instances. (An array of Chars, in other words.)
When you create a file using a structure containing strings and VB.NET's FilePut, you don't get what you might expect because VB.NET puts in extra information because it's an array of Chars. Consider this example program. (PrefixString and PostfixString are there just to provide a visual marker in the created file.)
Structure VariableType
Public PrefixString As String
Public myString As String
Public PostfixString As String
End Structure
Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
Dim myRecord As VariableType
FileOpen(1, "F:\TESTFILE.TXT", _
OpenMode.Binary)
myRecord.PrefixString = "X"
myRecord.myString = "AAAAA"
myRecord.PostfixString = "X"
FilePut(1, myRecord)
FileClose(1)
End Sub
When the resulting program is executed, here's what the file looks like in Notepad. To solve this problem, the structure is changed by adding the VBFixedString attribute. (Dim myRecord As FixedType must also be changed.)
Structure FixedType
<VBFixedString(1)> Public PrefixString As String
<VBFixedString(5)> Public myString As String
<VBFixedString(1)> Public PostfixString As String
End Structure
This results in what you need - an all-character file that you can use in other systems.
In the example above, each element - such as Public PrefixString As String - is called the target of the attribute in front of it. All .NET programming elements (assemblies, classes, interfaces, delegates, events, methods, members, enum, struct, and so forth) can be targets of attributes.
If you scan the list of attributes implemented in .NET, you will notice that one way to describe the way .NET uses attributes is to simply change the way .NET works to solve some problem that is difficult to solve with regular language elements. This makes them seem to be sort of a programming 'fudge factor' and you might wonder why Microsoft didn't just make the functions solved with .NET attributes part of the Visual Basic .NET language. The reason is that attributes are stored as metadata in the assembly. This makes them available (using a technique called reflection) both at design time in Visual Studio .NET, at compile time when the compiler can use attributes to customize the way the compiler works, and at runtime. This makes them pretty useful!
Here's a partial list of the ways that predefined attributes are used in .NET from the Microsoft documentation:
- Marking methods using the WebMethod attribute in XML Web services to indicate that the method should be callable over the SOAP protocol.
- Describing your assembly in terms of title, version, description, or trademark.
- Describing which members of a class to serialize for persistence.
- Specifying characteristics used to enforce security.
- Controlling optimizations by the just-in-time (JIT) compiler so the code remains easy to debug.
Next page >
An Example: Custom Attributes > Page
1,
2,
3,
4