Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent code of CreateObject in C#

Tags:

I have a code in VB6. Can anyone tell me how to write it in C#. This code is below:

Set Amibroker = CreateObject("Broker.Application")
Set STOCK = Amibroker.Stocks.Add(ticker)
Set quote = STOCK.Quotations.Add(stInDate)

quote.Open = stInOpen
quote.High = stInHigh
quote.Low = stInlow
quote.Close = stInYcp
quote.Volume = stInVolume


Set STOCK = Nothing
Set quote = Nothing

What is the equivalent of CreateObject in C#?. I try to add references to com object but i can't find any com object as Broker.Application or amibroker

like image 754
dralialadin Avatar asked Dec 05 '12 09:12

dralialadin


2 Answers

If you are using .net 4 or later, and therefore can make use of dynamic, you can do this quite simply. Here's an example that uses the Excel automation interface.

Type ExcelType = Type.GetTypeFromProgID("Excel.Application");
dynamic ExcelInst = Activator.CreateInstance(ExcelType);
ExcelInst.Visible = true;

If you can't use dynamic then it's much more messy.

Type ExcelType = Type.GetTypeFromProgID("Excel.Application");
object ExcelInst = Activator.CreateInstance(ExcelType);
ExcelType.InvokeMember("Visible", BindingFlags.SetProperty, null, 
    ExcelInst, new object[1] {true});

Trying to do very much of that will sap the lifeblood from you.

COM is so much easier if you can use early bound dispatch rather than late bound as shown above. Are you sure you can't find the right reference for the COM object?

like image 195
David Heffernan Avatar answered Sep 21 '22 07:09

David Heffernan


If you use .NET Framework 4.0 and above, you can use this pattern:

public sealed class Application: MarshalByRefObject {

    private readonly dynamic _application;


    // Methods
    public Application() {
        const string progId = "Broker.Application";
        _application = Activator.CreateInstance(Type.GetTypeFromProgID(progId));
    }

    public Application(dynamic application) {
        _application = application;
    }

    public int Import(ImportType type, string path) {
        return _application.Import((short) type, path);
    }

    public int Import(ImportType type, string path, string defFileName) {
        return _application.Import((short) type, path, defFileName);
    }

    public bool LoadDatabase(string path) {
        return _application.LoadDatabase(path);
    }

    public bool LoadLayout(string path) {
        return _application.LoadLayout(path);
    }

    public int Log(ImportLog action) {
        return _application.Log((short) action);
    }

    public void Quit() {
        _application.Quit();
    }

    public void RefreshAll() {
        _application.RefreshAll();
    }

    public void SaveDatabase() {
        _application.SaveDatabase();
    }

    public bool SaveLayout(string path) {
        return _application.SaveLayout(path);
    }

    // Properties
    public Document ActiveDocument {
        get {
            var document = _application.ActiveDocument;
            return document != null ? new Document(document) : null;
        }
    }

    public Window ActiveWindow {
        get {
            var window = _application.ActiveWindow;
            return window != null ? new Window(window) : null;
        }
    }

    public AnalysisDocs AnalysisDocs {
        get {
            var analysisDocs = _application.AnalysisDocs;
            return analysisDocs != null ? new AnalysisDocs(analysisDocs) : null;
        }
    }

    public Commentary Commentary {
        get {
            var commentary = _application.Commentary;
            return commentary != null ? new Commentary(commentary) : null;
        }
    }

    public Documents Documents {
        get {
            var documents = _application.Documents;
            return documents != null ? new Documents(documents) : null;
        }
    }


    public string DatabasePath {
        get { return _application.DatabasePath; }
    }

    public bool Visible {
        get { return _application.Visible != 0; }
        set { _application.Visible = value ? 1 : 0; }
    }

    public string Version {
        get { return _application.Version; }
    }
}

}

Next you must wrap all AmiBroker OLE Automation classes. For example wrap Commentary class:

public sealed class Commentary : MarshalByRefObject {

    // Fields
    private readonly dynamic _commentary;


    // Methods
    internal Commentary(dynamic commentary) {
        _commentary = commentary;
    }

    public void Apply() {
        _commentary.Apply();
    }

    public void Close() {
        _commentary.Close();
    }

    public bool LoadFormula(string path) {
        return _commentary.LoadFormula(path);
    }

    public bool Save(string path) {
        return _commentary.Save(path);
    }

    public bool SaveFormula(string path) {
        return _commentary.SaveFormula(path);
    }
}
like image 25
Eldar Agalarov Avatar answered Sep 22 '22 07:09

Eldar Agalarov