【问题标题】:Need to Add 3 Day to current Transaction Date需要在当前交易日期上增加 3 天
【发布时间】:2017-10-07 18:28:54
【问题描述】:

我有一个按钮,需要在 ArrayList 中的当前交易日期上增加 3 天。

如何做到这一点?

代码如下:

private void btnCheckCleared_Click(object sender, EventArgs e)
{
    foreach (Transaction item in tranArray)
    {
        if (What goes here?)
        {
            DateTime.Today.AddDays(3).ToLongDateString();
        }
    }
}

如果您需要更多代码,请告诉我。

这是我的 Transaction.cs 代码:

namespace TEXT TEXT TEXT

{
    public class Transaction
    {
        //data hiding (blackbox)
        //visible only to class itself
        //fields (variables)
        //4 member variables (instantiate object)
        private decimal decAmount;
        private DateTime dteTransactionDate;
        private string strCheckNumber;
        private string strPayee;
        private string strTypeTrans;
        public bool CheckCleared;

        public decimal Amount
        {
            get
            {
                return decAmount;
            }
            set
            {
                decAmount = value;
            }
        }

        public string CheckNumber
        {
            get
            {
                return strCheckNumber;
            }
            set
            {
                strCheckNumber = value;
            }
        }

        public string Payee
        {
            get
            {
                return strPayee;
            }
            set
            {
                strPayee = value;
            }
        }

        public DateTime TransactionDate
        {
            get
            {
                return dteTransactionDate;
            }
            set
            {
                dteTransactionDate = value;
            }
        }


        public TransactionType TypeTrans;

        //constructor 
        public Transaction(string payee, decimal amount, TransactionType typeTrans, DateTime transactionDate)
        {
            this.Payee = payee; //assignment operator =
            this.Amount = amount; //this is to qualify
            this.TypeTrans = typeTrans;
            this.TransactionDate = transactionDate;
        }

        public Transaction(string payee, decimal amount, TransactionType typeTrans, DateTime transactionDate, string checkNumber)
        {
            this.Payee = payee; //assignment operator =
            this.Amount = amount; //this is to qualify
            this.CheckNumber = checkNumber;
            this.TypeTrans = typeTrans;
            this.TransactionDate = transactionDate;
        }

        //public Transaction ()

        public override string ToString()
        {
            return this.TransactionDate.ToShortDateString() + " " + this.Amount.ToString("C") + "\t" + this.TypeTrans;
        }
    }
}

【问题讨论】:

  • 你的问题不清楚,能不能多解释一下。
  • Transaction 类是什么样的?它是否有要修改的 DateTime 字段?为什么我们会知道您想要什么作为“如果”条件?
  • 好吧,您正在对事务进行操作,因此您的“如果”可能需要对事务类中的某些内容进行条件检查。因此,您的 if 将类似于“if (item.[Property] == [value])。此外,您需要设置 Transaction 类的一些属性来更新它。您可能希望将 3 天添加到课堂上的当前日期,而不是今天。所以:item.[DateProperty] = item.[DateProperty].AddDays(3);
  • 只要支票日期至少在过去 3 天,标准要求一个新字段来存储支票是否已清算(布尔值)。例如现在,它显示,日期金额名称, 所以当用户点击 btnCheckClear 时,需要显示 Date Amount Name Cleared (Check) #.
  • -Aaron,您是说我需要更新 Transaction.cs 文件以显示更改并更新 main.cs?

标签: c# datetime


【解决方案1】:

你需要这样的东西:

private void btnCheckCleared_Click(object sender, EventArgs e)
{
    foreach (Transaction item in tranArray)
    {
        if (/*Whatever your condition looks like*/)
        {
            item.TransactionDate = item.TransactionDate.AddDays(3);
        }
    }
}

AddDays修改给定的DateTime,但返回一个DateTime,这就是它被分配回item.TransactionDate的原因

【讨论】:

  • 我应该设置什么条件?
  • 如果要更新数组中的每个项目,则不需要条件。仅在 存在 应更新项目的条件时指定它(例如,如果 TransactionDate 具有特定值)
猜你喜欢
  • 2012-08-12
  • 1970-01-01
  • 1970-01-01
  • 2014-10-11
  • 1970-01-01
  • 1970-01-01
  • 2019-02-08
  • 2023-03-23
  • 1970-01-01
相关资源
最近更新 更多