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?

4
  • If you're just going to ignore the warnings, why do you care about CLS in the first place?
    – MichaelGG
    Feb 17, 2009 at 23:50
  • 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. Feb 18, 2009 at 1:30
  • 3
    I'd rather understand the warnings and what they signify then have them straight removed/ignored. Sorry for any misunderstanding. Feb 18, 2009 at 15:03
  • _State is private - That's the part that baffles me, everything else is CLS compliant up until this point. Feb 18, 2009 at 15:09

3 Answers 3

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.

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.

1
  • 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. Sep 24, 2009 at 21:31
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.