3

I have a function written in vb

Public Function abc (ByVal x as Integer, ByVal y as String) As String 

End Function

I want to call this function on click of a button in C# applcation.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
CuriousMind
  • 118
  • 1
  • 1
  • 10

3 Answers3

2

If your VB function is inside a module, you need to call it by specifying the module name.

string result = MyVbModule.abc(1, "Hello");

You may also need to specify the namespace, wich per default is the name of the VB project:

string result = MyDll.MyVbModule.abc(1, "Hello");
awe
  • 21,938
  • 6
  • 78
  • 91
  • Hi, My VB Dll name is MyDll. I have a module in this Module MyDll. And the function is written in this module. Now I want to call this function from C# application. Should Module have any public specifier? – CuriousMind Feb 17 '12 at 10:57
  • You will probably get a hint of what the structure is by the intellisense in the C# code if you just starts to write `MyDll.` and see what pops up. – awe Feb 17 '12 at 11:00
  • Hi All, Thanks for your support. I am able to call the function. What I did : I Created the Module as Public and then I added the reference of the vb dll. Then called the function like MyDll.MyDll.abc(1, "Hello"). Thank you all... – CuriousMind Feb 17 '12 at 11:22
  • Hi, How were you able to do it? If the VB6 function is inside a module rather than a class module, then .NET intellisense does not even show it! – AllSolutions May 25 '16 at 23:42
1

Using this post I was able to just call a VB function inside a simple Class module. No VB.dll needed.

I needed to use some Date-math functions written in VB that where huge and almost impossible to rewrite. I had to make my comment an answer so it would fit and be easily understood.

I made a VB Project inside my C# Solution next to the C# project. I added a reference in the C# project to the VB one.

I looked in Object Browser to see how it was referenced. Name of the Project was referenced, twice: "VB_Stuff.VB_Stuff".

In C# Razor Page I put a "using VB_Stuff.VB_Stuff" at the top.

In the Razor Page OnGetAsync where I needed the VB function(s) I declared an object with the name of the VB Class: DateStuff obj = new DateStuff();

Then I called the function (named fTest) with my Model's field . . .

DateStuff obj = new DateStuff();
Dances_Moderated.RecurYesNo =obj.fTest(Dances_Moderated.RecurYesNo.ToString());

I did this right before my Razor Page was going to load and display the Details Page for the "Dances_Moderated" Model.

No .dlls! Just some VB code in a VB class in a VB Project inside a C# Solution. Make sure you right click on the solution to add the project, otherwise from the File menu "New Project" it wants to add a new Solution no matter what you do.

Here is the simple VB Class file with the simple Test function.

Namespace VB_Stuff

    Public Class DateStuff

        Public Function fTest(ByVal Recurring As String) As String

            fTest = Recurring & "xxxxx"

        End Function

    End Class

End Namespace
JustJohn
  • 1,362
  • 2
  • 22
  • 44
1
  1. Add a reference to the assembly containing the code.
  2. Create an object of the class containing the method (function) you want to call
  3. Call the method objectname.abc(3,"SomeSting");
Mithrandir
  • 24,869
  • 6
  • 50
  • 66
  • Hi, My VB Dll name is MyDll. I have a module in this Module MyDll. And the function is written in this module. Now I wan to call this function from C# application. – CuriousMind Feb 17 '12 at 10:54