【问题标题】:C# - duplicated output for start date in date range iterationC# - 日期范围迭代中开始日期的重复输出
【发布时间】:2016-05-09 19:21:36
【问题描述】:

我正在尝试制作简单的应用程序并卡住了。用户从日期选择器 1 28.01.2015 和结束日期选择器 2 29.01.2015 中选择开始日期,然后通过复选框标记他/她想要的内容并按下按钮。控制台中的输出应与此类似,具体取决于标记的内容。

tar zxcvf /.../c/folder/folder/$HOSTNAME.20150128.log;zxcvf /.../c/folder/folder/$HOSTNAME.20150129.log`

但是作为开始日期选择的日期在控制台中返回了两次,我不知道出了什么问题:

zxcvf /.../c/folder/folder/$HOSTNAME.20150128.log;zxcvf /.../c/folder/folder/$HOSTNAME.20150128.log;zxcvf /.../c/folder/folder/$HOSTNAME.20150129.log 

代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Renci.SshNet;
using System.Configuration;

namespace TestApp
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
            GlobalMethods configRead = new GlobalMethods();
            configRead.ConfigRead();
        }

...

        private void button1_Click(object sender, EventArgs e)
        {
            GlobalMethods configRead = new GlobalMethods();
            configRead.ConfigRead();
            GlobalMethods dateIterator = new GlobalMethods();
            dateIterator.Iterator();
            GlobalVariables.comminit = null;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                GlobalVariables.cLM = true;
            }
            else if (!checkBox1.Checked)
            {
                GlobalVariables.cLM = false;
            }
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            GlobalVariables.dateStart = dateTimePicker1.Value;

        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            GlobalVariables.dateEnd = dateTimePicker2.Value;
            //string endDate = dateTimePicker2.Value.ToString("yyyyMMdd"); 
        }

    }
    public static class GlobalVariables
    {
        // Log variables below
        public static Boolean mLM;
        public static Boolean cLM;
        public static Boolean hLM;
        public static Boolean pLM;
        public static Boolean prLM;
        public static Boolean compressMarked = true;
        public static String iterate;
        public static String comminit;
        public static String pH;
        public static String pP;
        public static String pC;
        public static String ppP;
        public static String pM;
        // Time variables below
        public static DateTime dateStart = DateTime.Now;
        public static DateTime dateEnd = DateTime.Now; 
    }

    public class Instructions
    {

        public static String hLl()
        {
            if (GlobalVariables.compressMarked)
            {
                return "tar zxcvf " + GlobalVariables.pH + "$HOSTNAME." + GlobalVariables.iterate + ".log;";
            }
            else { return "wget " + GlobalVariables.pH + "$HOSTNAME." + GlobalVariables.iterate + ".log;"; }
        }

...

        public static String commandsBath()
        {

            if (GlobalVariables.mLM == true)
            {
                GlobalVariables.comminit += mLl();


            }

            if (GlobalVariables.cLM == true)
            {
                GlobalVariables.comminit += cLl();

            }


            if (GlobalVariables.hLM == true)
            {
                GlobalVariables.comminit += hLl();

            }


            if (GlobalVariables.pLM == true)
            {
                GlobalVariables.comminit += pLl();

            }


            if (GlobalVariables.prLM == true)
            {
                GlobalVariables.comminit += pLlp();

            }

            return GlobalVariables.comminit;

        }
    }
    public class GlobalMethods
    {

       public void Iterator()
        {
            for (DateTime i = GlobalVariables.dateStart; i <= GlobalVariables.dateEnd; i = i.AddDays(1))
{
                string iterated = i.ToString("yyyyMMdd");
                GlobalVariables.iterate = iterated;
            }
        }
        public void ConfigRead()
        {
...
        }
        public void ConfigWrite()
        {
...
        }

    } 

}

希望有人可以提供帮助。

【问题讨论】:

    标签: date if-statement for-loop datepicker date-range


    【解决方案1】:

    没关系,找到解决方案了。与其使用 += 并添加 if 语句的结果,不如使用下面的字符串生成器。这样它就可以正常工作并且更简单。

            var sb = new StringBuilder();
    
            if (GlobalVariables.mLM == true) sb.Append(mLl());
            if (GlobalVariables.cLM == true) sb.Append(cLl());
            ...
    
            return GlobalVariables.comminit = sb.ToString();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-14
      • 1970-01-01
      相关资源
      最近更新 更多