0

这是我第一次尝试使用属性,但我遇到了障碍,并且花了很多时间寻找示例。可能是因为我这样做完全错误。

我有一个设置所有属性的方法。

public class wsBase : Page
{
    public class Client
    {
        public DateTime AppointmentDate{ get; set; }
        public int TIN { get; set; }
        public string Username { get; set; } 
        public string Password { get; set; }
    }

    public class Employee
    {
        public int SSN { get; set; }
    }

    public class Patient
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime DOB { get; set; }
        public int Gender { get; set; }
    }
}

设置功能:

public void SettingAppointmentData(int employeeID, DateTime ApptDt, DateTime patientDOB, string patientFname, string patientLname, int patientgender)
    {
        wsData wsD = new wsData();
        Client cli = new Client();
        Employee emp = new Employee();
        Patient pat = new Patient();

        pat.DOB = patientDOB;

        patientFname = ValidateName(patientFname);
        patientLname = ValidateName(patientLname);

        pat.FirstName = patientFname;
        pat.LastName = patientLname;
        pat.Gender = patientgender;
     }

我的问题在于尝试访问这些设置参数。下面是尝试访问参数的函数示例。下面不起作用,我知道我所看到的处理它是错误的。如何从这些属性访问属性集?

public bool eligibleAppointment()
{
        wsBase.Client cli = new wsBase.Client();
        wsBase.Employee emp = new wsBase.Employee();
        wsBase.Patient pat = new wsBase.Patient();

        DateTime DOB = pat.DOB;
        DateTime appt = cli.AppointmentDate;
}
4

4 回答 4

3

我不会在这里使用嵌套类,目的通常只是为了限制嵌套类的范围。与普通类相比,嵌套类具有额外的私有修饰符的可能性(当然还有受保护的)。

基本上,如果您只需要在“父”类中使用这个类(就范围而言),那么通常将其定义为嵌套类是合适的。如果这个类可能需要在没有程序集/库的情况下使用,那么用户将其定义为单独的(兄弟)类通常更方便,无论这两个类之间是否存在任何概念关系。尽管在技术上可以创建一个嵌套在公共父类中的公共类,但在我看来,这很少是一个合适的实现方式。

于 2013-07-11T21:26:32.797 回答
1
public bool eligibleAppointment()
{
        wsBase.Client cli = new wsBase.Client();
        wsBase.Employee emp = new wsBase.Employee();
        wsBase.Patient pat = new wsBase.Patient();

        DateTime DOB = pat.DOB;
        DateTime appt = cli.AppointmentDate;
}

What you're doing in this code is creating new objects of your Client, Employee and Patient objects, and you are then assigning Client.AppointmentDate to appt. This will set that specific property to DateTime.MinValue, since you haven't previously assigned a value to AppointmentDate; it'll assume its default value which is DateTime.MinValue from the top of my head.

What you are really trying to do is something like this:

    public class Client
    {
        public DateTime AppointmentDate { get; set; }
        public int TIN { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }
    }

    private Client _client = new Client();
    public bool EligibleAppointment()
    {
        _client.AppointmentDate = DateTime.Today; // Or something. This way you'll assign DateTime.Today to the AppointmentDate of this specific _client object.

        return _client.AppointmentDate > DateTime.MinValue; // Or whatever.
    }

This way you'll actually create an object of your Client class, and you can then use the setter of the AppointmentDate of that specific object. You can assign it any DateTime value you like, and re-use it later on. If you want to obtain the values from these properties (calling the getter), you'd simply use _client.AppointmentDate and do whatever you need to do with them.

What you should take into account here is the scope of your object declarations, and their lifespans. When for example you do the following:

public void Foo()
{
    var client = new Client();

    // As soon as Foo() stops executing, client will be disposed, and client.Username won't be SomeUser anymore.
    // Reason being is that it's declared on a scope local to Foo(), its lifespan is the duration of the method execution.
    client.Username = "SomeUser";
} 

On the other hand, if you assign the Client object in outer scope, the data will be retained for later, like so:

private Client _client = new Client();

public void Foo()
{
    // Since _client is declared in the outer scope now, the data we assign to _client.Username
    // below will still be there even when Foo() finished execution.
    _client.Username = "SomeUser";
} 

public void Bar()
{
    // This'll still give us "SomeUser", since the object is still "alive" since we've called Foo().
    Console.WriteLine("Username: " + _client.Username);
}

You may want to read up on how variable and method scoping works in .NET on MSDN.

On a side note, I'd personally not use nested classes in this specific implementation, simply because the enclosing type doesn't serve any purpose other than containing other types, which is hardly ever good practice. There's an SO article on that in one of the comments to your question I believe if you want to read up on it more.

于 2013-07-11T21:24:11.300 回答
0

除了关于正确设置课程的评论。

所以你真的想这样做或类似的事情:

Client cli = new Client();
Employee emp = new Employee();
Patient pat = new Patient();

public bool eligibleAppointment()
{
        cli.Client = "something";
        emp.Employee = "something";
        pat.Patient = "something";

        DateTime DOB = pat.DOB;
        DateTime appt = cli.AppointmentDate;
}

啤酒花这让你朝着正确的方向前进。

于 2013-07-11T21:14:26.143 回答
-2
public class wsBase : Page
{
public Client client {get;set;}
public Employee employee {get ; set;}
public Patient patient{get;set;}
// your existing code here 
}
public bool eligibleAppointment()
{

// Write your logic here .

}
于 2013-07-11T21:18:33.433 回答