我无法弄清楚如何摆脱这个错误来挽救我的生命。我的任务的主要目标是,一旦用户单击“提交”按钮,它便应从下拉菜单中过滤出用户“雇员类别”,并吐出X结果。我遇到的问题是一条错误消息,指出“无法将类型'csis265.domain(classname)隐式转换为csis265.employee”
主类库
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using log4net;
namespace csis265.domain
{
public abstract class employee
{
private static readonly ILog logger =
log4net.LogManager.GetLogger(System.Reflection.MethodBase.getcurrentMethod().DeclaringType);
protected string firstName;
protected string lastName;
protected double payRate;
protected double hoursWorked;
public employee() : this("","",0.0,0.0)
{
}
public employee(string firstName,string lastName,double payRate,double hoursWorked)
{
setfirstName(firstName);
setlastName(lastName);
setpayRate(payRate);
sethoursWorked(hoursWorked);
}
public void setfirstName(string firstName)
{
this.firstName = firstName;
}
public void setlastName(string lastName)
{
this.lastName = lastName;
}
public void setpayRate(double payRate)
{
this.payRate = payRate;
}
public void sethoursWorked(double hoursWorked)
{
this.hoursWorked = hoursWorked;
}
//GETTERS
public string getfirstName()
{
return firstName;
}
public string getlastName()
{
return lastName;
}
public double getpayRate()
{
return payRate;
}
public double gethoursWorked()
{
return hoursWorked;
}
public abstract double GetWeeklyPay();
public override string ToString()
{
string output = string.Format("EMP: FN: {0} LN: {1} PR: {2} HW: {3}",firstName,lastName,payRate,hoursWorked);
return output;
}
public virtual void Display()
{
System.Console.WriteLine(ToString());
}
}
}
Employee.ASPX
using System;
using System.Collections.Generic;
using System.Linq;
using system.web;
using system.web.UI;
using system.web.UI.WebControls;
using log4net;
using csis265.domain;
namespace csis265proj05
{
public partial class employee : system.web.UI.Page
{
private static readonly ILog logger =
log4net.LogManager.GetLogger(System.Reflection.MethodBase.getcurrentMethod().DeclaringType);
protected void Page_Load(object sender,EventArgs e)
{
log4net.Config.XmlConfigurator.Configure();
if (Page.IsPostBack)
{
logger.Debug("Web page is being posted back");
}
else
{
logger.Debug("first time entering");
}
}
protected void btnSubmit_Click(object sender,EventArgs e)
{
logger.Debug("user clicked submit");
string firstName = "";
string lastName = "";
double payRate = 0.0;
double hoursWorked = 0.0;
double bonus = 0.0;
string type = string.Empty;
employee emp = null;
type = DropEmpl.SelectedValue;
try
{
lblMessage.Text = string.Empty;
firstName = Convert.ToString(txtFirst.Text);
lastName = Convert.ToString(txtLast.Text);
payRate = Convert.ToDouble(txtPay.Text);
hoursWorked = Convert.ToDouble(TxtHours.Text);
if (type.ToLower().Equals("wage employee"))
{
emp = new wageemployee(firstName,hoursWorked);
}
if (type.ToLower().Equals("salary employee"))
{
emp = new salaryemployee(firstName,hoursWorked);
}
if (type.ToLower().Equals("exectuive employee"))
{
bonus = Convert.ToDouble(TxtBonus.Text);
emp = new executiveemployee(firstName,hoursWorked,bonus);
}
lblMessage.Text = emp.ToString();
logger.Debug(emp.ToString());
}
catch (Exception ex)
{
logger.Error(ex);
lblMessage.Text = "Please enter valid data";
}
}
}
}
实现的类
sal emp
namespace csis265.domain
{
public class salaryemployee : employee
{
private static readonly ILog logger =
log4net.LogManager.GetLogger(System.Reflection.MethodBase.getcurrentMethod().DeclaringType);
public salaryemployee(): this("",0.0)
{
}
public salaryemployee(string firstName,double hoursWorked)
:base(firstName,hoursWorked)
{
this.firstName = firstName;
this.lastName = lastName;
this.payRate = payRate;
this.hoursWorked = hoursWorked;
}
public override double GetWeeklyPay()
{
return payRate/52;
}
public override string ToString()
{
string output = string.Format("FN: {0} LN: {1} PR: {2} HW: {3} WKPay: {4}",GetWeeklyPay());
return output;
}
exec
namespace csis265.domain
{
public class executiveemployee : employee
{
private static readonly ILog logger =
log4net.LogManager.GetLogger(System.Reflection.MethodBase.getcurrentMethod().DeclaringType);
protected double bonus;
public executiveemployee() : this("",0.0)
{
}
public executiveemployee(string firstName,double hoursWorked,double bonus)
: base(firstName,hoursWorked)
{
setBonus(bonus);
}
public void setBonus(double bonus)
{
this.bonus = bonus;
}
public double getBonus()
{
return bonus;
}
public override double GetWeeklyPay()
{
return payRate * hoursWorked;
}
public override string ToString()
{
string output = string.Format("FN: {0} LN: {1} PR: {2} HW: {3} BN: {4} WKPay: {5}",bonus,GetWeeklyPay());
return output;
}
工资
namespace csis265.domain
{
public class wageemployee : employee
{
private static readonly ILog logger =
log4net.LogManager.GetLogger(System.Reflection.MethodBase.getcurrentMethod().DeclaringType);
protected double bonus;
public wageemployee() : this("",0.0)
{
}
public wageemployee(string firstName,double hoursWorked)
: base(firstName,hoursWorked)
{
this.firstName = firstName;
this.lastName = lastName;
this.payRate = payRate;
this.hoursWorked = hoursWorked;
}
public override double GetWeeklyPay()
{
return (payRate + bonus) * hoursWorked;
}
public override string ToString()
{
string output = string.Format("FN: {0} LN: {1} PR: {2} HW: {3} WKPay: {4}",GetWeeklyPay());
return output;
}
public new void Display()
{
System.Console.WriteLine(ToString());
}
}
}