3

I've attempted to create an abstracted control to manage some of the state in our application. However, I have run a foul of some CLS issues and was hoping that someone could provide some insight.

I have an enumeration as such:

<Flags()> _
Public Enum FormState
    Read = 1
    Edit = 2
    Insert = 4
End Enum

And a class as such:

Public MustInherit Class Fields
    Inherits System.Web.UI.UserControl

    Public Property State() As Enumerators.FormState
        Get
            Return _State
        End Get

        Set(ByVal value As Enumerators.FormState)
            _State = value
            ToggleState(value)
        End Set
    End Property

    Protected MustOverride Sub ToggleState(ByVal state As FormState)
End Class

When I attempt to compile this code I am left with a warning that the State property is not CLS compliant and neither is the state argument. How come? And how can I correct this problem to remove the warnings?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gavin Miller
  • 43,168
  • 21
  • 122
  • 188
  • If you're just going to ignore the warnings, why do you care about CLS in the first place? – MichaelGG Feb 17 '09 at 23:50
  • 3
    I'd rather understand the warnings and what they signify then have them straight removed/ignored. Sorry for any misunderstanding. – Gavin Miller Feb 18 '09 at 15:03
  • Is _State private? Is your whole ASSEMBLY tagged with the CLSCompliant attribute? Other than that, your code looks pretty valid as far as CLS compliance goes. – Dave Markle Feb 18 '09 at 01:30
  • _State is private - That's the part that baffles me, everything else is CLS compliant up until this point. – Gavin Miller Feb 18 '09 at 15:09

3 Answers3

5

Looking at your code, the enum seems to be part of a class called enumerators. The class is not listed in your code, but I'm assuming that you have full control over it.

The class needs to be tagged with the CLS compliant attribute as well.

Renze de Waal
  • 533
  • 2
  • 5
1

To remove the warnings add the following attributes so that the class, method and property look like this:

<CLSCompliant(False)> _
Public MustInherit Class Fields
    Inherits System.Web.UI.UserControl

    <CLSCompliant(False)> _
    Public Property State() As Enumerators.FormState
        Get
            Return _State
        End Get

        Set(ByVal value As Enumerators.FormState)
            _State = value
            ToggleState(value)
        End Set
    End Property

    <CLSCompliant(False)> _
    Protected MustOverride Sub ToggleState(ByVal state As FormState)
End Class

This signifies to the compiler that you want the warnings removed and that you're aware your code is not CLSCompliant.

Gavin Miller
  • 43,168
  • 21
  • 122
  • 188
  • 2
    It is not just a warning! You are actually telling the code it cannot run in medium trust or anywhere else that CLSCompliance is required. Many security scanners for running an app would prevent it from even loading because of that attribute being set. – Jason Short Sep 24 '09 at 21:31
0

It could be that you do not have an item with value 0.

test
  • 1