microsoft.net interoperability for beginners vjekoslav babic (fortempo)

51
MICROSOFT .NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

Upload: ross-chase

Post on 26-Dec-2015

244 views

Category:

Documents


7 download

TRANSCRIPT

Page 1: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

MICROSOFT .NET INTEROPERABILITY

FOR BEGINNERSVjekoslav Babic

(Fortempo)

Page 2: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

Vjekoslav Babićconsultant, trainer, blogger, author

Blog: vjeko.comE-mail: [email protected]

Author of many How Do I… videos for MSDN and PartnerSource for NAV 2013 and NAV 2013 R2

Co-author of “Implementing Microsoft Dynamics NAV 2009” book

ABOUT ME

Page 3: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

HOW USERS SEE PROGRAMMERS

Page 4: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

HOW PROGRAMMERS SEE USERS

Page 5: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

NAV PROGRAMMER PROGRAMMING IN C/AL

Page 6: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

NAV PROGRAMMER PROGRAMMING IN .NET

Page 7: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

A lot of the time?

WHAT DO PROGRAMMERS REALLY DO?

Page 8: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)
Page 9: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

SO, WHAT DO PROGRAMMERS DO A LOT OF THE TIME?

Page 10: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

UNFORTUNATELY – IT IS OFTEN A WRONG WHEEL

Page 11: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

WHAT ARE WE GOING TO DO TODAY…Managing strings and textManaging dates, times, and numbersAccessing operating system, file system, and the environmentManaging data, arrays, collections, and streams

C/AL.NET

Page 12: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

THE DotNet DATA TYPEVariables of type DotNet give access to .NET Framework

Each DotNet variable specifies:• Assembly• Type

DotNet gives access to:• Methods• Properties• Constructors• Enumerations

Page 13: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

System.StringA powerful class to handle text information.Maps fully and directly to Text data type in C/AL.

Page 14: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

System.Text.StringBuilderA very powerful class to handle large text data efficiently.

Page 15: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

CONSTRUCTORSCreate an instance of a class.

There is no equivalent in C/AL to constructors.CREATE function for instantiating automation objects is the closest

Typically used to:• Initialize the default state of an object• Execute default initialization logic• Limit instantiation

Page 16: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

SYNTAX OF A CONSTRUCTORThe name of the constructor always matches the class name.

This probably looks confusing in C/AL:StringBuilder := StringBuilder.StringBuilder;

(why in the Earth do we need all those StringBuilders?)

Replace the StringBuilder variable name with s, and it’s more manageable:s := s.StringBuilder;

The syntax of the constructor in C/AL is the following:Variable := Variable.[Class Name]({argument1, …});

Page 17: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

OVERLOADINGCapability that allows multiple members to use the same name as long as the signature remains different.

C/AL understands overloading of:• Methods• Constructors

Page 18: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

System.Diagnostics.StopwatchA simple and useful class to handle time measurements.

Page 19: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

NAMESPACEA higher-level scope used to organize code

Namespace has a many-to-many relationship to assemblies:• One assembly can contain multiple namespaces• The same namespace can span multiple assemblies

Page 20: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

System.Text.RegularExpressionsA namespace containing classes for managing regular expressions.

In case you didn’t know:• Regular Expressions (RegEx) are a language used for searching

text data through pattern matching• Regular Expressions are SQL of textual data

Page 21: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

System.DateTimeManages date and time information.Maps fully and directly to DateTime data type in C/AL.

Many useful properties and methods.

Page 22: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

System.GlobalizationNamespace that defines a lot of useful type for managing date and time formats, regional settings, cultures, etc.

Useful classes:• Calendar• CultureInfo• DateTimeFormatInfo• NumberFormatInfo

Page 23: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

System.Globalization.CultureInfoProvides access to culture information. Cultures are the heart of the .NET localization/globalization functionality.

Page 24: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

System.Globalization.DateTimeFormatInfoProvides access to date and time formatting information.

Page 25: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

System.Globalization.NumberFormatInfoProvides access to number formatting information.

Page 26: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

System.MathProvides methods for trigonometric, logarithmic, and other common mathematical functions.

Page 27: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

System.Numerics.BigIntegerRepresents an arbitrarily large integer value, without any theoretical upper or lower bounds.

Page 28: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

System.ConvertConverts a base type to another base type.

Page 29: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

System.IONamespace that provides many useful input and output classes.

Page 30: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

System.IO.FileProvides static methods for the creation, copying, deletion, moving, and opening of files.

Page 31: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

STATIC OBJECTS AND METHODSStatic classes cannot be instantiatedStatic methods can be called on classes

Static classes and members are shared for the whole application domain

Microsoft Dynamics NAV Server runs as a single application domain

Page 32: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

System.IO.DirectoryExposes static methods for creating, moving, and enumerating through directories and subdirectories.

Page 33: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

System.IO.PathPerforms operations on String instances that contain file or directory path information.

Page 34: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

System.IO.FileSystemWatcherListens to the file system change notifications and raises events when a directory, or file in a directory, changes.

Page 35: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

EVENTSEnable a class to notify other classes when something of interest

happens.

Equivalent to C/AL triggers.

Events are not automatically exposed.You must first set the WithEvents property.

Events can run be:• Server-Side: default behavior, if RunOnClient is No• Client-Side: if RunOnClient is Yes

Page 36: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

ENUMERATIONSTypes that consist of set of named constants.

Similar, but not nearly equal to C/AL options.

C/AL .NET

Always of Integer type Of any integer numeric type, except char

Always 0-based 0-based by default, but can be any value

Each consecutive element is increased by 1

Every element can have an explicit numeric value, even negative

Page 37: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

SUPPORT FOR ENUMERATIONS IN C/SIDE

NAV 2009 R2 NAV 2013

Does not recognize them Recognizes them

You must use integers You can use named values

You cannot use bitwise operators (AND, OR, NOT, XOR).You can use + in place of OR, but that’s about it.

Page 38: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

System.Diagnostics.EventLogProvides interaction with Windows event logs.

Page 39: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

MANAGING TYPE INFORMATION AT RUNTIMESystem.TypeGETDOTNETTYPECANLOADTYPE

Page 40: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

ARRAYS AND COLLECTIONSArrays:• Contain a fixed number of values of the same data type• Roughly correspond to C/AL arrays (variables with Dimensions

property)

Collections:• Contain an arbitrary, flexible number of values of the same data

type• No equivalent in C/AL

Both arrays and collections are extensively used in .NET

Page 41: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

System.ArrayProvides methods for creating, manipulating, searching, and sorting

arrays.

Serves as the base class for all arrays in the common language runtime.

Page 42: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

ENUMERATORSEnable iterating through arrays and collections.C# uses the foreach keyword to manage the iteration.

Two interfaces are in charge of the enumerator-based iteration:• IEnumerable<T>• IEnumerator<T>

IEnumerable<T>• Exposes the enumerator

IEnumerator<T>• Allows iteration through enumerable object• Current• MoveNext

Page 43: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

System.ObjectThe ultimate base class of all classes in the .NET Framework.The root of the type hierarchy.

Page 44: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

System.Collections.HashTableRepresents a list of key/value pairs that are stored by using a hash

table.

Page 45: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

System.Collections.Generic.List<T>Represents a strongly typed list of objects that can be accessed by

index.

Provides methods to search, sort, and manipulate lists.

Page 46: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

GENERICSAllow a single design-time declaration that compiles to multiple run-time types.

List<T> can compile to List<int>, List<string>, or List<MyDesiredClass>

Supported on:• Classes and interfaces• Methods• Properties• Structures• Fields

In C/AL, all generics are of type System.Object

Page 47: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

MAPPING .NET TYPES TO C/ALSome types map fully:• String• DateTime• InStream and OutStream

Many data types map uni-directionally:• Most simple data types (Boolean, Decimal, Integer, etc.)• Some complex data types (Duration)

You cannot use uni-directionally mapped .NET data types in C/AL directly.

Page 48: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)

System.IO.StreamA class that provides a generic view of a sequence of bytes.

Typical descendants:• System.IO.MemoryStream• System.IO.FileStream

Fully mutually interchangeable with both InStream and OutStream C/AL types.

Limitation:• CREATEOUTSTREAM and CREATEINSTREAM cannot be called on

System.IO.Stream

Page 49: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)
Page 50: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)
Page 51: MICROSOFT.NET INTEROPERABILITY FOR BEGINNERS Vjekoslav Babic (Fortempo)