2

What I am trying to do is get the UserID to generate random values based on the type of class it is the userID should be generated from person.cs. For example the student class generates a student id and a random number the staff class is supposed to generate a staff id and a random number, the issue I am having is getting any userID sample data to show up at all in the console output, I don't get any data to show up for the userID can anyone help me.

Here's the links requested to view the code

Person.cs

PersonTest.cs

Student.cs

Staff.cs

Faculty.cs

Person.cs

public class Person
{
    static string title;
    protected string firstName;
    protected string lastName;
    protected string address;
    protected string gender;
    protected string dateOfBirth;
    protected string userID;
    protected 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;
        Console.WriteLine( this.DisplayInfo() );

        //create userID


        Console.WriteLine(userID);

        this.DisplayInfo();
    }//end 6-parameter constructor

Just Added

public string DisplayInfo()
    {
        return " You have created the person " + firstName + lastName +"\n whose address is" + address + "\n" + " whos is a " + gender + " and was born on " + dateOfBirth;
    }//end method DisplayInfo

Staff.cs student.cs has the same data for now

  public class Staff : Person
    {
        public Staff(string aTitle, string aFirstName, string aLastName, string aAddress,
           string aGender, string aDateOfBirth)
            : base(aTitle, aFirstName, aLastName, aAddress,
                    aGender, aDateOfBirth)
        {

           this.userID = firstName.Substring(0, 1) + lastName.Substring(0, 5);

           this.userID = this.userID + this.rnd.Next(1000, 9999);


        }

PersonTest.cs

  public class PersonTest
    {
       static void Main(string[] args) 
      { 
            Person testPerson = new Student("Mr.", "Merry ", "Lanes", " 493 Bluebane RD", "Male", " 8-06-1953 ");


            Person studentPerson = new Person("Mr.", "Jerry ", "Panes", " 493 Bluebane RD", "Male", " 8-06-1953 "); 

            // THIS DATA SHOWS UP BUT NOT THE USERID



            Console.ReadLine();
        }//end main

UML Diagram

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Michael Quiles
  • 1,131
  • 3
  • 24
  • 41

3 Answers3

2

Your UserID is null.

The base class tries to print the user ID BEFORE the Staff class can give a value to UserID

Your problem is located in the person class where you wrote "Create userID"

If your unfamiliar with how the process goes for inheritance here is the key step that you missed out:

1: If you call the parent class constructor from it's child class, ALL of the parent class is ran before the child.

As you can see the userID will be null, if you replace "create userID" with userID = "1000"; you should have something being displayed.

From my view you just need to change your algorithm, a simple change hopefully.

ChickSentMeHighE
  • 1,706
  • 6
  • 21
  • 30
1

OK, this turns out to be a question about the sequence of constructors.

The base-class constructor(s) are always called first. You can somehow see that in the syntax of a derived class, the call to the base ctor happens between the parameter list and the derived body.

we have:

public class Person(....)
{
    protected string userID;

    public Person(....)
    {
      //create userID   //no, print it
      Console.WriteLine(userID);
    }
}

public class Staff : Person
{
    public Staff(...)
      : base(....)         // now it is printed
    {
       this.userID = ...;  // and now it is filled
    } 
}

Btw, using a random number for ID is questionable. Use a (static ounter) to generate 0,1,2,3 and you have zero chance of a collision. Regardless of the prefix.

H H
  • 263,252
  • 30
  • 330
  • 514
0

In my opinion, your problem is that the constructor from Person class is called first, and after that the constructor from Student (derived class) is called. See another post about base keyword.

So when you are calling "Console.WriteLine(userID);" in Person constructor, no userID has been assigned yet.

Try to add "Console.WriteLine(userID);" to the constructor in Student class. This should work for the "new Student" call.

From what you have shown there is no assigment of userID in Person class (unless there is someting after "//create userID" what you haven't shown us), so calling "new Person" will not return any userID, because it is a null string.

Btw. the last statement in Person constructor "this.DisplayInfo();" has no reason, because you are not writing it anywhere (you would have to use Console.WriteLine in DisplayInfo, or use "Console.WriteLine( this.DisplayInfo() );" as on the above line.

Post a link with complete code, so that we can try it out.

Community
  • 1
  • 1
devmake
  • 5,222
  • 2
  • 28
  • 26