I need help figuring this out, what am I doing wrong my book doesn’t give a clear explanation on how to deal with this and neither does the online help in VS2008.
Can you show me how to do this correctly?
Another error I am getting:
Cannot Create an instance of the
abstract class or interface
“Person.Person”Member Person.Person.rnd’ cannot be
accessed with an instance reference;
qualify it with a type name instead
Person.cs
public abstract class Person
{
private string title;
private string firstName;
private string lastName;
private string address;
private string gender;
private string dateOfBirth;
private string userID;
static Random rnd = new Random();
// constructors
public Person()
{
}//end default constructor
public Person(string aTitle, string aFirstName, string aLastName,
string aAddress, string aGender, string aDateOfBirth)
{
title = aTitle;
firstName = aFirstName;
lastName = aLastName;
address = aAddress;
gender = aGender;
dateOfBirth = aDateOfBirth;
}
}
Student.cs
public class Student: Person
{
public override string GenerateUserID()
{
this.userID = firstName.Substring(0, 1) + lastName.Substring(0, 5);
//ERROR HAPPENS HERE
this.userID = this.userID + this.rnd.Next(1000, 9999);
}//end method Generate UserID
}
PersonTest.cs
static void Main(string[] args)
{
//ERROR HAPPENS HERE TOO Cannot Create an instance of the abstract class or interface "Person.Person"
Person testPerson = new Person("Mr.", "Merry ", "Lanes",
" 493 Bluebane RD", "Male", " 8-06-1953 ");
}
You can’t create a Person object since it’s abstract.
If you are not a native english speaker or if you just don’t know what abstract means, grab a dictionary and see what this word means.
In the OOP world it means that you can only instancate classes that derive that class.
For further reference google polymorphism.
This means that your code should be:
You should also change your Student constructor to accept those parameters and move them up to the base class like this:
And as others have mentioned, if you want to access a member variable of the base class from a derived class use protected.
protectedmeans this is public for my derived classes, private for anyone else.Also, your rnd variable is static and private.
This means you can’t access it from
thisbecause it belongs to all Person objects and not to specific instances. You can remove this or if you think you need to, remove the static. Make it protected as well.Good luck.