user3108296
user3108296

Reputation:

Pull Properties from Class

I am trying to pull the properties from the PullConstants class to the CreateForecast class. In CreateForecast, I have created an instance of PullConstants with this code. I have also verified that both classes are in the same namespace.

PullConstants pc = new PullConstants();

However, when I try to pull a value from PullConstants to CreateForecast with code below, I always receive 0.

double sla = pc.sla;

I have verified that the value gets pulled from the database correctly, but its scope seems to not reach beyond the first run of the class. What exactly am I doing wrong that I am not able to pull the correct property value from PullConstants?

The PullConstants class is as follows:

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

Upvotes: 1

Views: 70

Answers (2)

Karthik Ganesan
Karthik Ganesan

Reputation: 4242

Either you are misssing a call to this function

 public void PullConstantValues();

or you may consider making this function a constructor by changing it to

 public PullConstant();

Upvotes: 0

Christoph Fink
Christoph Fink

Reputation: 23113

I guess you are missing the call to PullConstantValues:

PullConstants pc = new PullConstants();
pc.PullConstantValues();
double sla = pc.sla;

If supplying these values is the only purpose of this class you may are better of using it as a constructor:

class PullConstants
{
    /* ... */

    public PullConstants() // instead of 'void PullConstantValues()'
    {
        /* ... */
    }
}

If the are "real constants" may also use a singleton to not query the DB every time.

Upvotes: 2

Related Questions