0

I'm trying to use a C# tlb as a reference then use that code in VBA. The VBA code looks like this:

Sub startConsumer()
    Dim Consumer As Consumer.netConsumer
    Set Consumer = New Consumer.netConsumer
    MsgBox (Consumer.consume())
End Sub

The original C# code is like this:

namespace netConsumer
{ 
    public class netConsumer
    {

        public static string message;
        public static KafkaOptions options = new KafkaOptions(new Uri("http://rqdsn0c.bnymellon.net:9092"), new Uri("http://rqdsn0c.bnymellon.net:9092"), new Uri("http://rqdsn0c.bnymellon.net:9092"))
        {
            Log = new ConsoleLog()
        };

        public static BrokerRouter router = new BrokerRouter(options);

        public static string consume()
        {

            Task.Factory.StartNew(() =>
            {


                var consumer = new Consumer(new ConsumerOptions("TestHarness3", router));

                foreach (var data in consumer.Consume())
                {
                    Console.WriteLine("Response: P{0},O{1} : {2}", data.Meta.PartitionId, data.Meta.Offset, data.Value);
                    var utf8 = Encoding.UTF8;
                    message += utf8.GetString(data.Value, 0, data.Value.Length);
                    ExcelWorksheet.writeToExcel(message);

                }

          });


            return message;

        } 



    }
}

Earlier it was working but now I'm getting Runtime error 438. Object doesn't support this property or method. I checked other answers and couldn't get a answer to my problem.

shf301
  • 31,086
  • 2
  • 52
  • 86

2 Answers2

2

consume is a static method, COM does not support any support static methods only instance methods and properties. Get rid of the static's from your code.

You're consume method will also always return an empty string. You are stating your Consumer inside of a task but you are not waiting for that task to complete. You'll either need to get rid of the Task and run the Consumer synchronously or add an event to your netConsumer class and raise that event when the task completes.

Synchronous way:

public class netConsumer
{
    public string message;

    public KafkaOptions options = new KafkaOptions(new Uri("http://rqdsn0c.bnymellon.net:9092"), new Uri("http://rqdsn0c.bnymellon.net:9092"), new Uri("http://rqdsn0c.bnymellon.net:9092"))
    {
        Log = new ConsoleLog()
    };

    public BrokerRouter router = new BrokerRouter(options);

    public string consume()
    {
        var consumer = new Consumer(new ConsumerOptions("TestHarness3", router));
        foreach (var data in consumer.Consume())
        {
            Console.WriteLine("Response: P{0},O{1} : {2}", data.Meta.PartitionId, data.Meta.Offset, data.Value);
            var utf8 = Encoding.UTF8;
            message += utf8.GetString(data.Value, 0, data.Value.Length);
            ExcelWorksheet.writeToExcel(message);

        }
        return message;
    }
}

Asynchronous with event:

public delegate void ConsumeCompleteHandler(string message);

public class netConsumer
{
    public string message;

    public KafkaOptions options = new KafkaOptions(new Uri("http://rqdsn0c.bnymellon.net:9092"), new Uri("http://rqdsn0c.bnymellon.net:9092"), new Uri("http://rqdsn0c.bnymellon.net:9092"))
    {
        Log = new ConsoleLog()
    };

    public BrokerRouter router = new BrokerRouter(options);

    public void consume()
    {

        Task.Factory.StartNew(() =>
        {


            var consumer = new Consumer(new ConsumerOptions("TestHarness3", router));

            foreach (var data in consumer.Consume())
            {
                Console.WriteLine("Response: P{0},O{1} : {2}", data.Meta.PartitionId, data.Meta.Offset, data.Value);
                var utf8 = Encoding.UTF8;
                message += utf8.GetString(data.Value, 0, data.Value.Length);
                ExcelWorksheet.writeToExcel(message);
            }

            OnConsumeComplete(message);

        });
    }

    public event ConsumeCompleteHandler ConsumeComplete;

    protected virtual void OnConsumeComplete(string message)
    {
        var handler = ConsumeComplete;
        if (handler != null) handler(message);
    }
}
shf301
  • 31,086
  • 2
  • 52
  • 86
  • Yes, the static method was the problem. The external Consumer is he name of the tlb file I added as a reference. I tried to changed it but it was causing errors so I left it. – QWERTYP5391 Aug 03 '15 at 19:13
  • I will try your way, Thanks – QWERTYP5391 Aug 03 '15 at 19:15
  • @QWERTYP5391 If this solved your problem you should mark it as the answer. I was seeing the same runtime error and removing the `static` from my method solved it - thank you shf301 – Bassie Jan 01 '17 at 14:32
0

Couple of things. In the VBA code Consumer means two things (both the variable and the container of netConsumer); this is going to cause confusion at the least.

What is the external Consumer anyway? (The one that's not the variable.) It looks from the C# code like it should be netConsumer. How are you exporting the class to COM anyway? (Wrong stuff about namespace and class names edited out as per shf301 below.)

Third, since consume() is a static method, why are you creating a netConsumer anyway?

simon at rcl
  • 7,326
  • 1
  • 17
  • 24
  • You are "allowed" to have a class with the same name as it's namespace in that it's valid C# code. It's not not recommended because it will cause a lot of grief: http://stackoverflow.com/questions/18731415/namespace-and-class-with-the-same-name – shf301 Aug 03 '15 at 17:34
  • @shf301 - I was sure I'd had errors doing that, but it seems not. Thanks! I'll edit the answer. – simon at rcl Aug 03 '15 at 17:36
  • Yes, the static method was the problem. The external Consumer is he name of the tlb file I added as a reference. I tried to changed it but it was causing errors so I left it. – QWERTYP5391 Aug 03 '15 at 19:12