繁体   English   中英

C#属性以及如何从另一个函数/类访问它们的值?

[英]C# properties and how to access their values from another function/class?

这是我最先使用属性的尝试,遇到了障碍,并且花了很多时间来查找示例。 可能是因为我这样做完全错误。

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

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

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

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

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

你在做什么在此代码创建的新对象ClientEmployeePatient的对象,而你则分配Client.AppointmentDateappt 这将把该特定属性设置为DateTime.MinValue ,因为您之前没有为AppointmentDate分配值; 从我的头顶开始,它将假定其默认值为DateTime.MinValue

您真正想要做的是这样的:

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

这样,您实际上将创建Client类的对象,然后可以使用该特定对象的AppointmentDate的setter。 您可以为其分配任何您喜欢的DateTime值,并在以后重新使用。 如果要从这些属性中获取值(调用getter ),则只需使用_client.AppointmentDate并对其进行任何处理。

在这里您应该考虑的是对象声明的范围及其寿命。 例如,当您执行以下操作:

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

另一方面,如果您在外部范围内分配Client对象,则数据将被保留以备后用,如下所示:

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

您可能想了解MSDN上.NET中变量和方法作用域的工作原理。

附带说明一下,我个人不会在此特定实现中使用嵌套类,仅是因为封闭类型除了包含其他类型外没有任何其他用途,这几乎从来不是一个好习惯。 关于您的问题的评论之一中有一篇SO文章,我相信您是否想继续阅读。

除了有关正确设置课程的注释。

因此,您确实想要执行此操作或类似操作:

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

希望这能使您朝正确的方向前进。

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 .

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM