开发者

How to change language of class library in VS 2010

I've made vb console application project in vs 2010 and added one class with simple code.

Then I replace this code with c# code, and "i've changed extension from .vb to .cs .

But I can't run this application because I have got error:

'Sub M开发者_开发知识库ain' was not found in 'Test_VB_ClassLibrary'

Hwo can I convert my project to be c#?


I don't know that you can change the language of a project - it would change file extension and everything. It's probably simpler to just create a new project... if you want it to be in the same place, just rename and move the old one first.

EDIT: As noted in comments, you can change the language. But please, just make the right decision upfront or take the hit of creating a new project.


You will be better off re-creating your project, that said if you really want to do this then open up the vbproj file in notepad / a text editor and find the following line:

<Import Project="$(MSBuildToolsPath)\Microsoft.VisualBasic.targets" />

Change it to the following:

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />

You also need to rename the file to have a .csproj extension so that Visual Studio displays the project correctly.

Your current build problem is caused because the name of the startup object has also changed - to fix this go to the "Application" tab under the project properties and make sure that the startup object property is set correctly.

At this point your project is now a C# project file - any .vb files present in the project (including designer generated files) won't build correctly and you will need to remove them or convert them to C#.

This works (I have tried it), however there is a good chance that you have other problems with incorrect MSBuild properties being set, for example after performing the above steps:

  • The project didn't have the "Define DEBUG constant" checkbox ticked for the debug configuration
  • It also had a load of entries in the "Suppress warnings" property that aren't normally there for a C# project

Once again my advice is just to re-create your project from scratch instead.


You can't just change a file extension and expect this to work. There are MANY other files and settings involved. Your error is because "Sub Main" doesn't exist in C# and your project settings under VB are saying that Sub Main is the startup location of your app.

You have to create a new project and re-write your code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜