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.

1
  • Doesn't look like C to me... Commented Aug 3, 2015 at 17:16

2 Answers 2

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);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

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.
I will try your way, Thanks
@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
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?

3 Comments

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: stackoverflow.com/questions/18731415/…
@shf301 - I was sure I'd had errors doing that, but it seems not. Thanks! I'll edit the answer.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.