Mixing F# with C# in Visual Studio 2010

R

RayLopez99

I don't think it's possible to mix F# and C# together in the same project. Anybody done this?

RL
 
B

Brian Cryer

RayLopez99 said:
I don't think it's possible to mix F# and C# together in the same project.
Anybody done this?

Never used F#, but given that you can mix C# and VB.Net in the same project
(with certain caviats) then I would have though you should also be able to
mix C# and F#.

My notes on mixing C# and VB.Net are here:
http://www.cryer.co.uk/brian/mswinswdev/ms_dotnet_mix_csharp_and_vb.htm and
I would expect the same to apply when mixing C# and F#, but like I said,
I've never used F# so haven't tried.

The caviats when mixing C# and VB.Net (which I'd expect still to apply) are:
1. You cannot mix within a class library - but you can have multiple class
libraries, so one could bein C#, one F# and another in VB.Net.
2. In a desktop application you cannot mix languages.
3. In a web forms project you can have one form in one language and another
in a different languages. Also with web forms you cannot mix languages
inside the App_Code folder, but you can have a separate App_Code folder for
each language. Details in my notes, link given above.

Hope this helps.
 
J

Jeff Johnson

In Visual Studio, a "project" is a single compilation unit. You cannot
mix
different languages in the same project.

He should have said SOLUTION, not project.
 
B

Brian Cryer

Peter Duniho said:
In Visual Studio, a "project" is a single compilation unit. You cannot
mix
different languages in the same project.

Whilst I fully accept that you cannot mix them within the same compilation
unit, you can mix them within the same project (but with caviats). Yes I
mean "project" not "solution". I'm doing it in the project I'm working on
now - Visual studio 2010 Professional.

If you look at my notes (
http://www.cryer.co.uk/brian/mswinswdev/ms_dotnet_mix_csharp_and_vb.htm )
you will see what the caviats are.
Not in the application's own project, no. But it can reference managed
assemblies written in any other language.

Yes, quite correct. I should have said "project" not "application" (which
implied the solution).
Like your first two points, this is just another example of the
fundamental
rule: you cannot mix languages within a project, but any project can
reference managed assemblies written in any language.

Yes you can. I'm doing it now.
Personally I don't think its a good idea, I'm only doing it as part of a
gradual migration from VB.Net to C#, but it can be done.
 
B

Brian Cryer

Jeff Johnson said:
He should have said SOLUTION, not project.

No, I meant PROJECT not solution. You can mix different languages in the
same PROJECT, provided its a web forms project and not an application.
Whilst in general I wouldn't recommend it, I am doing in in some of my
PROJECTs.

If you don't believe me then follow my notes on it and try. See:
http://www.cryer.co.uk/brian/mswinswdev/ms_dotnet_mix_csharp_and_vb.htm
BUT you must follow these notes. If you just add .cs and .vb files without
changing your web.config file and even then keeping the two separate then it
won't work.
 
J

Jeff Johnson

No, I meant PROJECT not solution. You can mix different languages in the
same PROJECT, provided its a web forms project and not an application.

Oh, Web. I don't give a rat's ass about the Web, so that's why I never knew
that. Well, that plus I never would have even considered doing such a thing
in the first place and therefore would have never looked to see if it was
even possible.
 
A

Arne Vajhøj

I don't think it's possible to mix F# and C# together in the same project.

It is possible, but you will need to tweak the build manually as
VS does not support it.

Arne
 
A

Arne Vajhøj

Never used F#, but given that you can mix C# and VB.Net in the same
project (with certain caviats) then I would have though you should also
be able to mix C# and F#.

My notes on mixing C# and VB.Net are here:
http://www.cryer.co.uk/brian/mswinswdev/ms_dotnet_mix_csharp_and_vb.htm
and I would expect the same to apply when mixing C# and F#, but like I
said, I've never used F# so haven't tried.

The caviats when mixing C# and VB.Net (which I'd expect still to apply)
are:
1. You cannot mix within a class library - but you can have multiple
class libraries, so one could bein C#, one F# and another in VB.Net.
2. In a desktop application you cannot mix languages.
3. In a web forms project you can have one form in one language and
another in a different languages. Also with web forms you cannot mix
languages inside the App_Code folder, but you can have a separate
App_Code folder for each language. Details in my notes, link given above.

You can mix C# and VB.NET for whatever application type
you want at the .NET level.

The problem is just that VS can not auto generate the
build (in the more traditional cases - you have so found
out that it does work in some cases).

Arne
 
A

Arne Vajhøj

Like your first two points, this is just another example of the fundamental
rule: you cannot mix languages within a project, but any project can
reference managed assemblies written in any language.

????

It is one project in VS as far as I can see.

Arne
 
A

Arne Vajhøj

Oh, Web. I don't give a rat's ass about the Web, so that's why I never knew
that. Well, that plus I never would have even considered doing such a thing
in the first place and therefore would have never looked to see if it was
even possible.

If you are willing to create a custom build procedure you can
create your console exe from multiple languages as well.

Arne
 
A

Arne Vajhøj

If you are willing to create a custom build procedure you can
create your console exe from multiple languages as well.

C:\Work>type mlfs.fs
namespace global

open System
open System.Reflection

type FS() =
static member Test() =
Console.WriteLine("F# says hello from " +
Assembly.GetExecutingAssembly().Location)
C:\Work>fsc --target:module mlfs.fs
Microsoft (R) F# 2.0 Compiler build 4.0.30319.1
Copyright (c) Microsoft Corporation. All Rights Reserved.

C:\Work>type mlvb.vb
Imports System
Imports System.Reflection

Public Class VB
Public Shared Sub Test()
Console.WriteLine("VB.NET says hello from " &
Assembly.GetExecutingAssembly().Location)
End Sub
End Class

C:\Work>vbc /t:module mlvb.vb
Microsoft (R) Visual Basic Compiler version 11.0.50709.17929
Copyright (c) Microsoft Corporation. All rights reserved.


C:\Work>type mlcs.cs
using System;
using System.Reflection;

public class CS
{
public static void Test()
{
Console.WriteLine("C# says hello from " +
Assembly.GetExecutingAssembly().Location);
}
public static void Main()
{
FS.Test();
VB.Test();
CS.Test();
}
}

C:\Work>csc /t:module /addmodule:mlvb.netmodule
/addmodule:mlfs.netmodule mlcs.cs
Microsoft (R) Visual C# Compiler version 4.0.30319.17929
for Microsoft (R) .NET Framework 4.5
Copyright (C) Microsoft Corporation. All rights reserved.


C:\Work>link /LTCG /out:mltest.exe /entry:CS.Main /subsystem:console
mlcs.netmodule mlvb.netmodule mlfs.netmodule
Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation. All rights reserved.

Generating code
Finished generating code

C:\Work>mltest
F# says hello from C:\Work\mltest.exe
VB.NET says hello from C:\Work\mltest.exe
C# says hello from C:\Work\mltest.exe

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top