Fog Creek Software
Discussion Board

Welcome! and rules

Joel on Software

C# and VB.NET in same project

I've been using C# for a while now but I haven't really felt the need to look into VB.NET. A colleague is currently writing a project in VB.NET and wishes to intergrate one of my C# modules:

Is it possible to do this without having to first compile the C# module (a single .cs file) into a seperate DLL? I thought that part of the hype about .NET was the fact that you could easiliy combine 2 or more languages for a single project.

Thanks in advance


Marcus Greenwood
Tuesday, October 8, 2002

With the .NET toolset you can, but VS.NET doesn't support it directly.

What you want to do is build what's called a multi-module assembly. Each module can be written in whatever language you want, then you link them all together into a single assembly.

Unfortunately, VS.NET only supports single module assemblies, so if you want to go this route you'll need to use the .NET command line tools.

Chris Tavares
Tuesday, October 8, 2002

Yeah, what he said... (Damn it! somebody always beats me to the punch!).

Here's a crappy sample to go along with what Chris outlined:

Imagine the file "Dog.cs" is the file that you created in C# that your coworker needs to integrate into his VB project.

--- Dog.cs ---

public enum DogSize

public class Dog
    public Dog(string name, DogSize size)
        m_name = name;
        m_size = size;

    public string Name
        get { return m_name; }

    public string Bark()
        switch (m_size)
            case DogSize.Small:
                return "Yip";
            case DogSize.Medium:
                return "Bark";
            case DogSize.Large:
                return "Woof";
                throw new InvalidOperationException("The dog is sleeping.");

    private string  m_name;
    private DogSize  m_size;

-- Dog.cs --

Compile "Dog.cs" into a module using the following command line syntax:

csc /target:module Dog.cs

Next have your coworker compile his "DogPound.vb" program (written in VB.NET) with a special command line option to include your "Dog.netmodule" Module

vbc /out:DogPound.exe /target:exe /addmodule:Dog.netmodule DogPound.vb

-- DogPound.vb --

Imports System
Imports System.Collections

Module ConsoleMain

    Sub Main()

        Dim dogs As ArrayList = New ArrayList()
        dogs.Add(New Dog("Pickles", DogSize.Small))
        dogs.Add(New Dog("Shilo", DogSize.Medium))
        dogs.Add(New Dog("Dylan", DogSize.Large))

        Dim dog As Dog
        For Each dog In dogs
            Console.WriteLine("{0} says {1}", dog.Name, dog.Bark())
        Next dog
    End Sub

End Module

-- DogPound.vb --

The result is an Assembly (DogPound.exe) with 2 modules (Dog.netmodule and DogPound.exe).  Check it out for yourself by browsing the metadata in ILDASM.exe!

In the example above, you'd probably be better of creating a library for the Dog type and packaging it up in it's own Assembly.  That's what I think Microsoft is refering to by being able to use multiple languages in a project.  For example, your data services could be written by one team in C# and the user interface could be written by another team using VB.NET (or COBOL.NET if you please).

Guy Incognito
Tuesday, October 8, 2002

I see what you mean. Shame that VS.NET doesn't support multi-module assembly. Perhaps it'll be something that they add in the future.

Thankyou both for your help and time.



Marcus Greenwood
Wednesday, October 9, 2002

*  Recent Topics

*  Fog Creek Home