Unable to manually compile and use user-defined namespaces in c# - c#

I had just started learning C# programming language and I'm trying to run code manually(using command line) and on Visual Studio both. But now I'm stuck on "namespaces". I had made two files, one containing user-defined namespace and another file will access that namespace features. I succeeded in running this code on Visual Studio but unable to run manually. The file containing only namespace is not compiling and giving error that "it does not contain a static 'Main' method suitable for an entry point". The two files are in the same folder. See the code below:
//file "Check1.cs"
namespace Ankur{
public class Check1{
public string a = "Hello world!";
}
}
//file "Checkit.cs"
using System;
using Ankur;
class Checkit{
static void Main(){
Console.WriteLine(Check1.a);
}
}
How Visual Studio is running that code and what should I do to successfully run this code manually?

Related

.NET Assembly issue with MATLAB [duplicate]

I used the MATLAB Compiler to generate a .NET Assembly with a very little MATLAB code:
function output_arg = extest( input_arg1,input_arg2 )
output_arg = input_arg1+input_arg2;
end
I generated the dll with the wizard.
Within my Visual Studio project I added the reference to the generated dll (extest.dll) and to the MATLAB Runtime dll (C:\Program Files\MATLAB\MATLAB Runtime\v92\toolbox\dotnetbuilder\bin\win64\v4.0\MWArray.dll) as mentioned in the "Assembly Description".
This is my c# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MathWorks.MATLAB.NET.Utility;
using extest;
namespace DllTesting
{
class Program
{
static void Main(string[] args)
{
ExClass e1 = new ExClass();
}
}
}
It builds without errors an intelisense is working (so all references should be good from my understanding)
But when I launch it, the following exception gets thrown (on new ExClass()):
An unhandled exception of type 'System.TypeInitializationException' occurred in DllTesting.exe
Additional information: The type initializer for 'extest.ExClass' threw an exception.
Any suggestions what is wrong with this code or whats missing?
Try adding this before the class definition
[assembly: MathWorks.MATLAB.NET.Utility.MWMCROption("-nojit")]
Also make sure that the .NET version you use for assembly is the same or lower than the one used for your Visual Studio project.
Another solution might be adding the path of the MATLAB runtime (e.g. C:\Program Files\MATLAB\MATLAB Runtime\v92\runtime\win64) to the PATH Environment Variable.
If none of these helps, have a look here and here, you might have a 64/32 bit mismatch.

Text node cannot appear in this state (Line 1, position 1)

I'm just starting to learn C#, and I am using Koding to develop (since I don't currently have access to my Ubuntu machine). I'm attempting to get the Hello World program to work, but the compiler I'm using (which is Mono) isn't taking it.
This is the code that I'm using for the program:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1{
class Program{
static void Main(string[] args){
Console.WriteLine("Hello, World!");
Console.ReadLine();
}
}
}
I created a file, wrote the code, and attempted to compile it using xbuild program.cs but I got the error message Text node cannot appear in this state (Line 1, position 1).
What am I doing wrong here? Is it the compiler, the OS, the code?
xbuild expects a project or solution file (which are xml format), not a c# source file. You should pass that (e.g. xbuild program.sln), or if you don't have one then invoke the compiler directly by mcs program.cs or similar.
PS: You should have mentioned the command line you used in the original question, instead of in a comment later.

Why class definition is changed when reference added to C# project?

I am working on C# on Win 7 Visual Studio 2012.
I have added a reference (defined and compiled in a .dll file in another location) to my C# project because I need to call a function that is defined in a class of that lib file.
But, when I check the definition from my current project, I find that the definition is changed and it is different from what I see when I open the source file where the class is defined.
I am new to C#.
What is the possible reason?
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Data;
using System.Diagnostics;
using System.Data.Odbc;
namespace MyName
{
public class MyDataLoad // the class definition is changed when I call it !!!
{
#region
// some functions definition
#endregion
void MyLoadDataFile()
{
}
}
}
UPDATE AGAIN
The definition of MyLoadDataFile() noes not exist in the .cs file where class MyDataLoad is defined. I open the file by right-clicking MyLoadDataFile() in my project.
The opened file is different from the source file that is used to generate the .dll lib, which is added as a reference to my project. But, I do not know how to solve the problem.
The definition of class MyDataLoad is different from what I see in the source file that is used to generate the .dll file. This is the reason why my c# program in my project cannot call MyLoadDataFile() because it is defined in the source file that is used to generate the .dll file. The definition of class MyDataLoad is different from the files that are used to get .dll file.
Thanks
I suspect that what you referring to is the fact that you can't see method MyLoadDataFile in your c# app. This is because you didn't use access modifier. You have
void MyLoadDataFile()
You need
public void MyLoadDataFile()
Your default modifier is private on class members. And this is why you don't see it

Mono autoloading or auto namespace / class referencing

I am new to Mono, just freshly installed and am trying to get the basic setup up.
Hello.cs:
using System;
using Customz;
public class Hello
{
public static int Main(string[] args)
{
Console.WriteLine( "Hello world! Customz.Beko.Wally = " + Beko.Wally() );
Console.ReadKey();
return 0;
}
}
Customz/Beko.cs:
namespace Customz
{
public class Beko
{
public static int Wally()
{
return 15;
}
}
}
Basicly, I have been working with C# previously in Unity3D's adaption of MonoDevelop and Visual Studio.
I remember, that upon compilation, all the classes / namespaces get auto referenced when they are requested. Plus, they are looked up no matter how deep they dwell within the project directory / subdirectories.
Compiling this with:
mcs "C:\Users\psycketom\Desktop\Mono-C#\test\Hello.cs"
Results in:
C:\Users\psycketom\Desktop\Mono-C#\test\Hello.cs(2,7): error CS0246: The type or namespace name `Customz' could not be found. Are you missing a using directive or an assembly reference?
Compilation failed: 1 error(s), 0 warnings
Well, I know that I am not missing a using directive, but I have a feeling that I am missing an assembly reference.
How can I fix this and achieve the Visual Studio like behavior?
Update:
I managed to compile everything in a directory by issuing:
mcs -out:app.exe "C:\Users\psycketom\Desktop\Mono-C#\test\*.cs"
While it works, it does include only files within the folder, not taking into account any subfolders residing in the folder.
How do I make it select all the files, including those residing in subfolders? (I'm starting to think that I may need a bash script or something)
You'll probably want to be using make, NAnt or some other build system rather than the raw command line. The problem is this:
Your project folder has a few files and few folders. So you have
Foo.cs
Bar.cs
Biz/Test.cs
Biz/Foobar,cs
When you use mcs -out:app.exe "C:\Users\psycketom\Desktop\Mono-C#\test\*.cs" the command line shell (NOT the compiler) will expand the * wildcard for you. So, it expands to
Foo.cs
Bar.cs
Most shells don't go recursive(maybe without a special directive?) because it's much more expensive and generally unexpected behavior.
If you're really determined to use the command line, you'll have to do something like this:
mcs -out:app.exe "C:\Users\psycketom\Desktop\Mono-C#\test*.cs" "C:\Users\psycketom\Desktop\Mono-C#\test\Biz*.cs"
Also, if you're that determined to use the command line, you'll probably want to give something like Cygwin a try. It's a million times easier to use than cmd.exe (and it seems to work well with Mono)
One more idea that I've used in the past: You can also use something like MonoDevelop to manage a solution and project files, which basically will link everything up in your program and tell the compiler what needs to be included. Outside of that though, you can easily build from the command line by using the xbuild tool and the solution file that MonoDevelop made

Does not contain a static 'main' method suitable for an entry point

I began organizing my code to day into seperarate .cs files, and in order to allow the methods that work with the UI to continue to do so I would create the .cs code under the same namespace and public partial class name so the methods could be inter-operable.
My header look like this in four files, including my main core file that calls:
public shell()
{
InitializeComponent();
}
Header area of .cs files that work with the UI (and seem to be causing this new conflict):
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Data.SqlServerCe;
using System.Diagnostics;
using System.Threading;
using System.Collections.Specialized;
using System.Net;
using System.Runtime.InteropServices;
using watin = WatiN.Core;
using WatiN.Core.Native.InternetExplorer;
using System.Web;
namespace WindowsFormsApplication1
{
public partial class shell : Form
{
Now when I try to debug/preview my application (BTW this is a Windows Application within Visual Studio 2010 Express) I get this error message:
Does not contain a static 'main' method suitable for an entry point
I looked in the application properties in Application->Startup object, but it offers me no options. How can I inform the application to begin at the .cs file that has my InitializeComponent(); command?
I've looked around so far without a solution.
The properties on each .cs file are set to 'Compile'.
I do not see an App.xaml file in my Solutions explorer but I do see a app.config file.
I'm still very new and this is my first attempt at an organizing method with c# code.
I was looking at this issue as well, and in my case the solution was too easy. I added a new empty project to the solution. The newly added project is automatically set as a console application. But since the project added was a 'empty' project, no Program.cs existed in that new project. (As expected)
All I needed to do was change the output type of the project properties to Class library
Change the Output Type under the Project > Properties to that of a “Class Library”. By default, this setting may have been set to a “Console Application”.
I had this error and solved it using this solution.
Right click on the project
Select "Properties"
Set "Output Type" to "Class Library".
Try adding this method to a class and see if you still get the error:
[STAThread]
static void Main()
{
}
If you don't have a file named Program.cs, just add a new Class and name it Program.cs.
Then paste this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Sales {
static class Program {
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Select App.xaml and display its properties. Set Build Action to ApplicationDefinition.
App.xaml and its corresponding *.cs file must be placed into the root directory of the *.csproj file, i. e. not into a "Source" folder.
Had this problem in VS 2017 caused by:
static async Task Main(string[] args)
(Feature 'async main' is not available in C# 7.0. Please use language version 7.1 or greater)
Adding
<LangVersion>latest</LangVersion>
to app.csproj helped.
Edit .csproj file
<OutputType>Library</OutputType>
cheers !
If you do have a Main method but still get this error, make sure that the file containing the Main method has "Build action" set to "Compile" and "Copy to ouput directory" set to "Do not copy".
For me, the error was actually produced by "Feature 'async main' is not available in C# 7.0. Please use language version 7.1 or greater". This issue was resulting in the "Does not contain a static 'main' method suitable for an entry point" message in the Error List, but the Output window showed the "not available" error.
To correct this, I changed the language version from 'C# latest minor version (default)' to 'C# latest minor version (latest)' under Advanced Build Settings.
hey i got same error and the solution to this error is just write Capital M instead of small m.. eg:- static void Main()
I hope it helps..
Looks like a Windows Forms project that is trying to use a startup form but for some reason the project properties is set to startup being Main.
If you have enabled application framework you may not be able to see that Main is active (this is an invalid configuration).
Salaam,
I have both Visual Studio 2017 and Visual Studio 2019
Visual Studio 2019 does not show this error but 2017 does. Try Installing Visual Studio 2019.
Visual Studio 2017
Visual Studio 2019
Just right click on project and select properties and then set Output type on Class Library
After placing the above code in Program.cs, follow below steps
Right click on the project
Select Properties
Set Output Type to Windows Application
Startup object : namepace.Program
When you want to allow paramaters to be specified from the command, they must look like this:
[STAThread]
static void Main(params string[] paramaters)
{
you cannot specify more than one paramater, otherwise this will also cause the error reported above.
For some others coming here:
In my case I had copied a .csproj from a sample project which included <EnableDefaultCompileItems>false</EnableDefaultCompileItems> without including the Program.cs file. Fix was to either remove EnableDefaultCompileItems or include Program.cs in the compile explicitly
hellow your main class was deleted so add new class that name set as Main.cs and pest that code or if porblem in window so same problem on that
using System;
using System.Collections.Generic;
using System.Linq;
using Foundation;
using UIKit;
namespace your_PKG_name.iOS
{
public class Application
{
// This is the main entry point of the application.
static void Main(string[] args)
{
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main(args, null, "AppDelegate");
}
}
}
A valid entry looks like:
public static class ConsoleProgram
{
[STAThread]
static void Main()
{
Console.WriteLine("Got here");
Console.ReadLine();
}
}
I had issues as I'm writing a web application, but for the dreadly loading time, I wanted to quickly convert the same project to a console application and perform quick method tests without loading the entire solution.
My entry point was placed in /App_Code/Main.cs, and I had to do the following:
Set Project -> Properties -> Application -> Output type = Console Application
Create the /App_Code/Main.cs
Add the code above in it (and reference the methods in my project)
Right click on the Main.cs file -> Properties -> Build Action = Compile
After this, I can set the output (as mentioned in Step 1) to Class Library to start the web site, or Console Application to enter the console mode.
Why I did this instead of 2 separate projects?
Simply because I had references to Entity Framework and other specific references that created problems running 2 separate projects.
For easier solutions, I would still recommend 2 separate projects as the console output is mainly test code and you probably don't want to risk that going out in production code.
If you are using a class library project then set Class Library as output type in properties under application section of project.
Another situation where this occur is when someone (unintentionally) changes Build Action for Program.cs. The value for Build Action should be C# compiler.
I accidentally changed Build Action to None, which removed program.cs from the project and therefore wasn't included when compile started.
Did you accidentally remove the entire Program.cs file?
If you have removed,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ListWievKullanımı
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
This might work for you.
Make sure you are not using void with async like
static async void Main(string[] args)
If yes, then change void to Task like
static async Task Main(string[] args)
If you do indeed have a public static main method it could be your build settings as explained in this question: Troubleshooting "program does not contain a static 'Main' method" when it clearly does...?
I too have faced this problem. Then I realized that I was choosing Console Application(Package) rather than Console Application.
I am using Visual Studio and also had this problem. It took me some time, but in my program it was caused because I accidentally deleted a Class named "Program" that is generated automatically.
For future readers who faced same issue with Windows Forms Application, one solution is to add these lines to your main/start up form class:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyMainForm());
}
Then go to project properties > Application > Startup Object dropdown, should see the namespace.MyMainForm, select it, clean and build the solution. And it should work.
Check to see if the project is set as the "Startup Project"
Right click on the project and choose "Set as Startup Project" from the menu.
If you are like me, then you might have started with a Class Library, and then switched this to a Console Application. If so, change this...
namespace ClassLibrary1
{
public class Class1
{
}
}
To this...
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
}
}
}
If you use Visual Studio Code change Project Sdk="Microsoft.NET.Sdk.Web" to Project Sdk="Microsoft.NET.Sdk" on csproj file.

Categories

Resources