C# beginner program - c#

I'm using Visual Studio and when I try to compile the code below I get two errors;
CS0103 The name 'Console' does not exist in the current context
CS0017 C# Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point.
How do I fix this?
using System;
namespace App2
{
class Class1
{
static void Main()
{
Console.Writeline("Hello");
}
}
}

It looks like Class1 is not the only class that has a static void Main() method defined. Usually, when you create a console application, there's a class called "Program" that already contains a method Main. There should be no need to add another class.
Just modify the existing Main method. This should solve the second error.
The Main method is a bit like the Highlander of methods - there can only be one.
As for the first error: You do need to target the Windows platform to be able to use Console, so you need to create a Console application or a Windows Forms/WPF application or the like.

The first error is caused by a typo. To correct it, change Writeline to WriteLine (with a capital letter L).
The second error is caused by the fact that you did not specify clearly which entry point the program should use. To fix this, follow these steps:
Right-click on your project in Solution Explorer, and open the "Properties" menu. You will see the similar page and all that you should do is explicitly select entry-point.

As Thorsten Dittmar said, ensure you don't have a duplicate static void Main method,
CS0103 The name 'Console' does not exist in the current context
Ensure you did not create an Universal Windows Platform (UWP) project/Android. You can't call Console on it.

As many have suspected I fixed the error by going into the installer, modified and checked off the .NET Desktop Development.
It works now.

Related

ASP NET MVC application asks static 'Main' method

Building ASP NET Application I capture the following error:
Error CS5001 Program does not contain a static 'Main' method suitable for an entry point [ProjectName] [ProjectPath]\CSC 1 Active
Output type of the project is 'Windows Application'. Default namespace and assembly name match the namespace through the application. And there's just one project in solution.
The whole problem started as soon as I created entities model (based on Northwind), made some relevant changes in controller and called to create a new view. The window I received informed me
"There was an error running the selected code generator: 'There was an error getting the type [ProjectName].Models.Category'. Try rebuilding the project". [ProjectName].Models contains public class 'Category'.
I suppose it is just the result of some error, not the error itself.
I changed namespace all over the application not long ago, so I assume this might be the problem and the compiler still requires some changes to be done. But looking through the whole solution I didn't find any notions of the old namespace.
My competency in ASP.NET MVC is far from brilliant, so please any ideas how to check, what I should examine, where I should look through and/or change are welcome.
Oops, pity me. It should be "Class Library" type instead of "Windows Application". Simple mistake made me triplecheck almost all the application (and a relevant part of similar SO issues).

How to access a class which is in a folder c# (wpf)?

I'm building a WPF application. I'm almost finished with one part of it, and now I want to clean up the code a bit. English is not my native language (so sorry if this gets marked as a duplicate) and I couldn't find anything about this, so I hope someone can help me out.
If I have public voids in a class called 'class.cs' and this class is in a folder called 'classes', how do I access the class so I can use its functions?
At the top of the file where you want to use the class include a using statement.
using [namespace];
Then you will be able to instantiate the class
Class testClass = new Class();
testClass.Method();
If you aren't sure what the namespace for your Class class is, then you can write the line to instantiate the class and then use the intellisense (assuming you are using a development ide like Visual Studio) to automatically include the reference. Alternatively, you can look at the top of your class.cs file to see what namespace has been declared.
check what namespace it's in , then what the class name is and call it as
"Namespace.class"
in case that you can't see it still , check if the class is public and the class.cs properties have the build property set to compile
I'm not sure from where You are trying to access this class. But I think the easiest way of doing it probably will be just typing your class name in place where You are trying to use it and press 'ctrl+.' which should show You context menu with options to add using statement for this class.
If You are trying to access this class from other project in solution then above way will only work with VS2017(because it will also add missing project reference) otherwise You need to add reference for this project and then add missing using statement for your class(in your case 'using ProjectName.classes').
The folder you are talking about is called a namespace. You first need to import the namespace and then you can use your class.
*Normally the IDE will prompt you.

No main() in c# project

The issue I'm having is trying to find the run order for a number of classes in a project. There is no explicit main and I have looked in obj/debug and found nothing which might have been generated at compile time. Have you any ideas where I might find something. Also I cannot find any xaml files either. It's a console function.
This is a quick way to find the entry point (if there is one):
Without any specific source file open, press F11 to start the debugger at the first line. This should make your entry point obvious.
Note that if this is a Console Application like you said, there must be a static main() method.
Here is the documentation for Main():
The Main method is the entry point of a C# console application or windows application. (Libraries and services do not require a Main method as an entry point.). When the application is started, the Main method is the first method that is invoked.
If you check the project properties, under "Application", there is a "Startup object" listed. This should tell you the entry point to the application, which is the type which contains the Main method that is actually being used.
If it's a console application then you will usually find the Main method in the auto-generated Program.cs file.
If there is no main method, the project is a class library.
Other than #Gray's answer, you could also Ctrl+F for Main.

Problems when running project with two main mehods in VisualStudio

In my project I have two different classes with main methods to create different objects of different classes. But when I want to run one of the classes with a main method, it starts and run the other object that is created in the other main method!?
Perhaps a little bit confusing, but I hope the reader can understand what I'm trying to explain. I have googled and found some info about do some changes in the properties for the classes, but I couldn't find out how. Preciate some help! Thanks!
EDIT:
Perhaps I was unclear. I have main class called TaxProgram and it contain the main method that creats an object of the class Product.
Then I have another class called StartBox and it also contain a main method but it creates an object of the class Box.
The problem is when I press F5 to Debug, I thought that the merked tab with the class name StartBox should start and create an object of the box class, but now I knew that VisualStudio doesn't work that way. I have tried to change the properties to be able to start the StartBox insetad, but without any luck.
The image gives a view of my classes
You need to open project properties + Application Tab + set Startup object.
If you mean a standard Main signature :
static void Main(string[] args)
Then, you should only have one function like this in your program. Rename the other one.
IIRC there is an option in the project properties to set which class's Main is called at startup, but I can't confirm this until I get to work.
Click with the right button in your solution, then select "Set StartUp Projects..."
In the project properties, in the Application tab, is an option for Startup object. You can select the class that contains your intended entry point, as long as it contains a valid Main method.

C#: How to embed DLL into resourcefile (no dll copy in program directory)

I have a C# application (project A) that requires X.dll. I have added the project that produces X.dll to A as a reference in visual studio. I have also added the release build of X.dll to a resource file in A as a binary. I have told project A not to copy X.dll to the output directory.
Now I want A.exe to load, say "hey I can't find this file", then look in the resource file and use Assembly.Load(byte[]) get X.dll back. I have the code that re-magicifies the DLL back, however this code never get called.
Currently I have a bone simple project, just trying to get it to work. It compile OK. When I run it, I get a FileNotFoundException on X.dll.
I have:
[STAThread]
static void Main()
{
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
}
But the breakpoint in *CurrentDomain_AssemblyResolve* never gets hit. I get a FileNotFoundException immediately. Surely there is something I a missing?
This question seems to be very similar to what you are trying to achieve, and it worked for the asker.
Merge .dll into C# Assembly?
Are you doing something differently? Specifically, if you're targetting an older version of the .NET Framework, maybe that's a hint for understanding why your application is behaving differently.
Another direction, use a tool such as Fusion Log Viewer to analyze what was going on when you tried to load the assembly. This may give some more hints. If you manage to get log information, posting it in the question may help someone figure it out.
EDIT: Another explanation, following your comment.
Well, now I think I know what the problem is.
In your Main method, you are refering to the type in the other dll. But you are doing it in static code, i.e. you use the type explicitly in the code (as opposed to loading it dynamically by its name).
Why is this a problem? The CLR tries to load your assembly in order to JIT Main itself.
Your FileNotFoundException was thrown while Main was being compiled. Main didn't even start running, and therefore your event handler wasn't registered.
A simple way to check if I'm right is to change the code to something like this:
static public void Main(string[] args)
{
AppDomain.CurrentDomain.AssemblyResolve += MyEventHandler;
MyMain();
}
// The CLR will actually try to load your assembly before even starting the execution
// of this method. It needs the assembly in order to JIT the method because it has to
// know the Thing type.
static public void MyMain()
{
using(var thing = new Thing())
{
// ...
}
}
The important difference is that Main has no dependency on another assembly, so there will be no problem to JIT and run it.
By the time MyMain is being JIT'ed, your event handler is already in place so you can manually load the assembly.
By the way, to avoid another possible similar pitfall, make sure that the class in which Main is defined doesn't have any field with a type from the other assembly, because in this case also, the CLR will try loading the assembly before Main starts, in order to compile it.

Categories

Resources