-1

Learning C# professionally now, and this is a homework program. I can't figure out why one of the three class libraries works without issue, but I can't call any of the other three at all when they where constructed identically to each other.

There are no errors at all in any of the 4 class libraries. All of the Constructors are Public. I have checked the fields, properties, and CTORS multiple times, and none seem to have any issues they are all identical to the student.cs file in every way that I can find, but I can't call them from my console app like I could with student.cs.

The errors I get seem to indicate that the constructors are not public, but they are. I have added the class libraries as a reference to the console all, and included the using statement. Is there anything more that I need to use these other class libraries class files?

Error Messages received from code

C# Console App Class

Working C# Class Library

First C# Class Library not working.

Second C# Class Library not working.

Third C# Class Library not working.

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);
        }
    }
}

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

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

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
    }
}

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
    }
}
shaun
  • 1,223
  • 1
  • 19
  • 44
  • 4
    This question includes far too much code, and please do NOT post screenshots of your code! You need to narrow your problem down to the minimal code necessary to demonstrate your problem. – Blorgbeard Apr 08 '15 at 02:21
  • It's only a duplicate if you know the answer... – Ray Apr 08 '15 at 05:04

3 Answers3

7

Classes are internal by default. So if Vehicle is in a separate assembly you won't be able to access it. Declare the class as public.

public class Vehicle
Ray
  • 45,695
  • 27
  • 126
  • 169
2

The constructors are public but the classes in your code are not (classes are internal unless specified otherwise), which means the constructors are only public for calls made from within the same assembly. Make the classes public and their public members will then be accessible to external assemblies.

class Vehicle

is the same as:

internal class Vehicle
Nathan Ridley
  • 33,766
  • 35
  • 123
  • 197
0

The constructors are public, but they forgot to make the class as a whole public.

public class Vehicle
public class Login
public class ContactInfo
Ben
  • 2,433
  • 5
  • 39
  • 69
shaun
  • 1,223
  • 1
  • 19
  • 44