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

 
 
Опции темы Поиск в этой теме Опции просмотра
Старый 14.05.2017, 12:16   #1  
Blog bot is offline
Blog bot
Участник
 
25,475 / 846 (79) +++++++
Регистрация: 28.10.2006
ievgensaxblog: D365O. X++ objects equality.
Источник: https://ievgensaxblog.wordpress.com/...ects-equality/
==============



There are two different kinds of equality: equality by reference and equality by value. I’m not going to explain these basic concepts here, because you can find tons of information in the web, today I want to show how you can implement value based comparison in X++.

By default AX uses reference check. In previous versions we could override equal method of the Object class to implement value based comparison, however, we have to explicitly derive new class from Object, because it was not derived by default and equal was not called by AX (as far as I know). For example, if you wanted to use value equality with collections, like set, you had to workaround using map where key is combination of fields and value is object itself.

With new version of AX we moved to .NET world where X++ is almost a first class citizen. Why almost?  There are bunch of limitations and not implemented features like generics, but we slowly getting there.

Let’s get back to value equality. Now, each class is derived from System.Object. System.Object has Equals and GetHashCode methods used to check equality that we can override. Again, there are lots of info in the web about these two methods and guidelines how to override them, so I’ll jump straight into the example.

I have a new class with two public fields:

class MyClass{ public int a; public int b;}In my test job, I want to store instances of MyClass in a set. Set should contain only instances with unique combination of a and b values.

public static void main(Args _args){ MyClass myClass1 = new MyClass(); myClass1.a = 5; myClass1.b = 10; MyClass myClass2 = new MyClass(); myClass2.a = 5; myClass2.b = 10; Set set = new Set(Types::Class); set.add(myClass1); set.add(myClass2); info(int2Str(set.elements()));}As we already know, AX will add 2 elements into the set because it always performs reference check by default.

Let’s override Equals and GetHashCode using MSDN guidelines

public int GetHashCode(){ return this.a ^ this.b;}public boolean Equals(System.Object _obj){ if (_obj == null) { return false; } if (System.Object::ReferenceEquals(this, _obj)) { return true; } if (!this.GetType().Equals(_obj.GetType())) { return false; } MyClass compareTo = _obj; if (compareTo.a == this.a && compareTo.b == this.b) { return true; } else { return false; }}It’s just an example how “standard” features from .NET could be used in X++. Moving forward, you can expand this example implementing IComparable interface, so new class could be added to any standard .NET collection and be sorted! But it’s a topic for another blog.




Источник: https://ievgensaxblog.wordpress.com/...ects-equality/
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору.
 

Похожие темы
Тема Автор Раздел Ответов Посл. сообщение
D365O. How to deploy Demo VM using Visual Studio subscription. Blog bot DAX Blogs 0 23.03.2017 11:11
stoneridgesoftware: Version Control in Dynamics AX: Check-in a project and all of its pending objects Blog bot DAX Blogs 0 11.11.2016 04:26
NAV Team: Merging Application Objects using Windows PowerShell in Microsoft Dynamics NAV 2015 Blog bot Dynamics CRM: Blogs 0 03.10.2014 14:30
emeadaxsupport: Group policy setting "System Objects: Default owner for objects created by members of the administrators group" is missing on Windows Server 2008 Blog bot DAX Blogs 2 28.08.2009 02:14
NAV Team: Automation Objects in Microsoft Dynamics NAV 2009 Blog bot Dynamics CRM: Blogs 0 03.12.2008 17:27
Опции темы Поиск в этой теме
Поиск в этой теме:

Расширенный поиск
Опции просмотра

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

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

Рейтинг@Mail.ru
Часовой пояс GMT +3, время: 10:05.
Powered by vBulletin® v3.8.5. Перевод: zCarot
Контактная информация, Реклама.