15

I'm getting the following warning message...

Return type of function 'ConnectionNew' is not CLS-compliant.

...for this function:

Public Function ConnectionNew(ByVal DataBaseName As String) As MySqlConnection
      Dim connection As MySqlConnection = Nothing
      connection = getConnection(DataBaseName())
      Return connection
End Function

What does this message mean, and how can I fix it?

Urbycoz
  • 7,247
  • 20
  • 70
  • 108
  • 1
    What is `MySqlConnection`? Where is it defined? The error indicates that is the problem. – Oded Aug 15 '11 at 12:59
  • `MySqlConnection` is a member of `MySql.Data.MySqlClient`. Am I not allowed to use that? – Urbycoz Aug 15 '11 at 13:17
  • 3
    Of course you are allowed. But it is not an assembly that is CLS compliant. And as such, the only thing you can do is follow the answer from @Hans. – Oded Aug 15 '11 at 13:18

1 Answers1

32

It is because you are returning an object of a type that's not CLS compliant. Nothing you can do about that, you didn't write the type. Just acknowledge that you know that it isn't compliant, it isn't otherwise likely to cause any problems. Unless you use the function in another language that doesn't support all the .NET types. Fix:

<CLSCompliant(False)> _
Public Function ConnectionNew(ByVal DataBaseName As String) As MySqlConnection
   '' etc...
End Function
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 1
    Is that underscore deliberate, or a typo? Never mind, I see in a StructLayout "decoration" that it has the space and underscore appended, too, so it is now obvious to me that it was no typo. – B. Clay Shannon-B. Crow Raven Aug 23 '13 at 22:10
  • 4
    The underscore signifies a line continuation in VB, as if you'd written ` Public Function ConnectionNew ...` (sorry to necromance) – mwfearnley Nov 19 '15 at 16:10