No main() in c# project - c#

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.

Related

C# beginner program

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.

How can I read console input before open a window in a C# Form Application?

I am making a program that ask you if you want to run your application on console or in GUI. I tried to make a read from the console before open the GUI but it doesn't work. The if don't evaluates the input. I'm new with C#.
This is a screenshot
You need to make a solution with 2 project, one console application and the other windows form application, and in the first one you can call the other application adding some references.
FIRST ATTEMPT : I'd say - try to build two seperate projects( one for console and one for windows form). Build those two projects. You'll get separate executable files.
Then in the console invoke the form's executable file when necessary.
I think this link might help you -
Can you execute another EXE file from within a C# console application?
Edited :
I think you've two main methods in the same namespace. So, your project has two entry points. Two entry points will ultimately confuse you machine :) . So, you need to create two separate projects( i.e two separate namespaces will be created). Also take a look at this code by one of my favourite youtubers Jerry_aka_Barnacules https://github.com/Barnacules/Codegasm/blob/master/Test%20App%203%20-%20Jarvis/Program.cs .
Also take a look at his video at youtube - https://www.youtube.com/watch?v=c1In6NbJt5E&list=PLEbaEyM-xt9mVQEAXGlRRmbO2Qp_oqF-n&index=3
Also try creating a solution with two projects.
By the way you can learn a lot by visiting these links. I'm suggesting you that because you said you're new to C#.
I hope you can solve your problem easily by referring to those links. So, best of luck for that.
I'm new to stackOverflow and I'm learning new stuffs. So, please let me know whether my suggestion helped you or not.

0xC0020001: The string binding is invalid. - Only occurring in WPF

First off, I should probably say that I'm probably at a grade 5 level with this stuff... I'm using a C++ add-in in a WPF application. Whenever I try to exit the program, I get this error:
Unhandled exception at 0x770d15de in Raptor.exe: 0xC0020001: The string binding is invalid.
I've been using this blog entry to try and figure the problem out, but I'm having no luck. One thing I noticed though, when I use the same C++ addin in a Console application, calling many of the same methods used in the WPF application, the Console exits without a problem.
I've also gone through the C++ code and cannot find a single static variable declared anywhere. There are static methods though.
Any help would be much appreciated!
EDIT: I enabled a number of debugging features to see where this breaks. It was breaking the sp_counted_impl.hpp file (Boost) on the last bracket of the following:
virtual void dispose() // nothrow
{
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
boost::sp_scalar_destructor_hook( px_, sizeof(X), this );
#endif
boost::checked_delete( px_ );
}
This occurs with certain DLLs that don't link with native libraries and thus their DllMain does not initialize some needed native subsystem (like CRT or ATL). Sounds like you have a mixed-mode DLL of some sort. One recommended solution is to remove the entry point from the managed DLL:
Remove the Entry Point of the Managed DLL
Link with /NOENTRY. In Solution Explorer, right-click the project
node, click Properties. In the Property Pages dialog box, click
Linker, click Command Line, and then add this switch to the
Additional Options field.
Link msvcrt.lib. In the Property Pages dialog box, click Linker,
click Input., and then add msvcrt.lib to the Additional Dependencies
property.
Remove nochkclr.obj. On the Input page (same page as in the previous step), remove nochkclr.obj from the Additional Dependencies property.
Link in the CRT. On the Input page (same page as in the previous step), add __DllMainCRTStartup#12 to the Force Symbol References property.
More detail can be found here: https://support.microsoft.com/en-us/kb/814472

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.

Running Windows Forms Application (C# .NET 4.0) from batch file with Command Line Arguments

I'm a Silverlight/ASP.NET developer trying to write my first Windows Forms application to run in the background on a server, populating our database. Eventually would like this to be a Windows service, but it's not required initially.
I need to create a batch file to execute 5 instances of this application, passing in the URL to 5 RESTful endpoints. So I published my app, which created a setup.exe. After installing it, I have an item that points to
C:\Users\mi2dev\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Microsoft\, with a .appref-ms file.
I'm not sure at this point what to do. Running:
"C:\Users\mi2dev\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Microsoft\StreamingApp.appref-ms" -"http://www.myURL.com" throws up a command window briefly, but the app doesn't run, data doesn't populate in DB.
What am I missing here?
since your application is in .exe format. And make your winform accepts command line arguments (check the main method) also make your Form ctor accepts params too. Then just launch it via cmd line just as you would other command, but here only to navigate to that dir where file exists.
In case of batch, use start command followed by program name and then arguments
It's hard to understand what is happening inside your application. You need to debug to understand what is going on there when it receives given parameters.
So I would suggest to debug an EXE. For this go to your EXE project properties, select DEBUG tab in CommandLineArguments insert your parameter string.
Run it in DEBUG and hopefully you will figure out a problem.
If after debugging it's not yet clear why it behaves in that way, come back to SO :)
Silvi if you plan to use your windows forms application from a batch file and you imagine the applicationm will behave differently in such mode than when opened witha double click, the usual approach is to parse the command line (arguments, also available in the main method as parameter) and to avoid loading the UI at all.
in fact if you have written your application properly the UI only managed the UI and does not contain the whole logic of database manipulation and data transformation.
what you could do is check inside the Main method if there are command line parameters and if you detect any of the special ones you have definded you really avoid to even call Application.Run(new Form1(...)); and start working in batch mode without user interface.
the same logic you want to use in batch mode or in UI mode can be wrapped in helper classes (often also called business managers or business logic... it depends), so that you do not have code duplication but simply UI or batch will call those classes nicely.

Categories

Resources