1

I have a COM object that works fine in VB.NET, but not in C#. Both projects are .NET 4 console applications.

The COM object loads in C#, but the methods don't return any values. Why would it work in VB.NET and not C#?

Thanks!

Sub Main()
    Dim server As New NoahVersionLib.Version

    Dim val As Int32

    server.GetNoahServerVersionMS(val)



End Sub


    static void Main(string[] args)
    {
        var server = new NoahVersionLib.Version();

        int val= 0;

        server.GetNoahServerVersionMS(ref val);
    }

val is 0 in the C# build, but has a value in the VB.NET build.

UPDATE:
I needed to put [STAThread] on my Main() in C#. It works now.

7
  • 6
    Code? Or are we guessing..? Commented May 10, 2011 at 13:47
  • 3
    please show a minimal but working (i.e. compiling) code sample illustrating the problem in both environmnents. tinyurl.com/so-hints Commented May 10, 2011 at 13:50
  • 5
    There's no special reason why it would only work in VB.NET and not in C#. They compile down to the same code. The only way that we can answer this question is if you provide us with a minimal sample that reproduces the behavior. Chances are, there's an error in how you're converting the VB.NET code into C#. Commented May 10, 2011 at 13:57
  • Have a look at both exe files in ildasm. Do you see a difference in the generated IL code? If yes, what is it? Commented May 10, 2011 at 14:14
  • also, it makes on difference in the C# build if var is used instead of NoadVersionLib.Version. Commented May 10, 2011 at 14:15

2 Answers 2

1

I needed to put [STAThread] on my Main() in C#. It works now.

Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried the following in C#?

static void Main(string[] args)    
{        
    NoahVersionLib.Version server = new NoahVersionLib.Version();        
    Int32 val= 0;        
    server.GetNoahServerVersionMS(ref val);    
}

The only differences between your 2 versions are that you are using var instead of declaring the actual type, allowing the compiler to potentially infer an incorrect type (unlikely, but surely possible); and use of int instead of Int32. As far as I know, int == Int32, but possibly in some weird corner case of COM, it may not..?

Comments

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.