想要在运行时创建类的多个对象

时间:2015-01-23 06:55:45

标签: c# asp.net

我是一个完全新鲜的人,我被告知为候选人创建一个班级。 在为模块创建数据库之后。我创造的是

public class Class1
{
   public class JobHistory
    {
     public string CompName, Tech, Profile;
     public string StartDate;
     public string EndtDate;
     public int CurrentAnnualSalary;
     public void AddNew(string CompName, string Tech, string Profile, 
                           string StartDate, string EndtDate, 
                           int CurrentAnnualSalary)
        {
            this.CompName = CompName;
            this.Tech = Tech;
            this.Profile = Profile;
            this.StartDate = StartDate;
            this.EndtDate = EndtDate;
            this.CurrentAnnualSalary = CurrentAnnualSalary;

        }
    }
    public class PersonalInformation
    {
        public string FirstName, LastName, DateOfBirth, PhoneNo1, PhoneNo2;
        public string EmailId;
        public int TotalExperienceInMonths;

        public void AddNew(string FirstName, string LastName, string DateOfBirth,
                           string PhoneNo1, string PhoneNo2, 
                           int TotalExperienceInMonths, string EmailId)
        {
            this.FirstName = FirstName;
            this.LastName = LastName;
            this.DateOfBirth = DateOfBirth;
            this.PhoneNo1 = PhoneNo1;
            this.PhoneNo2 = PhoneNo2;
            this.EmailId = EmailId;
            this.TotalExperienceInMonths = TotalExperienceInMonths;

        }
    }

    public class Skills
    {
         int SkillId;
         string Skill;
    }
    public class Candidate
    {
        int CandidateId;
        JobHistory jh1 = new JobHistory();
        PersonalInformation pi = new PersonalInformation();

        public void AddNew(int CandidateId,JobHistory jh2,PersonalInformation pi2)
        {
            this.CandidateId = CandidateId;
            this.jh1.AddNew(jh2.CompName, jh2.Tech, jh2.Profile, 
                            jh2.StartDate, jh2.EndtDate, 
                            jh2.CurrentAnnualSalary);

            this.pi.AddNew(pi2.FirstName, pi2.LastName, pi2.DateOfBirth,
                           pi2.PhoneNo1, pi2.PhoneNo2, 
                           pi2.TotalExperienceInMonths, pi2.EmailId);
        }
        public void UpdateExisting();
        public void Delete();

    }
}

这里我想要另一类技能。 但从候鸟角度来看,候选人将有多种技能,他会更新。所以为此,我希望在运行时创建类技能的对象,那么我应该使用List<>吗?如何继续?我到现在为止是否正确?

2 个答案:

答案 0 :(得分:0)

首先,您可以使用构造函数替换AddNew-Methods。

对于您的候选人课程,它可能如下所示:     public Candidate(int CandidateId,JobHistory jh2,PersonalInformation pi2)

使用List<Skill>是一个好主意。然后,您可以使用Add - 方法添加技能。

答案 1 :(得分:0)

正如@Jon双向飞碟所提到的,你需要使用Constructor,你需要删除很少需要的class1嵌套类。

首先为您的类编写构造函数,而不是像下面的AddNew方法

public class JobHistory
{
 public string CompName, Tech, Profile;
 public string StartDate;
 public string EndtDate;
 public int CurrentAnnualSalary;
 public JobHistory(string CompName, string Tech, string Profile, 
                       string StartDate, string EndtDate, 
                       int CurrentAnnualSalary)
    {
        this.CompName = CompName;
        this.Tech = Tech;
        this.Profile = Profile;
        this.StartDate = StartDate;
        this.EndtDate = EndtDate;
        this.CurrentAnnualSalary = CurrentAnnualSalary;

    }

 //If required then you can also need write out the empty constructor 
    public JobHistory(){}     

}

您还可以将class1更改为命名空间,以便您的课程可以保持同一namespaceMore info on namespace

 namespace StudeProfileProject

还有一件事,如果你宣布这些属性是公开的,那么在声明时使用getterssetters,这样你的最后一堂课就会像,

public class JobHistory
{
 public string CompName{get;set}
 public string Tech {get;set;}
 public string Profile{get;set;}

 //More properties ....
 public JobHistory()
 {}


 public JobHistory(string CompName, string Tech, string Profile, 
                       string StartDate, string EndtDate, 
                       int CurrentAnnualSalary)
    {
        this.CompName = CompName;
        this.Tech = Tech;
        this.Profile = Profile;
        this.StartDate = StartDate;
        this.EndtDate = EndtDate;
        this.CurrentAnnualSalary = CurrentAnnualSalary;

    }

}

如果您希望每个Candidate有多种技能,那么List将是不错的选择,您可以在候选类中声明它

  public class Candidate
  {
    int CandidateId;
    JobHistory jh1 = new JobHistory();
    PersonalInformation pi = new PersonalInformation();

    List<Skill> CandidateSkills = new List<Skills>();        

    public Candidate(int CandidateId,JobHistory jh2,PersonalInformation pi2)
    {
        this.CandidateId = CandidateId;
        this.jh1= new JobHistory (jh2.CompName, jh2.Tech, jh2.Profile, 
                        jh2.StartDate, jh2.EndtDate, 
                        jh2.CurrentAnnualSalary);

        this.pi =  new PersonalInformation (pi2.FirstName, pi2.LastName, pi2.DateOfBirth,
                       pi2.PhoneNo1, pi2.PhoneNo2, 
                       pi2.TotalExperienceInMonths, pi2.EmailId);
    }
    public void UpdateExisting();
    public void Delete();

}

您将能够根据需要使用所有Collection方法。 Here is more info on it.