Maptitude GISDK Help |
One key difference between C# and VB.NET is how they handle "late binding" and "early binding". Early binding, also known as static binding, refers to the process where method calls and property accesses are resolved at compile time. Late binding, on the contrary, is the process where method calls and property accesses are resolved at runtime. VB.NET has more native support for late binding, while C# supports late binding through the dynamic keyword and reflection.This difference significantly impacts how return values from the DoFunction() or DoMacro() methods are managed in each language, specially when the returned value is a GISDK compound variable.
VB.NET Example
In VB.NET, late binding allows for more flexibility when dealing with objects. Here’s an example that works in VB.NET:
' VB.NET example that works
Dim SomeCoord As Object
SomeCoord = Gisdk.DoFunction("Coord", -118071223, 36.112608)
Console.WriteLine("The Longitude of the coordinate is {0}", SomeCoord.Lon)
In this code, SomeCoord is declared as an Object, and VB.NET can resolve the Lon property at runtime. This is possible because VB.NET uses late binding, which allows the CaliperForm/CaliperNet assembly to resolve and access the Lon property dynamically.
C# Examples
In contrast, C# uses early binding by default, which requires explicit type definitions at compile time. The equivalent C# code would fail:
// C# code that fails
Object SomeCoord;
SomeCoord = Gisdk.DoFunction("Coord", -118071223, 36.112608);
Console.WriteLine("The Longitude of the coordinate is {0}", SomeCoord.Lon);
This code fails because SomeCoord is a plain .NET Object, and the Lon property is not defined for the Object type. C# uses early binding, meaning it does not dynamically resolve the Lon property at runtime by default.
To overcome this limitation in C#, you can use either of the following approaches:
dynamic
; the C# compiler bypasses compile-time type checking for that object, deferring it until runtime.
Example 1. Using reflection to get a coordinate latitude and longitude
using System;
using System.Reflection;
using CaliperForm
...
...
static public void Get_Coord_LatLon()
{
// Initialize GISDK connection
Connection Conn = new Connection();
Conn.Open();
// Example usage of DoFunction
object SomeCoord = Conn.DoFunction("Coord", -118071223, 36112608);
// Use reflection to get the laittude and longitude
Type GisdkType = SomeCoord.GetType();
object longitude = GisdkType.InvokeMember(
"lon",
BindingFlags.GetProperty,
null,
SomeCoord,
null
);
object longitude = GisdkType.InvokeMember(
"lat",
BindingFlags.GetProperty,
null,
SomeCoord,
null
);
Console.Out.WriteLine("The Longitude of the coordinate is {0} ", longitude);
Conn.Close();
}
This example uses the Reflection.InvokeMember method of the Type class to dynamically access the specified property (propertyName) of the targetObject. By using reflection, C# can dynamically resolve and retrieve the property value at runtime, similar to how VB.NET handles late binding.
Example 2. Getting a scope properties using dynamic casting
using System;
using CaliperForm
...
...
static public void Get_Scope_Properties()
{
// Initialize the GISDK connection
Connection Gisdk = new Connection();
Gisdk.Open();
// Define an array of points using the Coord function
object[] points = new object[]
{
Gisdk.DoFunction("Coord", -72999114, 400032221),
Gisdk.DoFunction("Coord", -73325463, 398023816),
Gisdk.DoFunction("Coord", -74087023, 401237433)
};
// Retrieve the map units abbreviation
object mapUnitsAbbreviation = Gisdk.DoFunction("GetMapUnits", "Abbreviation");
// Get the scope of the array, cast the result as dynamic
var scp = (dynamic) Gisdk.DoFunction("GetArrayScope", new object[] { points });
// Get the scope width
double width = scp.width;
// The scope center is a GISDK coord type, we need to cast it as dynamic in order to obtain latitude and longitude
var center = (dynamic) scp.center;
int center_lon = center.lon;
int center_lat = center.lat;
// Output the scope's width and center
Console.WriteLine("The scope encompassing the points in the array is {0:0.00} {1} miles wide .", width, mapUnitsAbbreviation);
Console.WriteLine("The scope center is at lon/lat ({0:0.000000} , {1:0.000000}) ", (double) center_lon/1000000, (double) center.lat/1000000);
// Close the GISDK connection
Gisdk.Close();
}
This example casts the scp object to dynamic
, and subsequently casts the center property of the scope oject, allowing the types and members to be resolved at runtime.
For more information, see:
Getting Started with CaliperForm
Using the CaliperForm.Connection Class
Using the CaliperForm.GisdkDataAdapter Class
Using the GISDK Extension Engine In-Process API
Using the GISDK CaliperForm Out-of-Process API
See also these specific C# examples for:
©2025 Caliper Corporation | www.caliper.com |