• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

VB and C# files in web application

Is it possible to have two web pages developed in VB.NET and C# respectively in the same web application?

Is this possible using web site types of solutions?
0
Sudheer_Keesara
Asked:
Sudheer_Keesara
  • 12
  • 6
  • 5
1 Solution
 
ddayx10Commented:
When dealing with pages its not a problem at all. You can make one page in vb.net and another in C# and run them under the same site. I do it all the time when mocking things up for answers here.

If you want to mix them up in the app_code folder though that is not quite as straight forward, but it can be done:

http://pietschsoft.com/post/2006/03/ASPNET-20-Use-VBNET-and-C-within-the-App_Code-folder.aspx

And finally if you compile code into .dll's for example and put them in the bin folder it doesn't matter what code they were originally created in.

Hope that helps.
0
 
Sudheer_KeesaraAuthor Commented:
That was fast. Thank you.

Ok. So what if I open the solution which has one C# code behind file and one VB.NET. Will it be able to debug the application?
0
 
ddayx10Commented:
I should add that I don't think its a good idea and many complications can arise if you make pages in different code then want them to inter-communicate in some complex way (.ascx talking to its parent that is a different type of code).

Previously I was only discussing what is possible, not what I think is a good practice. In practice I would definitely only use one or the other per site.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
ddayx10Commented:
Yes you can debug, but typically when you debug on a site you are only debugging from one page at a time so "debugging" accross languages shouldn't often even come up. I can tell you I haven't run into it but I wouldn't be surprised if you had some situation where you did try to debug from a page written in vb.net to a page written in C# that it would fail miserably.

Again, possible and if you have a strong reason to do it in a limited way fine go for it, but I wouldn't recommend it as a strategy.
0
 
Sudheer_KeesaraAuthor Commented:
Ya I do understand that. This is more of a case where I am trying to merge an app with one pgae ( many classes which are in C#) to an app which is in VB.NET and as you can guess I don't have time convert the one to another.

From a strategy point of view, this as you said is not the one to be recommended. Thanks a lot , I will take a closer look at the information you provided and will play around it.

I will also award you the points ofcourse.I appreciate your quick responses.

-Sudheer
0
 
ddayx10Commented:
Ok well when you ask me questions like that last one I get curious.

So I set up 2 pages one .cs and one .vb. I setup 2 class files as per same. I setup the webconfig and app_code per sample I gave you.

I can call and debug through both languages both in pages and class files without any problem. So YES you can debug flawlessly.
0
 
ddayx10Commented:
Yes I figured you were merging a project and it likely wasn't your 1st choice, but frankly I am never surprised at the crazy things beginners try to do for the most unfathomable reasons. :)
0
 
käµfm³d 👽Commented:
To be clear: this works for web sites via the App_Code special folder; for web applications, the code must be put into different assemblies (i.e. projects) if you are going to mix languages.
0
 
Sudheer_KeesaraAuthor Commented:
So that means I can't debug for web applications using the same project. Correct? Not sure IF I can do that by referencing the library too? I am assuming that you have used web site for the above example you have tried. I am not sure How it would work using web apps though.(from a debugging prospective)

0
 
ddayx10Commented:
hey @kaufmed, you still up? Well him did say "web application" didnt he. He can always call me stupid later. Wouldn't be the 1st time.

0
 
käµfm³d 👽Commented:
So that means I can't debug for web applications using the same project. Correct?
You can, if the code of your project is written in one language and you have the other language in its own assembly/project (think DLL). If you have the assembly and its corresponding PDB file, then you should be able to debug it. What I am saying is that you can't create a web application (one project) and have one page be in C# and one page be in VB--you have to choose one or the other. If you've already created a page in another language, compiled the associated code-behind into a DLL, and then added the page and a reference to that DLL, I believe you can debug the page. I have to side with ddayx10 on this one, though: I've never had a reason to do so. For the debug part, I am speculating a bit. I do know that you can't do something like "Add New Item->VB code file" if you initially set up the project to be C#. Again, that is for a web application. Web sites work a tad different with respect to mixing code.
0
 
käµfm³d 👽Commented:
Well him did say "web application" didnt he.
Actually, he said both:

Is it possible to have two web pages developed in VB.NET and C# respectively in the same web application?

--and--

Is this possible using web site types of solutions?

= D
0
 
Sudheer_KeesaraAuthor Commented:
Yes. That's true. I needed to understand the issues in both types of apps. Your explanation on having pdb file makes sense but I have to try and see how it works though.
This has been a great discussion for me so far. I must say thanks to both of you.
0
 
ddayx10Commented:
Well I can tell you the same is still true for pages. You can mix cs and vb pages in a web application although I never tried it before 5 mnutes ago. Still debugs fine.

App_Code all becomes moot at that point.

You can add more than one project and reference into them, still debug I'm 99% sure. I'll see if I can find a sample. Still possible just done differently.
0
 
käµfm³d 👽Commented:
@ddayx10
You can mix cs and vb pages in a web application although I never tried it before 5 mnutes ago.
Are you referring to in the same project? If so, how are you managing to compile the code-behinds?
0
 
Sudheer_KeesaraAuthor Commented:
I have not tried it yet and may be that is what I have not conveyed propperly. So I am not sure if can have the two different versions of code files in the same project. I have not tried it either with web apps or with web sites.
0
 
ddayx10Commented:
Well I might learn something here on someone else's points :)

@kaufmed I was just curious so I made a new web application using vb then I copied/pasted a default.aspx and default.aspx.cs set over from my previous testing (earlier in post).

Setup response.write from one to the other. Set breakpoints. Ran Debug. Worked like charm. I was actually surprised.

I will now await you to tell me something I didnt know (when I'm lucky this happens).

I hope I was clear that I was only discussing pages (default.aspx.vb and webform1.aspx.cs) with their respective front ends.
0
 
ddayx10Commented:
meant response.redirect of course (type too fast).
0
 
käµfm³d 👽Commented:
@ddayx10
I was just curious so I made a new web application using vb then I copied/pasted a default.aspx and default.aspx.cs set over from my previous testing (earlier in post).
What did you name the page in your VB project? Is it also "Default.aspx"?
0
 
käµfm³d 👽Commented:
This is what I get when I try the same thing:

Sample Solution (VB)


Untitled.png
0
 
ddayx10Commented:
No I got the names mixed around.

VB Web Application
WebForm1.aspx & WebForm1.aspx.vb
Copied over Default.aspx & Default.aspx.cs

Default.aspx.cs Page_Load >> Response.Redirect >> WebForm1.aspx.vb Page_Load

I don't work with web apps a lot lately. Spent a couple years on a big project using them, but lately most of my stuff just websites. Unfortunately I dont always control the infrastructure I get stuck with.
samplewebapplication.jpg
0
 
ddayx10Commented:
@kaufmed

I'm sure I'm just being a knucklehead and lucked into something. Like I said I never even thought about trying it until you mentioned it.

I only knew about it for websites because someone forced me to figure it out a few years ago, since abandoned thank god.

Poor Sudheer_Keesara we are like monopolozing his thread. Its relevant to his situation but I still wonder if we should go on and on.

Maybe you or me should open a question of our own and we can discuss this further sometime if you like? Anyway I'm going to bed, way too late here.

Thx.
sample2.jpg
0
 
ddayx10Commented:
Well Sudheer_Keesara. I'm going to come back to this and say...

C# and VB.NET can co-exist in the same project (webpage or application) and work fine. They will debug and behave properly. Its not ideal (and you know that) but it is workable.

When dealing with the app_code folder I've shown you the trick for that.

When dealing with a web app it also works both at the page level and debugging into multiple referenced apps of differing code (c#/vb). This is a very obscure subject and the best I can determine is that it likely shouldn't be done within the same assembly (multiple pages of differing code type in same project).

On this matter I can maybe make 2 points:

1. In earlier versions of .NET each project compiled into its own assembly. In later versions there were subtle changes made to this that I don't think I can explain but they changed that model.

2. If you create two web applications in the same project (one vb and one c#) and you copy/paste from one to the other from within Visual Studio (I tested c# to vb) it will not compile. You will experience exactly what kaufmed did. If on the other hand you copy/paste from windows explorer and then "add existing" item it will compile.

Visual Studio performs a subtle manipulation of the file (I can't determine exactly what) that will cause the error. I could infer that this means you weren't meant to do this sort of thing, or I could infer that its just an oversight of an obscure issue in Visual Studio.

I'm not sure if a lot of developers say "You can't do that" because it doesn't work easily or if there is some documentation directly forbidding this type of activity. I can say what I did previously (and you already know) that its probably not a good idea.

I gave you samples and explanations of most of this. It became maybe more complicated as the experts haggled over stuff, but hopefully you didn't get too lost along the way and have had a chance to try some of this out.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 12
  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now