-1

I want to be able to use this COM class as if it were a static class, and call methods / functions statically from a VB6 application. Here is my current class:

using System;
using Linq;
using System.Runtime.InteropServices; 

namespace VNDBUtils
{
    [ComVisible(true)]
    public static class BracketString(){

      [ComVisible(true)]
      public static string HellowWorld(){

          get{
            return "Hello World";
          } 
      }
   }
}

This is the way I am currently calling the class in VB6, I am creating an object of the class and calling it this way. However now that the class is static I am unsure as to the exact syntax to use.

I originally thought it could be called using the following syntax:

Dim test As VNDBUtils
test.BracketString.HelloWorld(); 

However this is giving me an error, so if someone could give me an example of how I can call this class statically from a Visual Basic 2006 application. Thanks!

dantheman
  • 273
  • 1
  • 3
  • 14
  • You cannot store a namespace name in a variable. You must either use `Imports` or write it out like VNDBUtils.BrackString.HelloWorld() – Hans Passant Apr 16 '15 at 13:43
  • @HansPassant thank you but this gives me the following error: Method or data member not found. For some reason it can't see my BracketString method? – dantheman Apr 16 '15 at 13:47
  • You'll have to post real code instead of this nonsense snippet to get a decent guess at the underlying problem. – Hans Passant Apr 16 '15 at 13:50
  • @HansPassant. I used the exact code you suggested, "VNDBUtils.BracketString.HelloWorld(). Gave me the error I stated above. – dantheman Apr 16 '15 at 13:54
  • 1
    You are not using the exact code you posted, it does not compile. So I cannot possibly guess what it really looks like. It is not even clear what the point of using [ComVisible(true)] could possibly be. – Hans Passant Apr 16 '15 at 13:56
  • @HansPassant, I am referencing the class created above (as a .tlb) file. I then want to use the class statically in my vb6 code. Your code does not compile. – dantheman Apr 16 '15 at 13:58
  • What do you mean? This is the exact code I am using, as of now : Dim test as String test = VNDBUtils.BracketString.HelloWorld() MsgBox(test) – dantheman Apr 16 '15 at 13:59
  • 1
    That is not possible, COM must be able to create an instance of the class. So it cannot be a *static class*. – Hans Passant Apr 16 '15 at 14:00
  • That is what my entire question is about, IF IT WAS POSSIBLE, AND IF SO HOW. Therefore I cannot have a static COM class? The class must be instantiated ? – dantheman Apr 16 '15 at 14:01
  • Does this answer your question? [Can I call a static method of a C# class from VBA via COM?](https://stackoverflow.com/questions/24193183/can-i-call-a-static-method-of-a-c-sharp-class-from-vba-via-com) – hollen9 Oct 12 '20 at 03:33

1 Answers1

0

Use the static keyword:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(BrackString.HelloWorld());
            Console.Read();
        }
    }

    public static class BrackString
    {
      public static string HelloWorld()
      {
            return "Hello World" ;
      }
   }
}

Without using static, you would have to create a new object of class BrackString as follows:

BrackString brackString = new BrackString();
Console.WriteLine(BrackString.HelloWorld());

So it's not necessary to "Dim VNDBUtils", the point of the static method is that it is available without instantiation. Note class names don't take parentheses, and you need a semicolon after program lines!

08Dc91wk
  • 4,254
  • 8
  • 34
  • 67
  • sorry I did have static in my class name and string, i just forgot to write that in my example above. Should I only have the string as static ? – dantheman Apr 16 '15 at 13:32
  • Yes, the string should be static. I have updated my answer with the static method inside a console application that I've tested to be working. – 08Dc91wk Apr 16 '15 at 13:34
  • Therefore my class does not need to be static? Because this is a dll i have created and I will be calling the method from a vb6 program. – dantheman Apr 16 '15 at 13:38
  • Static methods and classes are always available without instantiation. So you don't strictly need to make the class static, but if it has no non-static members inside it then it makes sense to do so. – 08Dc91wk Apr 16 '15 at 13:42
  • Alright thanks, so it doesn't necessarily matter if i declare the class static or not. Either way I don't want to instantiate it. However the code you suggested above : BrackString.HelloWorld() doesn't seem to be working in vb6. – dantheman Apr 16 '15 at 13:45
  • 1
    Sorry, I saw you tagged it with c# so I thought it was written in c#. I will update it to work in VB6 but I suggest you remove that tag so future users aren't confused! – 08Dc91wk Apr 16 '15 at 13:52
  • Well the class is written using C# syntax. But I will remove the tag. – dantheman Apr 16 '15 at 13:53
  • OK I understand now, you are writing a static C# class that you want to call using VB6. That explains the "Dim". – 08Dc91wk Apr 16 '15 at 14:04
  • Yes, exactly, sorry if my question seemed unclear. The C# class is a COM. – dantheman Apr 16 '15 at 14:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/75409/discussion-between-ivan-and-dantheman). – 08Dc91wk Apr 16 '15 at 14:07