Показать сообщение отдельно
Старый 21.03.2011, 13:59   #1  
Blog bot is offline
Blog bot
Участник
 
25,643 / 848 (80) +++++++
Регистрация: 28.10.2006
In my initiative to start blogging about .Net Interop, and putting some kind of "useful" examples online (and because

Microsoft's attempt isn't really a good one, imho) .. I might have started of a little bit complicated. You might remember my first post about it: "NAV 2009 R2 .Net Interop: Calling a Web Service". In that post, I had to use both the .Net Framework, and a self made proxy class, which I had to deploy to (classic) client and (RTC) server to be able to use with .Net Interop.

The .Net framework contains a huge amount of code, functionality, ... or how do you kids call it these days (), so in many cases, you won't even have to go and write your own classes, and use it via .Net Interop in stead of COM (yes, you can expect that COM is going to disappear in the future - and who can blame that? :-)). You'll be able to just use the functionality that is out there .. within C/AL.

The example in this post is about a new way to use arrays in your favorite development environment (yes, I mean C/SIDE - and yes, I'm serious ).

In the old days

You have always been able to use arrays in C/SIDE. You did this by setting the property "Dimensions" (which was quite confusing for some) to a certain value:



This way, you set the number of elements an array could contain. I'm not going to spend time on explaining you how you could have been using it .. but I do want to stress out that it (as you can see) was always a fixed size. In many cases, you use arrays when you don't know how many elements you need in that array .. and in those cases, this wouldn't be useable at all .. and you had to use a temp table to work around that.. .

Look at this very simple, unuseful, but hopefully clarifying example:



There are two things in this code that I don't like:
  • I have a fixed number (ARRAYLEN will return '10' as being the number of "possible" elements in my array).
  • I have a check (if there is not text, don't display the message) on whether the value is filled or not. So I don't know how many elements I actually filled up..
I'm sure there are other ways to do the above, but frankly .. the "fixed size" is a bothering situation..

And now with .Net Interop

With .Net Interop, you have the ability to work with .Net Arrays and Collections.. . And this is actually quite powerful. Not only are you able to define (unlimited) arrays on simple data types like "String" (texts), Integer and such .. but you are also able to use this on your own built classes (and this would take us again to the ability to create your own .Net classes like I did in my first .Net Interop example..) and have a collection of class-instances.. . E.g.: you could be building a set (collection) of customers (your class) where you could be executing your own business logic (method in your class) or whatever.. .

But let's go back to the basics

There are different types of arrays and collections. And in .Net, they all have their own characteristics. I'm thinking of Arrays, ArrayLists, Dictionaries, Queues (first in first out), SortedList, ... . I'm no .Net expert for sure, so I'd suggest that if you want to know more about it, you read up a little bit on this .. :
Let's do the same as my NAV example, but with dictionary. The code could look like this:



Sure I have more lines of code, but it just makes more sense.. . Let's start with the variables:

Name

DataType

Subtype

Length

dict

DotNet

'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Collections.Generic.Dictionary`2



mytext

Text



30

The first line in code is the constructor, which is quite common in .Net for "Value Instances" (non-static). Next, we fill the collection with data (just the same data as we had in our first example). I can manually define the "index" of the dictionary. I could have used other values as key .. even datatypes. So, let's consider this example:



I loop the customer table, use the Customernumber as "index" of the collection. After this, I can just use the customernumber again, to get to the entry in my dictionary. I don't care anymore how many entries my array could hold .. it's not necessary to define that.. . Just work and enjoy! :-).

You have to agree this opens up opportunities.. .

Oh yeah? What opportunies are you talking about?

Well, in many cases, we have been using Temp tables to do quite the same. Why not using an array, to distinct or sum up groups of values.. . No need to use a table and reuse a field for that ... .

Or you could use a single instance codeunit, store some settings into an array when starting up, and use it whenever you want in the application..

Or...

You can probably come up with much better examples to use collections

A few remarks

I would like to end with a few remarks:
  • I'm not a .Net guru, so I wouldn't know the exact details of what types of collections should be used in what case (or does it matter). I just picked a dictionary as it seemed to be most useable in most scenario's.
  • .Net Interop code will only run in the RTC Client, so don't be tempted to use .Net Collections in your classic environment!
  • When using a .Net Array (System.Array), it's indexed at 0, while a NAV Array is indexed at 1
  • A .Net Array may be assigned to a .Net array
  • A NAV Array assignment to a NAV Array or .Net Array is NOT possible
  • A .Net Array doesn't use a [index] syntax
  • You can't pass a .Net Array as a NAV Array Parameter or Return Type
I also provided the examples as a download on mibuso, for your convenience.. :
http://www.mibuso.com/dlinfo.asp?FileID=1342



Читать дальше
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору.