Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

Utilize VB.NET in C# Project

1.75/5 (3 votes)
28 Oct 2013CPOL1 min read 15.8K  
How to utilize VB.NET in C# project

Introduction

As some of you may know, I love C# and obviously the .NET Framework; however, I recently took on a new job doing VB.NET programming. I can honestly say I like VB.NET, or for that matter most programming languages that serve their purpose. Most server side programming projects I have been working on are strictly C#, but I want to practice VB.NET, while still working towards something that I can actually use (e.g., not programming strictly just for exercise but exercise and purpose to follow the 80/20 rule). In this brief post, we will efficiently go through how we can use VB.NET in a C# project.

DLLs Baby

The solution is rather simple: Create a VB.NET class library and class, add business logic to the class, and add a reference in our C# project to the VB.NET class library’s DLL to utilize it.

Simple VB.NET interface and implementation to get the ball rolling:

VB.NET
Namespace Brandrick.Services.BrandrickNetCSSEditor

    Public Interface IBrandrickNetCSSEditor
        'Update CSS Colors and return resource location
        Function UpdateColor(ByVal Red As Integer, _
        ByVal Green As Integer, ByVal Blue As Integer) As String

    End Interface

    Public Class BrandrickNetCSSEditor
        Implements IBrandrickNetCSSEditor

        Public Function UpdateColor(Red As Integer, Green As Integer, _
        Blue As Integer) As String Implements IBrandrickNetCSSEditor.UpdateColor
            'ToDo: Business Logic
            Return "CSS Resource"
        End Function
    End Class

End Namespace

Add a DLL reference from the VB.NET project to the C# project:

Add Reference

Utilize VB.NET code in C#:

C#
using Brandrick.Services;
using Brandrick.Services.VB.Brandrick.Services.BrandrickNetCSSEditor;
using System.Web;
using System.Web.Mvc;
namespace Brandrick.Controllers
{
    public class ThemeController : Controller
    {
        public ActionResult CreateCustomTheme(int red, int green, int blue)
        {
            IBrandrickNetCSSEditor bNetCSSEditor = new BrandrickNetCSSEditor();
            var resource = bNetCSSEditor.UpdateColor(red, green, blue);
            ViewBag.resource = resource;

            return View();
        }
    }
}

Conclusion

While the code is not complete in these examples, you see it is simple to utilize both VB.NET and C# in the same solution, which provides the option to pick your .NET programming language flavor within the same solution.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)