AXForum  
Вернуться   AXForum > Microsoft Dynamics AX > DAX Blogs
All
Забыли пароль?
Зарегистрироваться Правила Справка Пользователи Сообщения за день Поиск

 
 
Опции темы Поиск в этой теме Опции просмотра
Старый 06.02.2008, 05:50   #1  
Blog bot is offline
Blog bot
Участник
 
25,631 / 848 (80) +++++++
Регистрация: 28.10.2006
Issues concerning X++: Caveat: Using the += and -= operators for dates.
Источник: http://blogs.msdn.com/x/archive/2008...for-dates.aspx
==============

As you may know, it is possible in X++ to add integers to dates. The semantics are that the integer value is considered a number of days to add or subtract to the date.
X++:
{ 
   Date d; 
   // ... 
   d = d + 7; // Seven days later 
   // ... 
   d = d - 7; // Seven days prior. 
}
The example above works well. However, the programmer may see the code above and decide that it can be expressed more succinctly by using the composite assignment operators += and -=. In the case above there is no gain by using these, but in cases where the lefthand side is very complex (involving lots of method calls with side effects) a case can be made for the argument that it should be replaced with += and -=.
The obvious thing to do then is:
X++:
{ 
   Date d; 
   // ... 
   d += 7; // Seven days later 
   // ... 
   d -= 7; // Seven days prior. 
}
This too will work as expected. The problem occurs if you try to use the += operator to subtract days or the -= operator to add days; that is, supplying a negative number of days to add or subtract.
X++:
{ 
   Date d; 
   // ... 
   d += 7; // Seven days later 
   // ... 
   d += -7; // ERROR Seven days prior. 
}
The problem is based in the fact that the compiler will generate code to do a type conversion from the integer argument to a date, which will succeed for positive values, but not for negative ones. It is not easy to fix, but in due course we will, either by disallowing the composite assignment operators on dates or by throwing an exception when the argument is negative.
To make matters even worse, the kernel will simply show a dialog box but continue execution with wrong data afterwards.
The learning from all this is that if you use these operators for dates, you must make sure the expressions on the right hand side always evaluate to non-negative values. If you cannot guarantee this, use the d = d + expr syntax instead.





Источник: http://blogs.msdn.com/x/archive/2008...for-dates.aspx
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору.
 


Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход

Рейтинг@Mail.ru
Часовой пояс GMT +3, время: 16:36.