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'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:
'mscorlib, Version=220.127.116.11, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Collections.Generic.Dictionary`2
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..
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 also provided the examples as a download on mibuso, for your convenience.. :http://www.mibuso.com/dlinfo.asp?FileID=1342
very useful for me..thanks..