dll with both C# and VB

  • Thread starter Thread starter tshad
  • Start date Start date
T

tshad

I have a VS 2008 project that is going to create a Class Library.

Can I use both C# and VB files to create this?

I have 3 classes that are in C# and 4 that are in VB and would like to put
them in the same library.

If not, I will create 2. But if I create 2 .dlls can they use the same
namespace?

Thanks,

Tom
 
re:
!> Can I use both C# and VB files to create this?

Yes.

re:
!> I have 3 classes that are in C# and 4 that are in VB and would like to put them in the same library.

No problem.

You can add subdirectories to the App_Code directory
in order to process multiple languages under the App_Code directory.

In order to do this, you need to register each subdirectory in the
<codeSubDirectories> element of the application's Web.config.

<configuration>
<system.web>
<compilation>
<codeSubDirectories>
<add directoryName="VB"/>
<add directoryName="CS"/>
</codeSubDirectories>
</compilation>
</system.web>
</configuration>

Then, simply creating the App_Code\VB and App_Code\CS directories in your application's root,
and placing your VB and CS files in each, will allow you to use both languages in your app.

You can use any name you want for your subdirectories,
as long as you register the correct name in web.config.

This works in ASP.NET 2.0 and later versions!

Note: this only works for class files, and not for code-behind, OK ?

re:
!> can they use the same namespace?

I've never tried it, but that shouldn't be a problem.

The files in each directory will wind up in a different dll depending on your compilation model,
but since ASP.NET 2.0 and later use partial classes there shouldn't be a problem.

An alternative way to do this would be to compile your different-language source files from
the command-line, and placing the compiled assemblies in the application's /bin directory.

In both cases, you'll have to import the desired namespaces into your aspx files.




Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
 
Juan T. Llibre said:
re:
!> Can I use both C# and VB files to create this?

Yes.

re:
!> I have 3 classes that are in C# and 4 that are in VB and would like to
put them in the same library.

No problem.

You can add subdirectories to the App_Code directory
in order to process multiple languages under the App_Code directory.

Do the folders need to be in the App_Code folder?
In order to do this, you need to register each subdirectory in the
<codeSubDirectories> element of the application's Web.config.

This isn't a Web Site project but a Class Library. There is no web.config.

How would I do it with that type of project?

Thanks,

Tom
 
re:
!> Do the folders need to be in the App_Code folder?

Yes, and the pointer to the folders needs to be in web.config.

re:
!> This isn't a Web Site project but a Class Library.

That's immaterial. It's still an ASP.NET Application.

re:
!> There is no web.config.

You'll have to create one, then, if you want to use more than one language.

Just create it, even though Web Site Projects don't create one by default.
Web Site Projects can use web.config files, too.

re:
!> How would I do it with that type of project?

The same way I explained in my previous post.




Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
 
You can't create a single dll that is coded in different languages.
Either create two dll's or convert the source code to a single language.
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
VB & C# to Java
Java to VB & C#
Instant C#: VB to C#
Instant VB: C# to VB
Instant C++: VB, C#, or Java to C++/CLI
 
David said:
You can't create a single dll that is coded in different languages.
Either create two dll's or convert the source code to a single
language.
Now I am confused.

What about what Juan said about the App_Code folders and using it to created
one dll with different languages?

Thanks,

Tom
 
yes you can, but visual studio does not have builtin support for two
languages in one project, so you will need to edit the project file to
do it. you need to compile the source with the correct compiler, then
use al.exe to build the assembly. you could also to two projects and
combine as a separate step.

unless you need one dll, the simple answer is to create two projects
using the same namespace (or just convert the vb code c#, probably less
work than editing the project file).

-- bruce (sqlwork.com)
 
Juan said:
re:
!> Do the folders need to be in the App_Code folder?

Yes, and the pointer to the folders needs to be in web.config.

re:
!> This isn't a Web Site project but a Class Library.

That's immaterial. It's still an ASP.NET Application.

re:
!> There is no web.config.

You'll have to create one, then, if you want to use more than one
language.
Just create it, even though Web Site Projects don't create one by
default. Web Site Projects can use web.config files, too.

re:
!> How would I do it with that type of project?

The same way I explained in my previous post.
I still can't seem to get my Class Library to work. No matter what I do, I
can't even get my C# programs to work.

I did what you suggested and added the App_Code folder with 2 folders (VB
and CS) below that and then putting my 2 vb files in the VB foldler and my 2
cs files in the CS folder.

It only compiled the VB files.

When I Updated my reference to this dll it showed the 2 classes from the 2
vb files but not the ones from the cs files.

It didn't matter where I put the vb files they got compiled and no matter
where I put the cs files they didn't.

I added the web.config file 1st as:

<configuration>
<system.web>
<compilation>
<codeSubDirectories>
<add directoryName="VB"/>
<add directoryName="CS"/>
</codeSubDirectories>
</compilation>
</system.web>
</configuration>

This didn't help. I then changed it by adding a defaultLanguage entry:

<configuration>
<system.web>
<compilation defaultLanguage="C#">
<codeSubDirectories>
<add directoryName="VB"/>
<add directoryName="CS"/>
</codeSubDirectories>
</compilation>
</system.web>
</configuration>

But still the cs files didn't compile but the vb files did.

Thanks,

Tom
 
It does seem to make a difference.

I finally got it to work in my Web Site Application doing what you suggested
below. But it doesn't work in my Class Library as others have suggested. I
took out the reference to the Class Library (which I will deal with later)
and then set up the folders and web.config as you suggested and now it
works fine.

As I mentioned, it only compiled by VB files in my Class library and never
did compile my C# files.

Not sure if the web.config does anything for a Class Library. Doesn't seem
to affect anything. It didn't matter where I put the VB files in the root,
another folder or App_Code - it always compiled. It wouldn't compile my C#
files no matter where I put them. I assume that I have to tell project what
Language I am using - not sure where this is yet.

Thanks,

Tom
 
re:
!> What about what Juan said about the App_Code folders
!> and using it to created one dll with different languages?

I didn't say that, at least in reference to MSIL.

I said you can use both languages and that you can use the
same namespace even though you're using using both languages.

In sum :

1. You will wind up with different dlls for vb and c#.
2. You can use the same namespace in both
3. The MSIL in both dlls will be merged into the page's dll in the temp files directory.





Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
 
re:
!> That isn't a dll though...

If you use code subdirectories for VB and C#...

1. You will wind up with different dlls for vb and c#.
2. You can use the same namespace in both
3. The MSIL in both dlls will be merged into the page's dll in the temp files directory.



Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
 
Back
Top