【问题标题】:How to create dynamic array of DateTime?如何创建 DateTime 的动态数组?
【发布时间】:2012-11-29 19:34:18
【问题描述】:

我需要创建一个 DateTime 的动态数组,但我事先不知道我需要添加多少个日期。我确实对 ArrayList 进行了实验,但它没有帮助。

那么,如何在 Delphi Prism 中创建 DateTime 的动态数组?

你是这样做的吗?

mydates: array of DateTime;

更新 1

我执行了以下操作,编译器说这些参数没有重载 set_BoldedDates。"

  mydates:ArrayList;
  mydates := new ArrayList;
  mydates.Add(new DateTime(2012,11,23));

  DataCalendar.BoldedDates := mydates; //also I did mydates.ToArray caused error.

上述代码仅在我将 mydates 设置如下时才有效:

const
mydates : Array[0..1] of DateTime = [new DateTime(2012,11,23), new Datetime(2012,11,13)];

谢谢,

【问题讨论】:

  • List<DateTime> dates = new List<DateTime>();delphi没有泛型吗?
  • @LarsTech,是的,那将是 Delphi Prism 中的 ArrayList 或 Delphi 中的 TList,但我需要 DateTime 数组。如果我这样做mydates: array of DateTime,编译器会引发错误。
  • 为什么 ArrayList 不起作用的背景故事是什么?
  • @LarsTech :) 我正在使用我在 Internet 上找到的用户控件月历。它有一个名为BoldedDates 的属性,它只接受日期时间数组。它不允许我为 DateTime 数组设置 ArrayList。
  • 显然我不了解Delphi。 .ToArray() 不能在 ArrayList 或 TList 上工作吗?

标签: .net arrays delphi-prism oxygene


【解决方案1】:

这对我来说很好,并且可以正确显示日期。 (注意:没有针对解析错误或超出范围日期的错误处理!这严格设计用于显示在 Delphi Prism 中 array of DateTime 与 MonthCalendar.BoldedDates 的使用。)

method MainForm.button2_Click(sender: System.Object; e: System.EventArgs);
var
  Dt: array of System.DateTime;
  TheSize: Int32;
begin
  TheSize := Int32.Parse(textBox1.Text);

  Dt := new System.DateTime[TheSize];
  for i: Int32 := 0 to TheSize - 1 do
    Dt[i] := new DateTime(2012, 11, i + 4);

  monthCalendar1.BoldedDates := Dt;  
end;

在文本框中输入5 的测试显示了以下结果:

【讨论】:

  • Ken,虽然我使用的不是.NET 月历,而是我在互联网上找到的第三方用户控件,但两个控件中的BoldedDates 属性的处理方式相同。我不使用 .NET MonthCalendar 的唯一原因是我不能只将粗体日期的颜色或它们的背景颜色更改为我需要设置的颜色。无论如何,您的答案确实按预期工作。谢谢。
猜你喜欢
  • 2011-10-20
  • 1970-01-01
  • 2021-08-17
  • 2015-07-15
  • 2015-01-06
  • 1970-01-01
  • 1970-01-01
  • 2011-02-12
相关资源
最近更新 更多