Solved

Call a VB routine from C#

Posted on 2008-06-10
13
775 Views
Last Modified: 2013-12-17
Hi,

I have this file in VB (.vb) that has a routine I must call from another file in C#.

I have no idea on how to do that.

Any help would be appreciated.

Warm Regards,
Mário Gamito
0
Comment
Question by:angelsofgrace2006
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 3
13 Comments
 
LVL 9

Expert Comment

by:Rob Siklos
ID: 21751666
Compile the VB program into a library (DLL).  

In your C# project, add a reference to that DLL.

In your C# code, add a "using" statement for the namespace in which your routine is defined.
0
 

Author Comment

by:angelsofgrace2006
ID: 21751719
Dear rob263,

How do I compile the .vb file to a DLL ?

How do I add a reference to it in my C# file ?

How do I identify the namespace of the routine ?

Where do I put the created DLL (C:\Windows\system32) ?

Sorry for the lame questions, but I'm really a newbie at C#

Warm Regards,
Mário Gamito
0
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 21751789
Is this .vb file vb6 or vb.net?
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 9

Accepted Solution

by:
Rob Siklos earned 250 total points
ID: 21751860
In your VB source, check to see if your routine is inside a "Namespace / End Namespace" scope.  If not, add one around the code (see code snippet below)

In your VB project,  go to the "Project" menu, and then "<Project Name>  Properties".  Make sure the Application Type is set to "Class Library".

Compile the project, and you will find the DLL in the bin/Debug or bin/Release folder underneath your main project folder.

Take the DLL (and any other DLLs in that folder) and copy them to the "bin" folder of your C# project.

In your C# project, go to the "Project" menu, and click "Add Reference".  Go to the "Browse" tab, and navigate to the "bin" folder of your c# project.  Select all DLL files and add them as references.

In your c# code file, add something like "using MyVbLibrary;"
Namespace MyVbLibrary
 
  Public Module MyModule
 
    Public Sub DoSomething()
      ' code goes here
    End Sub
 
  End Module
 
End Namespace

Open in new window

0
 

Author Comment

by:angelsofgrace2006
ID: 21752059
Dear rob263,

I can't find any "Project" menu or any other thing that has what you mention.

I'm using Visual Studio 2008.

BTW, do I have to compile the whole website as a DLL or only the file that has the routine I want to call from C# ?

Warm Regards,
Mário Gamito
0
 
LVL 9

Expert Comment

by:Rob Siklos
ID: 21752096
Oh - you didn't mention that it was a web site.

In that case, you should create a new VB "Class Library" project, and move the code which you want to be shared into there.
0
 

Author Comment

by:angelsofgrace2006
ID: 21752128
Dear rob263,

Move the code to where ?

Does the rest still applies ?

Warm Regards,
Mário Gamito
0
 
LVL 9

Expert Comment

by:Rob Siklos
ID: 21752167
Dude, if you don't know how to make a new VB project in VS 2008, you're in trouble.

- Open VS 2008.
- Create a new project
- Choose VB.NET as the Language
- Choose Class Library as the project type
- Take the code which has the sub-routine you want to call from C#, and move (or copy) it to your new project.
- compile the project, and copy the DLL as described above.
0
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 21752224
It might be simpler if you just post the routing and let us translate it to C#...
0
 
LVL 9

Expert Comment

by:Rob Siklos
ID: 21752262
jcoehoorn:

FYI, I think .vb files are only for .NET.  VB6 uses .frm, .mdl, .cls, etc..
0
 

Author Comment

by:angelsofgrace2006
ID: 21752521
Hi,

I know how to make a new project in any language.
That's not the problem. The problem is how to invoke a VB function from C#, being them in separate projects/websites.

Site 1 is located at C:\site1 in C# running on port 80.
Site 2 is located at C:\site2 in VB running on port 8020.

All that I need is from Site 1 to access a tiny function in a .vb file in Site 2.

Regards,
Mário Gamito
0
 
LVL 18

Expert Comment

by:jcoehoorn
ID: 21752549
You need to put the vb function in a class library project, not in a web site.
0
 

Author Comment

by:angelsofgrace2006
ID: 21752594
Just one more thing: the author of Site 2 says that I can only empty the cache (by calling the function) if Site 1 is within the same folder as Site 2.

Mário Gamito
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PowerShell:  Where-Object not working as a filter 2 93
Upgrade code from VS 2010 to VS 2015 7 45
How to force output to ascii 2 57
What are MicroServices? 4 65
IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

736 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question