我在我的代码上实现了getter和setter但是我的getter和setter遇到了问题,在setter中使用代码进行验证时它总是返回null,这是我的代码:
private string _employeeId;
public string EmployeeId
{
get
{
return this._employeeId
}
set
{
if (!String.IsNullOrEmpty(this._employeeId))
{
this._employeeId = value;
}
else
{
throw new Exception("Employee ID is required");
}
}
}
在我的应用程序中,我分配了_employeeId的值 创建类的对象
Employees obj = new Employees();
obj.EmployeeId = txt_empId.Text;
答案 0 :(得分:4)
setter正在尝试设置局部变量,但永远不会设置它,因为void RotateCamera(double Angle, double x, double y, double z)
{
quaternion temp, quat_view, result;
temp.x = x * sin(Angle/2);
temp.y = y * sin(Angle/2);
temp.z = z * sin(Angle/2);
temp.w = cos(Angle/2);
quat_view.x = View.x;
quat_view.y = View.y;
quat_view.z = View.z;
quat_view.w = 0;
result = mult(mult(temp, quat_view), conjugate(temp));
View.x = result.x;
View.y = result.y;
View.z = result.z;
}
返回IsNullOrEmpty(this._employeeId)
,阻止它被设置。也许您打算在true
??
IsNullOrEmpty
答案 1 :(得分:-2)
在您的代码中,变量_employeeId
为空,因为您没有为其设置初始值。在set方法中,你是验证_employeeId
变量,所以这个结果总是为null,然后抛出异常!我想你想验证一个值是什么设置方法输入值。因此,您必须验证变量value
,而不是变量_employeeId
!
private string _employeeId;
public string EmployeeId
{
get
{
return this._employeeId
}
set
{
if (!String.IsNullOrEmpty(value))
{
this._employeeId = value;
}
else
{
throw new Exception("Employee ID is required");
}
}
}