Vectors and ".NET Automatic Type Conversion"


Frederik Soete
 

Hi, fellow Magicians,

I have included a piece of the Magic documentation below, which appears to offer tantalizing possibilities for converting vectors to and from .Net datatypes.

Given that documentation, I must admit, in practice I am still not able to pass a vector of e.g. strings to a .Net snippet.

Apparently the type conversion is not meant to do that? (But what does it do, I wonder?)

Does anyone know a way to send the contents of a big vector to a .Net snippet, using this automatic conversion or something like it?

I mean without creating a DotNet array variable, and then manually updating each individual cell through a laborious and timeconsuming block-while-loop?

Or should I just give up on the idea of a fast way of passing a vector to .Net?

Thanks in advance, for all help in understanding this arcane part of the Magic documentation.

Bye,

Frederik Soete


.NET Automatic Type Conversion

Magic xpa automatically converts Magic xpa types to .NET types and vice versa; so, for example, it is possible to send Magic xpa variables to .NET methods without performing cast on them.

The conversion rules are as follows:

Magic xpa Type

.NET Type

Numeric

SByte, Byte , Int16, Uint16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Char, Decimal, Single, Double, Float

Alpha, Unicode

Char, Char[], String, StringBuilder

Date

DateTime

Time

DateTime, TimeSpan

Logical

Boolean

BLOB

Byte, Byte[], Char, Char[], String, StringBuilder

Vector

ICollection (only from .NET to Magic xpa), IList and objects that implement Indexers through the usage of 'this' keyword. Only indexers that have integer indexes can be converted.

 

  • There is no automatic conversion for nested vectors.

  • NET arrays are not automatically converted to a Magic type.

  • Usually you can use simple values (such as strings or numbers) and Magic xpa will automatically convert them to the proper .NET variable type. But, when the conversion is not unique (for example, the method is overloaded and can receive both Int and Long variables) or that you need to specifically convert the value to a .NET type, you will have to use the DNCast() function to specifically do the conversion.

  • When trying to update a .NET Datetime property with a number, such as Datetime+12, the automatic conversion will fail and you will receive a syntax error. This is because the Magic xpa internal representation of a date is numeric, so the end result is numeric. To solve this issue you can use the Magic xpa AddDate function.

Automatic conversion is used when:

  • Sending or receiving values to and from .NET calls.

  • Updating fields by any expression.


Frederik Soete
 

Hi group,

In the meantime, I have tried my hand at the problem yet again. Contrary to what the docs seem to allow, I have still not succeeded in casting a Magic vector into a DotNet object in one go.

But I have found a good workaround in the idea that you can fill a "System.Collections.ArrayList" object just like a vector, and pass that to a DotNet invoke. This should be adequate for most purposes.
Better yet: the array list may contain all kinds of differently typed .Net objects.

By the way, searching this group for "ArrayList", I have found a previous short post by Andreas Sedlmeier. Hats off to him. It just goes to show I should have searched the group more thoroughly before asking.

That post's URL was: https://magicu-l.groups.io/g/main/message/176432

Thanks for reading,

Frederik Soete.