由于其保护级别,C#代码无法访问,但所有CTORS都是公共的

sha*_*aun -1 c#

现在专业学习C#,这是一个家庭作业计划.我无法弄清楚为什么三个类库中的一个没有问题地工作,但是当它们彼此构造相同时,我根本不能调用其他三个库中的任何一个.

4个类库中的任何一个都没有错误.所有的建设者都是公共的.我已经多次检查了字段,属性和CTORS,似乎没有任何问题它们都是我可以找到的各种方式都与student.cs文件完全相同,但我不能从我的控制台应用程序中调用它们我可以和student.cs一起.

我得到的错误似乎表明构造函数不是公开的,但它们是.我已经添加了类库作为对控制台all的引用,并包含了using语句.还有什么我需要使用这些其他类库类文件?

从代码收到的错误消息

C#控制台应用程序类

使用C#类库

第一个C#类库无法正常工作.

第二个C#类库无法正常工作.

第三个C#类库无法正常工作.

Program.cs中

using System;
using System.Collections.Generic;
using System.Linq; 
using System.Text;
using System.Threading.Tasks;

using ClassesLibrary; //adding our Class Library to this runnable program

namespace TesterProgram
{
    class Program
    {
    static void Main(string[] args)
        {
            Console.WriteLine("***************** Students ******************");
            Student s1 = new Student();
            s1.FirstName = "David";
            s1.LastName = "Castle";
            s1.ID = "987";
            s1.GPA = 3.87f;

            Student s2 = new Student("Michael", "Angelo", "789", 2.8f);
            Console.WriteLine(s1);
            Console.WriteLine(s2);

            Console.WriteLine("******************* Vehicle ******************");
            Vehicle v1 = new Vehicle();
            v1.Make = "Chevy";
            v1.Model = "Malibu";
            v1.Year = 2005;
            v1.Weight = 2050;

            Vehicle v2 = new Vehicle("Ford", "Taurus", 2014, 3000);
            Console.WriteLine(v1);
            Console.WriteLine(v2);

            Console.WriteLine("******************* Login ******************");
            Login l1 = new Login();
            l1.UserName = "Shaun";
            l1.Password = "Forest";

            Login l2 = new Login("Alex", "Swing");

            Console.WriteLine(l1);
            Console.WriteLine(l2);

            Console.WriteLine("******************* Contact Info ******************");
            ContactInfo c1 = new ContactInfo();
            c1.StreetAddress = "112 SW XXXXXXXXXXXXXX Dr.";
            c1.City = "XXXX XXXXXXX";
            c1.State = "YYYYYYYY";
            c1.Zip = "#####";
            c1.Phone = "816-XXX-YYYY";
            c1.Email = "YYYYY.XXXXXXX@gmail.com";

           ContactInfo c2 = new ContactInfo("12418 YYYYY St.", "YYYYYYYYYY YYYY", "YYYYYYY", "#####", "913-XXX-YYYY", "XXXXXXXX@yahoo.com");

           Console.WriteLine(c1);
           Console.WriteLine(c2);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Student.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassesLibrary
{
    public class Student
    {
        #region Fields
        //x TODO fields
        private string _firstName;
        private string _lastName;
        private string _id;
        private float _gpa;
        #endregion

        #region Properties
        //x TODO Properties
        public string FirstName
        {
            get { return _firstName; }
            set { _firstName = value; }
        }//ends FirstName

        public string LastName
        {
            get { return _lastName; }
            set { _lastName = value; }
        }//ends LastName

        public string ID
        {
            get { return _id; }
            set { _id = value; }
        }//ends ID

        public float GPA
        {
            get { return _gpa; }
            set { _gpa = value; }
        }//ends GPA
        #endregion

        #region CTORS
        //x TODO CTORs
        //default ctor
        public Student() { }

        //FQCTOR
        public Student(string firstName, string lastName, string id, float gpa)
        {
            FirstName = firstName;
            LastName = lastName;
            ID = id;
            GPA = gpa;
        }

        #endregion

        #region Methods
        //x TODO Methods
        public override string ToString()
        {
            //return base.ToString();
            return string.Format("Congratulations {0} {1}, Student ID Number {2}, you have a GPA of {3}",
            FirstName,
            LastName,
            ID,
            GPA);
        }

        #endregion
    }//ends class
}//ends namespace
Run Code Online (Sandbox Code Playgroud)

Vehicle.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassesLibrary
{
    class Vehicle
    {
        #region Fields
        //x TODO fields
        private string _make;
        private string _model;
        private int _year;
        private float _weight;
        #endregion

        #region Properties
        //x TODO Properties
        public string Make
        {
            get { return _make; }
            set { _make = value; }
        }

        public string Model
        {
            get { return _model; }
            set { _model = value; }
        }

        public int Year
        {
            get { return _year; }
            set { _year = value; }
        }

        public float Weight
        {
            get { return _weight; }
            set { _weight = value; }
        }

        #endregion

        #region CTORs
        //x TODO CTORs

        //default ctor
        public Vehicle() { }

        //FQCTOR

        public Vehicle(string make, string model, int year, float weight)
        {
            Make = make;
            Model = model;
            Year = year;
            Weight = weight;
        }
        #endregion

        #region Methods
        //x TODO Methods

        public override string ToString()
        {
            //return base.ToString();
            return string.Format("I really like your {0} {1} {2}. Are you sure it weighs {3}lbs?",
                Year,
                Make,
                Model,
                Weight);
        }
        #endregion
    }//ends class
}//ends namespace
Run Code Online (Sandbox Code Playgroud)

Login.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassesLibrary
{
    class Login
    {
        #region Fields
        //x TODO fields
        private string _userName;
        private string _password;
        #endregion

        #region Properties
        //x TODO Properties
        public string UserName
        {
            get { return _userName; }
            set { _userName = value; }
        }//ends UserName

        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }//ends Password
        #endregion

        #region CTORs
        //x TODO CTORs

        //default ctor
        public Login() { }


        //FQCTOR

        public Login(string userName, string password)
        {
            UserName = userName;
            Password = password;
        }
        #endregion

        #region Methods
        //x TODO Methods
        public override string ToString()
        {
            //return base.ToString();
            return string.Format("Congratulations your UserName and Password are {0} {1}.",
                UserName,
                Password);
        }

        #endregion
    }
}
Run Code Online (Sandbox Code Playgroud)

ContactInfo.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassesLibrary
{
    class ContactInfo
    {
        #region Fields
        //x TODO fields
        private string _streetAddress;
        private string _city;
        private string _state;
        private string _zip;
        private string _phone;
        private string _email;
        private string p1;
        private string p2;
        #endregion

        #region Properties
        //x TODO Properties
        public string StreetAddress
        {
            get { return _streetAddress; }
            set { _streetAddress = value; }
        }//ends StreetAddress

        public string City
        {
            get { return _city; }
            set { _city = value; }
        }//ends City

        public string State
        {
            get { return _state; }
            set { _state = value; }
        }//ends State

        public string Zip
        {
            get { return _zip; }
            set { _zip = value; }
        }//ends Zip

        public string Phone
        {
            get { return _phone; }
            set { _phone = value; }
        }//Ends Phone
        public string Email
        {
            get { return _email; }
            set { _email = value; }
        }//Ends Email
        #endregion

        #region CTORs
        //x TODO CTORs

        //default ctor
        public ContactInfo() { }

        //FQCTOR
        public ContactInfo(string streetAddress, string city, string state, string zip, string phone, string email)
        {
            StreetAddress = streetAddress;
            City = city;
            State = state;
            Zip = zip;
            Phone = phone;
            Email = email;
        }

        public ContactInfo(string p1, string p2)
        {
            // TODO: Complete member initialization
            this.p1 = p1;
            this.p2 = p2;
        }
        #endregion

        #region Methods
        //x TODO Methods
        public override string ToString()
        {
            //return base.ToString();
            return string.Format("{0}\n{1}, {2} {3}\n\nPhone: {4}\tEmail: {5}",
                StreetAddress,
                City,
                State,
                Zip,
                Phone,
                Email);
        }
        #endregion
    }
}
Run Code Online (Sandbox Code Playgroud)

Ray*_*Ray 7

默认情况下,类是内部的.因此,如果Vehicle在单独的程序集中,您将无法访问它.将该类声明为public.

public class Vehicle
Run Code Online (Sandbox Code Playgroud)