Solved

How To Pass a String Between vb.net and a C sharp DLL

Posted on 2011-02-27
6
596 Views
Last Modified: 2012-05-11
Hello Experts,
I am new to C sharp, I need to pass a string parameter from my vb.net project to a  c # dll . I have a code in c# application and need to convert to dll that can be used in my code.
I know it is a problem since  strings are handeled diffrently by both languages plus I have never written a dll in c#. can anyone make it clearer? asample code would be great.
Thanks.  
0
Comment
Question by:Hagita
6 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34991601
It isn't a problem, and the two languages don't handle strings differently. C# and VB.Net are both CLR languages and therefore work the same way. You can pass a string from VB.Net to C# in exactly the same way as you would normally.
0
 
LVL 11

Expert Comment

by:srikanthreddyn143
ID: 34991679
As Carl_tawn said, Just if you refer your dll to your VB.Net project, create an object for the class and use it.

Ex:

C# Project:

Public class CSharpTest
{
Public string HelloWorld(string psTest)
{
   return psTest;
}
}
Make this as dll

Add this dll as reference to your VB.Net project

In you vb.net code
Dim objCSharpTest As New CSharpTest
Dim sRetValue As String = objCSharpTest.HelloWorld("Accessed CSharp dll")

SRetValue should have string you passed.
0
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 34991985
Hi Hagita;

You can download this small solution which implements a C# dll in a VB .Net project to use as a template  https://filedb.experts-exchange.com/incoming/ee-stuff/8063-MySolution.zip

Fernando
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:Hagita
ID: 34994727
Fernando, Thank you so much for the code solution. I will use it and see if no problem arise.
Thaks alot!
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 34996513
Not a problem, glad to be of help.  ;=)
0
 
LVL 5

Expert Comment

by:andr_gin
ID: 34996739
A C# and a VB.NET dll are the same except for debugging information

When mixing VB.NET and C# code you should take following things into consideration:

1) VB.NET supports optional parameters, but C# does not (except C# 4.0). If you want to call a VB.NET method from C# you must provide all parameters and must know the default values. If you use them excessively this can be a pain.

2) C# is case sensitive, but VB.NET is not. Avoid any situations where you have to members with the same name, but only different casing. On the other side if you call VB.NET functions with strange casing rules from C# it can also be a problem. If you change casing in VB.NET code this will not break anything. In the C# code accessing it, it will create syntax errors or in some situations create other meaning. Example:

VB.NET
Dim openFileDialog as New OpenFileDialog
OpenFileDialog.Show() 'Accesses the instance members of the class.

C#:
OpenFileDialog openFileDialog = new OpenFileDialog();
OpenFileDialog.Show(); //Accesses the shared members of the class => syntax error

3.) In VB.NET you can access shared members of a class through and instance. If C# you cant do that. This does not break code, but it can have strange effects when translating code.

4.) C# has static classes. VB.NET only has Modules, but this is not the same. Maybe you can run into trouble here.

5) For debugging a C# dll behaves like a VB.NET dll if you use Visual Studio Standard or higher. You can debug your C# dll line by line directly from your VB.NET project like you would do with a VB.NET dll, can edit the code with limited syntax highlighting, but for recompiling it you have to open your C# dll project.

6) If you use the Express edition you have to install both editions and must choose which version you use to open your solution.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now