factory create method in C#?
-
15-03-2021 - |
Question
I am working on an API which supplies some ActiveX COM Object and I read these warning below:
"You must use the factory “create” methods to create the COM objects in this section. Once a COM object has been created by a factory method, the COM object is tied to a corresponding TWS COM object (an instance of the COM object). Do not try to pass a COM object to another instance of a TWS COM object."
These words come from a part of VB example on this ActiveX API. Now I am using c#, what should I do to follow this rule?
Solution
Not knowing what any of these data types are or what you are naming these types, let's go with a data type called ActiveXType
that is defined in ApiComObject
.
class TwsCom {
private ApiComObject apiComObject;
public TwsCom() {
apiComObject = new ApiComObject(); // create an instance, if required
}
// you might want to keep this variable type private to avoid breaking
// the rules
private ActiveXType NewActiveXType() {
return apiComObject.Create();
}
public object SomeMethod() {
ActiveXType activeX = NewActiveXType();
return activeX.SomeMethod();
}
}
This code is all very vague, but that's about the best I can do unless you'd like to provide more details.