简体   繁体   中英

Calling C# code from VB.net code

This is my c# code in some Dll

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;


namespace LP_Misc
{
    public class LP_Registery
    {
        LP_Registery()
        {
            ReadMyTestRegKey();
        }
        public void ReadMyTestRegKey()
        {

            RegistryKey regkey;/* new Microsoft.Win32 Registry Key */

            regkey = Registry.CurrentUser.OpenSubKey(@"Software\PCBMatrix\LPWizard\experimental");


            string[] valnames = regkey.GetValueNames();

            string val0 = (string)regkey.GetValue(valnames[0]);

            Console.Write("--------------------------------The {0} val is {1}", valnames[0], val0);

        }

    }
}

I am trying to call it from some vb code in another dll .

Like this

Imports LP_Misc
.
.
.
Dim T As LP_Registery()

I don't get any errors but it just not enters to the C# code . It just jumps over it and goes over .

any idea ?

You didn't instantiate your object. You need this:

Dim T As New LP_Registery()

However, your constructor is private, so that won't work either. You need this in your object:

public LP_Registery()
{
    ReadMyTestRegKey();
}

Your constructor is not declared as public, so by default in C# it is private:

    LP_Registery()
    {
        ReadMyTestRegKey();
    }

Change it to:

    public LP_Registery()
    {
        ReadMyTestRegKey();
    }

You are also not calling the constructor in your VB.NET code.

T = new LP_Registery()

Without the New keyword, your VB code hasn't executed anything in the C# dll. Make this your last line of VB code:

Dim T As New LP_Registry()

This line in your code:

Dim T As LP_Registery()

...declares T to be an array of type LP_Registery but does not actually create any instance of it. You probably want to change it to this:

Dim T As New LP_Registery()

You may have to add the csproject into the same solution as your vbproject is. Make sure that in your vbproj, you are referencing the csproj by reference.

Dont you need

Dim t as LP_Registery

t = new LP_Registery

?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM