views:

157

answers:

1

hello, I have had a hard time getting and answer to this and i would really , really appreciate some help on this.

i have been on this for over 2 weeks without headway.

i want to use c# to add a line of stock data to amibroker but i just cant find a CLEAR response on how to instantiate it in C#.

In VB , I would do it something like;

Dim AmiBroker = CreateObject("Broker.Application")                
sSymbol = ArrayRow(0).ToUpper
Stock = AmiBroker.Stocks.Add(sSymbol)
iDate = ArrayRow(1).ToLower
quote = Stock.Quotations.Add(iDate)
quote.Open = CSng(ArrayRow(2))
quote.High = CSng(ArrayRow(3))
quote.Low = CSng(ArrayRow(4))
quote.Close = CSng(ArrayRow(5))
quote.Volume = CLng(ArrayRow(6))

The problem is that CreateObject will not work in C# in this instance.

I found the code below somewhere online but i cant seem to understand how to achieve the above.

Type objClassType; 
objClassType = Type.GetTypeFromProgID("Broker.Application");
// Instantiate AmiBroker
objApp = Activator.CreateInstance(objClassType);
objStocks = objApp.GetType().InvokeMember("Stocks", BindingFlags.GetProperty,null, objApp, null); 

Can anyone help me here?

Thanks

A: 

The VB code uses something called late binding against a "COM IDispatch" compatible component. Late binding is not supported by C# (up to C# version 3). The C# compiler only compiles code it knows how bind to (called early bind).

To do what you want to do, it would be easier to generate a proxy dll via Visual Studio - select add reference on a project, then select the tab COM, and then search for that ami broker component in the list. This will generate a proxy dll which you can program against using similar code as the one you have showed for VB.

In C# 3.0, you'll discover that you sometimes have to use Type.Missing and that you have to do some additional explicit casting, even though you'd think that it doesn't seem logical.

C# 4.0 has something called dynamic, which allows you to write much cleaner code when accessing COM components.

taoufik