|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionMost developers already know what the Common Language Specifications (CLS) are: a set of rules that every developer should adhere to, to make sure their managed assemblies can work even when referenced by applications written with programming languages different from the one used by the developer herself. In this article, we’re going to focus on writing CLS-Compliant structures in Visual Basic 2008. As an extension, this code will work also in Visual Basic 2005 and .NET 2.0. Using the codeWhen you implement your own object types using Visual Basic (typically, writing Particularly, CLS-compliant structures must override the equality and difference operators, and the The last mentioned operation can also be accomplished by implementing the The following code shows an example of how to override the operators and the Public Structure MyStructure
'If our assembly is CLS-Compliant, we have to override some members
Public Shared Operator =(ByVal obj1 As MyStructure, _
ByVal obj2 As MyStructure) As Boolean
Return obj1.Equals(obj2)
End Operator
Public Shared Operator <>(ByVal obj1 As MyStructure, _
ByVal obj2 As MyStructure) As Boolean
Return Not obj1.Equals(obj2)
End Operator
Public Overrides Function Equals(ByVal obj As Object) As Boolean
Return Object.Equals(Me, obj)
End Function
'Add other code here
............
End Structure
To compare two objects, we can call the shared method Points of interestThis is just an example, but I think it’s really important to take care of such situations when writing CLS-compliant class libraries. Anyway, remember that if you want to be sure of your assembly being CLS-compliant, you can analyze it with the free Microsoft FxCop analysis tool.
|
||||||||||||||||||||||