Why is my code not CLS-compliant?
Asked Answered
C

2

11

I've got errors when I build my project:

Warning as Error: Type of 'OthersAddresses.AddresseTypeParameter' is not CLS-compliant C:...\Units\OthersAddresses.ascx.cs

public Address.AddressTypeEnum AddressTypeParameter
    {
        get 
        {
            return _addressTypeParameter;
        }
        set 
        {
            _addressTypeParameter = value;
        }
    }

and this one:

Warning as Error: Type of 'Global.UserInSession' is not CLS-compliant C:...\Global.asax.cs

public static User UserInSession
{
    get
    {
        return (HttpContext.Current.Session["CurrentUser"] == null) 
            ? null 
            : HttpContext.Current.Session["CurrentUser"] as User;
    }
    set
    {
        HttpContext.Current.Session["CurrentUser"] = value;
    }
}

I added attribute [CLSCompliant(false)] before UserInSession and AddresseTypeParameter and it works but I would like to understand why it was not CLS-compliant.

Some more information about classes and enum:

Class User (User.cs)

public class User
    {
        private string _uniqueIdentifier;
        private string _password = string.Empty;
        private string _email = string.Empty;
        private string _passwordQuestion = string.Empty;
        private string _passwordAnswer = string.Empty;
        private string _id_directions_db = string.Empty;
        private string _id_gesab = string.Empty;
        private string _zipCode = string.Empty;
        private string _fonction_id = string.Empty;
        private string _fonction = string.Empty;
        private string _structure_id = string.Empty;
        private string _structure = string.Empty;
        private string _firstName = string.Empty;
        private string _lastName = string.Empty;
        private string _company = string.Empty;
        private string _avatarPath = string.Empty;
        private Role _role = new Role();
        private List<Address> _addressList = new List<Address>();
        private string _otherInformation = string.Empty;
        private MembershipUser _membershipUserAssociated = null;
        ...

        public enum GenderEnum
        {
            Empty = 0,
            Monsieur,
            Madame
        }

And

enum AddressTypeEnum (Address.cs)

public class Address
{
    private AddressTypeEnum _addressType;
    private string _firstName = string.Empty;
    private string _lastName =string.Empty;
    private string _structure = string.Empty;
    private string _structureComplementary = string.Empty;
    private string _addressStreet = string.Empty;
    private string _addressComplementary = string.Empty;
    private string _bp = string.Empty;
    private string _zipCode = string.Empty;
    private string _country = string.Empty;
    private string _countryId = string.Empty;
    private string _city = string.Empty;
    private string _phone = string.Empty;
    private string _fax = string.Empty;
    private string _email = string.Empty;

    public enum AddressTypeEnum
    {
        Empty = 0,
        Personal = 1,
        Billing = 2,
        Delivery = 3
    }
Candlepin answered 4/1, 2012 at 14:28 Comment(8)
Probably you have to show the definition of the User and Address classes to help you further .Prosper
You spelt address wrong by the way.Snuff
Did you make the property types CLS-compliant?Chantey
Do you really care to know exactly why your code is not CLS-compliant, or do you just want to know how to get rid of these warnings-as-errors ? In other words, are you sure that your code needs to be, has to be, absolutely must be, CLS-compliant?Hallowed
Did you mark the assembly as CLS compliant? msdn.microsoft.com/en-us/library/…Taritariff
Read the guide on Writing CLS-compliant code; much of it explains the error's you're having: msdn.microsoft.com/en-us/library/bhc3fa7f.aspxChantey
Are you by any chance using types in User and Address that are not CLS compilant? msdn.microsoft.com/en-us/library/12abdwbc(v=vs.80).aspxTurbofan
Not sure if nested types are CLS compliant.Hennery
T
9

You need to use the CLSCompliantAttribute:

If no CLSCompliantAttribute is applied to a program element, then by default:

  • The assembly is not CLS-compliant.
  • The type is CLS-compliant only if its enclosing type or assembly is CLS-compliant.
  • The member of a type is CLS-compliant only if the type is CLS-compliant.

Apart from this you need to ensure your assembly is indeed CLS compliant.

Taritariff answered 4/1, 2012 at 14:36 Comment(1)
To follow-up, you will get these kinds of warnings -- 'X' is not CLS Compliant -- if you are using a type from an assembly that is not flagged as CLSCompliant from one that is. Adding the attribute as indicated to the AssemblyInfo.cs file of the 'source' project will typically resolve this.Vocalist
T
1

This is letting you know that "Type of OthersAdresses.AdresseTypeParameter is not CLS-compliant", not that the properties necessarily are. Take a look at the types, not the properties, and you will probably find what is causing the warning.

Thumbprint answered 4/1, 2012 at 15:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.