3

I need to create a dynamic array of DateTime, but I don't know before hand how many dates I will have to add. I did experiment with ArrayList, but it doesn't help.

So, how do you create dynamic array of DateTime in Delphi Prism?

Is this how you do it?

mydates: array of DateTime;

UPDATE 1

I did the following and compiler says that there is no overloaded set_BoldedDates with these parameters."

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

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

The above code only works if I set the mydates as follows:

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

Thanks,

Ken White
  • 123,280
  • 14
  • 225
  • 444
ThN
  • 3,235
  • 3
  • 57
  • 115
  • `List dates = new List();` Does delphi not have generics? – LarsTech Nov 29 '12 at 19:37
  • @LarsTech, Yes and that would be ArrayList in Delphi Prism or TList in Delphi, but I need array of DateTime. If I do `mydates: array of DateTime`, it complier raises an error. – ThN Nov 29 '12 at 19:43
  • What's the background story on why ArrayList doesn't help? – LarsTech Nov 29 '12 at 19:51
  • @LarsTech :) I am using an usercontrol, Month Calendar, that I found on the Internet. It has a property called BoldedDates, which only accepts array of datetime. It won't allow me to set ArrayList for array of DateTime. – ThN Nov 29 '12 at 20:01
  • Obviously I don't know Delphi. Wouldn't a .ToArray() work on the ArrayList or TList? – LarsTech Nov 29 '12 at 20:03
  • One last shot: ArrayList is a list of objects, so the method is probably choking on that. If TList is equivalent to List, then try using the ToArray() on the TList, since it should return an array of DateTimes. – LarsTech Nov 29 '12 at 20:27
  • @LarsTech, I don't have access to TList. I am using Delphi-Prism Visual Studio 2010 for .NET. So, I can only use ArrayList. – ThN Nov 29 '12 at 20:30
  • @LarsTech: Delphi Prism is the .NET version of Delphi, and it has access to the standard .NET data types (including generics, which aren't needed here - a standard array of System.DateTime works fine). – Ken White Nov 30 '12 at 00:12

1 Answers1

3

This works fine for me, and displays the dates properly. (Note: there is no error handling for parsing errors or out of range dates! This is strictly designed to show the use of the array of DateTime with MonthCalendar.BoldedDates in Delphi Prism.)

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;

A test entering 5 in the textbox shows this result:

Sample display of bolded dates

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • Ken, Although I am not using .NET month calendar but a third-party usercontrol that I found on the internet, BoldedDates property in both controls treated the same way. The only reason I am not using .NET MonthCalendar is because I can't change just the color of boldeddates or their background color to color I need to set. Anyways your answer does work as expected. Thanks. – ThN Nov 30 '12 at 14:53