2012-01-10 22 views
0

私のコーディングに関して本当に助けが必要です。私のコーディングでは、関数間で値を渡すために静的変数を使用する必要があります。実際に行う方法はありません。誰かが私にこのことを援助できるかどうか本当に感謝します。ありがとう。静的変数を使用してパラメータを渡す方法は?

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

グローバル変数を使用するときにパラメータを渡す必要はありません。 –

+0

ToString()またはreaderGetString( "VALUE")リーダー["StartDate"]を試してください。デフォルトでは、データリーダーはObjectを返します。 – Lloyd

+0

@srahifahあなたは[同時](http://www.ldoceonline.com/popup/popupmode.html?search_str=concurrent)の実行を検討しましたか? – ANeves

答えて

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; 
} 
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(...); 
    } 
} 
関連する問題