从类中提取属性
本文关键字:属性 提取 | 更新日期: 2023-09-27 18:08:25
我试图将属性从PullConstants类拉到CreateForecast类。在CreateForecast中,我用下面的代码创建了一个PullConstants实例。我还验证了两个类在同一个命名空间中。
PullConstants pc = new PullConstants();
但是,当我尝试用下面的代码从PullConstants拉一个值到CreateForecast时,我总是收到0。
double sla = pc.sla;
我已经验证了从数据库中正确提取的值,但是它的作用域似乎没有超出类的第一次运行。究竟是什么我做错了,我不能从PullConstants拉正确的属性值?
PullConstants类如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
namespace ForecastBuilder
{
class PullConstants
{
InitializeDB idb = new InitializeDB();
ErrorLogger el = new ErrorLogger();
public double sla { get; set; }
public int serviceTime { get; set; }
public int avgHandleTime { get; set; }
public int maxWait { get; set; }
public double shrinkageAdjustment { get; set; }
public double alpha { get; set; }
public double beta { get; set; }
public double specialDayPerInc { get; set; }
public void PullConstantValues()
{
idb.OpenDatabases();
try
{
string sqlConstants = "select * from forecastconstants";
MySqlCommand cmdConstants = new MySqlCommand(sqlConstants, idb.myconn);
MySqlDataReader rdrConstants = cmdConstants.ExecuteReader();
while (rdrConstants.Read())
{
sla = double.Parse(rdrConstants["SLA"].ToString());
serviceTime = int.Parse(rdrConstants["ServiceTime"].ToString());
avgHandleTime = int.Parse(rdrConstants["AvgHandleTime"].ToString());
maxWait = int.Parse(rdrConstants["MaxWait"].ToString());
shrinkageAdjustment = double.Parse(rdrConstants["ShrinkageAdjustment"].ToString());
alpha = double.Parse(rdrConstants["Alpha"].ToString());
beta = double.Parse(rdrConstants["Beta"].ToString());
specialDayPerInc = double.Parse(rdrConstants["SitCallIncrPer"].ToString());
}
}
catch (Exception e)
{
el.createError(2, e.ToString(), "Could not pull constants");
}
finally
{
idb.myconn.Close();
}
}
}
}
我想你错过了呼叫PullConstantValues
:
PullConstants pc = new PullConstants();
pc.PullConstantValues();
double sla = pc.sla;
如果提供这些值是该类的唯一目的,则最好将其用作构造函数:
class PullConstants
{
/* ... */
public PullConstants() // instead of 'void PullConstantValues()'
{
/* ... */
}
}
您错过了对该函数的调用
public void PullConstantValues();
或者你可以考虑把这个函数改成
构造函数 public PullConstant();