JonoB
JonoB

Reputation: 5887

Adobe flex dateField

I have some code as follows:

private function onComboChange(evt:Event):void {
  var temp:Date = df_date.selectedDate;
  temp.date += 5;
  df_dateDue.selectedDate = new Date(temp);
}

In essence, I am trying to add 5 days onto the selected date in df_date, and put that date into df_dateDue. This fires off via an EventListener on a combobox. Both df_date and df_dateDue are dateFields.

OK, so the first time that I run this, it works fine; df_date stays the same and df_dateDue is set to 5 days past df_date. However, the next time that I run it, df_dateDue increments by 10 days from df_date, the next time by 15, and so on.

So, stepping through the code shows that somehow df_date has become linked to the temp var, and that the temp var is not resetting itself each time the function is called.

Example: df_date = 01 Jan, df_dateDue = 01 Jan.

  1. Fire off the event, df_date = 01 Jan, df_dateDue = 06 Jan

  2. Fire off the event again. At this point, var temp = 06 Jan (even though df_date still shows 01 Jan), and df_dateDue is then set to 11 Jan

  3. Fire off the event again. At this point var temp = 11 Jan (even though df_date = 01 Jan), and df_dateDue is then set to 16 Jan

What am I missing here?

Upvotes: 0

Views: 1222

Answers (1)

JeffryHouser
JeffryHouser

Reputation: 39408

In Flex/AS, variables that contain objects are really just pointers to some memory space. Date's in Flex are an Object, not a native type. This line:

var temp:Date = df_date.selectedDate;

Creates a new pointer to an existing date object. It does not create a copy.

This line:

temp.date += 5;

increments the date property of the dateObject. All references pointing to that date object will be updated. Try using objectUtil.copy

var temp:Date = ObjectUtil.copy(df_date.selectedDate) as Date;

Upvotes: 2

Related Questions