I'm trying to make a simple addin. The developers documentation states this:
Late bind to ETABS.exe, create an instance of the ETABSObject, and get a reference to the cOAPI interface.
I'm very confused on how to late bind to an already running exe. If you could give me an example or point me in the right direction I would really appreciate it. I've been banging my head on wall for days.
When performing Early Binding, an object is assigned to a variable declared to be of a specific object type. Early binding objects are basically a strong type objects or static type objects. While Early Binding, methods, functions and properties which are detected and checked during compile time, perform other optimizations before an application executes. The biggest advantage of using early binding is for performance and ease of development.
System.IO.FileStream FS ; FS = new System.IO.FileStream("C:\\temp.txt", System.IO.FileMode.Open);
Above code creates a variable FS to hold a new object and then assigns a new object to the variable. Here type is known before the variable is exercised during run-time.The FileStream is a specific object type, the instance assigned to FS is early bound.
While preforming Early Binding the compiler can ensure at compile time that the function will exist and be callable at runtime. Moreover the compiler guarantees that the function takes the exact number of arguments and that they are of the right type and can checks that the return value is of the correct type.
By contrast, in Late binding functions, methods, variables and properties are detected and checked only at the run-time. It implies that the compiler does not know what kind of object or actual type of an object or which methods or properties an object contains until run time. The biggest advantages of Late binding is that the Objects of this type can hold references to any object, but lack many of the advantages of early-bound objects.
//The var keyword instructs the compiler to infer the type of the variable // from the expression on the right side of the initialization statement. var eTabsObj = null;//notice the var keyword //instead of var you can use an interface variable to hold reference // of late bind object eTabsObj = CreateObject("<create an instance of ETABSObject>");//executed at runtime
Above code does not require a reference to be set beforehand, the instance creation and type determination will happen at runtime. It is important to note that the Late binding can only be used to access type members that are declared as Public. Accessing members declared as Friend or Protected Friend result in a run-time error.
To answer your specific question, you are creating an object during runtime, and it makes sense because when the author of the ETABS.exe wrote the code he didn't knew how you would like to use it in your project.You created an object in the way which suits your requirement.
Notice how we used var to hold reference to ETABS object , var is basically a syntactic sugar which infers object type from the object being assigned. Now instead of using var you could use an interface variable which ETABS class implements to hold the reference of the object.
While perform late binding there is a possibility of the target function may not exist. Also the target function may not accept the arguments passed to it, and may have a return value of the wrong type.
Hope this helps !
objectwhen the Datatype is not known beforehand.