错误不一致的可访问性方法C#

本文关键字:方法 访问 不一致 错误 | 更新日期: 2023-09-27 18:27:35

获取消息:

"错误1可访问性不一致:参数类型'Assignment_5.Address'的可访问性不如方法'Assignent_5.ContactManager.AddContact(string,string,Assignment_5.Access)'C:''Users''OscarIsacson''documents''visual studio 2012''Projects''Assignment 5''Assignment 5''ContactFiles''ContactManager.cs 24 21 Assignment 5"

这是方法代码(所有类都是公共的):

    private List<Contact> m_contactRegistry;
    public bool AddContact(string firstName, string lastName, Address adressIn) 
    {
        Contact contactIn = new Contact(firstName, lastName, adressIn);
        m_contactRegistry.Add(contactIn);
        return true;
    }
    public bool AddContact(Contact ContactIn)
    {
        m_contactRegistry.Add(ContactIn);
        return true;
    }

地址类别:

namespace Assignment_5
{
    public class Address
    {
        private string m_street;
        private string m_zipCode;
        private string m_city;
        private Countries m_country;
        public Address() : this (string.Empty, string.Empty, "Göteborg")
        {
        }
        public Address(string street, string zip, string city)
            : this(street, zip, city, Countries.Sweden)
        { 
        }
        public Address(string street, string zip, string city, Countries country)
        {
        }
        public string Street;
        public string City;
        public string ZipCode;
        public Countries Country;
        /// <summary>
        /// This function simply deletes the "_" from country names as saves in the enum.
        /// </summary>
        /// <returns>the country name whitout the underscore char.</returns>
        public string GetCountryString()
        {
            string strCountry = m_country.ToString();
            strCountry = strCountry.Replace("_", " ");
            return strCountry;
        }
        /// <summary>
        /// Method that overrides the ToString method
        /// </summary>
        /// <returns>Formatted string with address detail on one line</returns>
        public override string ToString()
        {
            return string.Format("{0, -25} {1,-8} {2, -10} {3}", m_street, m_zipCode, m_city, GetCountryString());
        }
    }
}

错误不一致的可访问性方法C#

Address类是protectedprivateinternal,以便您获得此错误。请注意,在C#中,类默认为internal

我认为Address可能不是public class。你应该让它成为

Address类不是public

您收到此错误是因为您正在引用public方法上的非公共类。此代码的调用者可以访问AddContact方法,但不能访问Address类。这意味着任何人都不可能呼叫AddContact