Utilize VB.NET Project in C# Project

Robbie Brandrick | 10/20/2013 7:54:40 AM

Introduction

As some 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:

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#:

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.