如何使用静态变量传递参数?

时间:2012-01-10 11:18:33

标签: c# parameters

我真的需要一些关于编码的帮助。在我的编码中,我需要使用静态变量在函数之间传递值,而我真的不知道如何去做。真的很感激,如果有人可以帮助我这件事。感谢。

static String TypeOfReport;
static DateTime DateOfExecution;

static DateTime StartDate;
static DateTime EndDate;
static int SpokeCode;

static void Main(string[] args)
{
    DateTime start = System.DateTime.Now.AddMinutes(1);
    Schedule.PeriodicSchedules schedule = new Schedule.PeriodicSchedules(start, Schedule.PeriodicSchedules.Frequency.Minutely);
    schedule.Elapsed += new System.Timers.ElapsedEventHandler(GenerateReport);
    schedule.Enabled = true;

    Console.ReadLine();
}

static void GenerateReport(object sender, EventArgs e)
{
    if (TypeOfReport == "BillingReport")
    {
        Schedule.PeriodicSchedules s = new Schedule.PeriodicSchedules(DateOfExecution, Schedule.PeriodicSchedules.Frequency.Minutely);
        s.Elapsed += new System.Timers.ElapsedEventHandler(hell);

        crRpt.Load("C:\\rptBilling.rpt");
        ReportLogin(crRpt);

        crRpt.SetParameterValue("@CollectionStartDate", StartDate);
        crRpt.SetParameterValue("@CollectionEndDate", EndDate);
        crRpt.SetParameterValue("@SpokeCode", SpokeCode);
    }                
}            

static void ReportAccess()
{
    SqlConnection thisConnection = new SqlConnection("data source=s3rosteam;initial catalog=ReportDB; integrated security=True; Pooling=False;");
    SqlCommand thisCommand = null;

    try
    {
        String strSQL = "SELECT TypeO fReport,DateOfExecution,StartDate,EndDate,SpokeCode FROM dbo.Schedule WHERE TypeOfReport ='" + TypeOfReport + "', DateOfExecution = '" + DateOfExecution + "'";
        thisConnection.Open();
        thisCommand = new SqlCommand(strSQL);
        thisCommand.Connection = thisConnection;
        thisCommand.CommandType = CommandType.Text;

        using (SqlDataReader reader = thisCommand.ExecuteReader())
        {
            reader.Read();
            StartDate = Convert.ToDateTime(reader["StartDate"]);
            EndDate = Convert.ToDateTime(reader["EndDate"]);
            SpokeCode = Convert.ToInt16(reader["SpokeCode"]);  
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
    }

    finally
    {
        thisCommand.Dispose();
        thisConnection.Close();
        thisCommand.Dispose();
    }
}

2 个答案:

答案 0 :(得分:0)

您不需要将每个函数声明为静态。相反,你可以做一些事情,如:

internal class MyReportGenerator
{
    // Private member variables
    private int someVar;

    // Public properties
    public string TypeOfReport { get; set; }

    // Constructor
    public MyReportGenerator(string s, int i)
    {
        // Here you can initialize your member variables
    }

    // Methods
    void GenerateReport(string otherParams)
    {
    }

    public static void Main(string[] args)
    {
        // Setup the report generator, scheduler, etc
        MyReportGenerator generator = new MyReportGenerator(...);
        generator.GenerateReport(...);
    }
}

答案 1 :(得分:0)

感谢您的建议。我设法传递了价值。但我没有使用静态变量。

static void GenerateReport()
{  
    string[] arrTypes = ReadReport().Split('~');

    foreach (string strReportType in arrTypes)
    {
        if (strReportType == "BillingReport")
        {
            while (ThisReader.Read())
            {
                crRpt.SetParameterValue("@CollectionStartDate", ThisReader["StartDate"]);
                crRpt.SetParameterValue("@CollectionEndDate", ThisReader["EndDate"]);
                crRpt.SetParameterValue("@SpokeCode", ThisReader["SpokeCode"]);
            }
        }           
    }
}

static string ReadReport()
{
    try
    {
        ………     
        using (SqlDataReader reader = thisCommand.ExecuteReader())
        {
            while (reader.Read())
            {
                strReturn += reader["TypeOfReport"].ToString() + "~";
            }
            reader.Close();
        }
        strReturn = strReturn.Remove(strReturn.Length - 1);                

    }
    catch (Exception ex)
    {  }

    finally
    {  }
    return strReturn;
}