Click here to Skip to main content
Licence CPOL
First Posted 9 Mar 2008
Views 17,080
Bookmarked 12 times

Writing CLS-Compliant Structures in Visual Basic 2008

By | 9 Mar 2008 | Article
Techniques for writing your own Structures which adhere to the Microsoft CLS, using Visual Basic 2008.

Introduction

Most 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 code

When you implement your own object types using Visual Basic (typically, writing Structures), you must override some members which are inherited by Structures. You are obliged to do this if you’re writing a CLS-compliant assembly.

Particularly, CLS-compliant structures must override the equality and difference operators, and the Equals and GetHashCode methods.

The last mentioned operation can also be accomplished by implementing the IEqualityComparer interface, which provides definitions for both methods.

The following code shows an example of how to override the operators and the Equals method:

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 Equals, which is provided by the Object class.

Points of interest

This 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Alessandro Del Sole

Other

Italy Italy

Member

I'm a Microsoft Visual Basic MVP. I'm an Italian .NET developer and I write articles and books about Visual Basic, Visual Studio LightSwitch, and the .NET technologies.
 
Check out my blog at: http://community.visual-basic.it/AlessandroEnglish

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generali am new programer for visual basic 2008 Pinmembershitesh15:09 1 Nov '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120528.1 | Last Updated 9 Mar 2008
Article Copyright 2008 by Alessandro Del Sole
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid