For an assignment i had different DataStructures (in C#) that I needed to implement. That part was easy. But I am also required to make a main menu driven programme for all those projects. So i searched a bit and found out a way. For example i had a project named linkedlist with namespace as Implementing_LinkedList and another project queue and namespace as Implementing_queue. To make a menu driven programme i just created another project named Assignment with namespace assignment. Mainconsole. Then i changed the namespace of linkedlist project to Assignment.Implementing_LinkedList and similarly with queue.Also made all the methods and classes public. Then i just called the main method of linkedlist inside main method of assignment as follows
Implementing_LinkedList.Program.Main();
Main method is inside a class named program in linked list project and it worked. Now i want to know how this worked because specifically .MainConsole part and if there is any other way to acheive this.
(Also i am pretty new to Stack overflow so pardon me for the extra long question but i didn't want to skip any details)
You don't have to rename namespaces or anything. Each program's Main() function is static and it can be called from any other function using a fully qualified name.
Program1.cs
namespace Project1
{
public static class Program1
{
public static Main(string[] argc)
{
// code here
}
}
}
Program2.cs
namespace Project2
{
public static class Program2
{
public static Main(string[] argc)
{
// code here
}
}
}
MenuProgram.cs
namespace MenuProject
{
public static class MenuProgram
{
public static Main(string[] argc)
{
// call all projects in sequence
Project1.Program1.Main();
Project2.Program2.Main();
}
}
}
This assumes the menu project has all the sources of the sub projects included. If they are separate project files, then you need to Add Reference... to those projects from the project file references.
Related
I am getting an error on my code that says "Error CS5001
Program does not contain a static 'Main' method suitable for an entry point"
I am coding in C# using Microsoft Visual Studio and .NET. This is my code.
using System.IO;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using System;
class projectsummer
{
[CommandMethod("OpenDrawing", CommandFlags.Session)]
public static void OpenDrawing()
{
string strFileName = "C:\\DRAFT.dwg";
DocumentCollection acDocMgr = Application.DocumentManager;
if (File.Exists(strFileName))
{
acDocMgr.Open(strFileName, false);
}
else
{
acDocMgr.MdiActiveDocument.Editor.WriteMessage("File " + strFileName +
" does not exist.");
}
}
}
I am not sure how to go about this error. Thank you!
Looking at this post and your previous question, let's try and break down what's going on.
You created a new Console application in Visual Studio. You did not tick "Do not use top level statements". This gave you a Program.cs file that was essentially empty (there was no "Main" method visible).
You erased the Hello World code given to you, and went to make a static method - the code from your previous question.
Damien_The_Unbeliever commented that based on the error, you put your method inside a "top level statement" file, and to put your method inside a class.
You wrap your method (which is still inside Program.cs) in a class, and now suddenly you get a Can't Find Entry Point error.
User Ryan Pattillo posted a great explanation of the original issue - where your method was "by itself" in the Program.cs file. You should follow their advice, but you should also ensure that this class is in its own file.
You should end up with this:
Program.cs
// this is the entire contents of the file
using ConsoleApp1;
ProjectSummer.OpenDrawing();
ProjectSummer.cs
using System.IO;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Runtime;
using System;
namespace ConsoleApp1
{
public class ProjectSummer
{
[CommandMethod("OpenDrawing", CommandFlags.Session)]
public static void OpenDrawing()
{
// ...
}
}
}
Change ConsoleApp1 to the name of your project.
The entry point of your application, which right now is the only file that has "top level statements", remains Program.cs, thus you fix the Can't Find Entry Point error.
Another adjustment you can make, which seeing you're new to C# might be useful, is to not use top level statements at all. Modify your Program.cs to this:
namespace ConsoleApp1
{
internal static class Program
{
// this is your program's entry point
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
ProjectSummer.OpenDrawing();
}
}
}
Change ConsoleApp1 to the name of your project.
You cannot use the AutoCAD .NET API out of process. To be able to use the AutoCAD .NET libraries, you have to build a "class library" project (DLL) and NETLOAD this DLL from a running AutoCAD process. See this topic about in-process vs out-of-process and you can start from this other one to see how to create an AutoCAD .NET project.
Say If we use two separate files called a.cs and b.cs in a C# project using VISUAL STUDIO, my question is does one file aware of the other WITHOUT putting a using statement about the other file. ie In the file a.cs can we use a class that is already defined in b.cs but not putting a using b.cs; statement in the beginning of the file?.When we compile altogether will the project know each file content and won't raise any error?
I guess you are on the wrong track here. Files don't interact with each other. But the classes do. Namespaces are used to refer to the class that are meant to be used. You can change the file name to anything several times, it won't affect your project. Moreover you can put many classes inside the same file name under the same namespace and you won't have to use using.
Just consider this scenario, Namespace are the area code, and the phone numbers are the classes. Being in the same area already, you don't have to use the area code to call a different number that exists in the same area. But if you are dialling a number outside your area, you would want to use the area code. Basically by adding area code(namespace) infront of the number, you are applying using to refer to the other number(class). Hope you got the idea.
Edit: Explaining programmatically
Suppose this is your Area
using something;
using someotherthing;
namespace MyMainNamespace
{
private class MyMainClass
{
private void blahblah { ... }
}
private class ClassABC
{
private void blahblah { ... }
}
private class ClassXYZ
{
private void blahblah { ... }
}
}
See, in the above example, to interact with the MyMainClass, ClassABC & ClassXYZ. you don't have to use using MyMainNamespace;. Because they all lie in the same area MyMainNamespace. But there exists a class in another namespace like shown below:
using something;
using someotherthing;
namespace SubNamespace
{
public class SecondaryClass
{
public void apple{ ... }
}
}
If you want to access SecondaryClass which lies in SubNamespace(different area) you would have to use using SubNamespace; in your main area. Like:
using something;
using someotherthing;
using SubNamespace; //add the namespace
namespace MyMainNamespace
{
private class MyMainClass
{
private void blahblah {
...
// Now you can use methods & functions that exist in `SecondaryClass`
SecondaryClass secondary = new SecondaryClass();
secondary.apple();
....
....
}
}
}
Hope this is enough to get the idea by now
Also, it doesn't matter that these namespace(MyMainNamespace & SubNamespace) lies in the same file or different file. You NEVER REFER TO THE FILENAME(filename.cs) by applying using. You ALWAYS REFER TO THE NAMESPACES.
If the C# code in a.cs and b.cs are inside the same namespace, then no using statement should be needed. If the 2 cs files use different namespaces, then you will have to put a using statement for the namespace of the code you want to reference.
My Setup :
Project A
Class Log
{
public static void WriteLog(string msg)
{
Trace.write(GetTimestamp(), GetAppDominNameCallingWriteLog(), msg);
}
}
Project B
contains Static link Log.cs (Add as link)
Project C
contains Static link Log.cs (Add as link)
Project D
contains Static link Log.cs (Add as link)
Using one log file for all the project. Now I need to get the project name (GetAppDominNameCallingWriteLog()) in the Log class. How can i achieve this without passing Project name to WriteLog().
e.g. Project C calls Log.Writelog("logging msg")
the result should be 201511121232 Project C logging msg
Project D calls Log.Writelog("logging msg")
the result should be 201511121232 Project D logging msg
Tried with Thread.AppDomain(), it always return Project A.
Sorry I forgot to add this case : Project C is referenced in Project A.
You can find out the executing project using the below code
AppDomain.CurrentDomain.FriendlyName;
I have also checked this with the Thread class. It returns the result correctly
System.Threading.Thread.GetDomain().FriendlyName;
UPDATE
Based on the additional information provided in the comments, you can identify the calling Assembly using Reflection
System.Reflection.Assembly.GetCallingAssembly().FullName;
Working Example
static void Main(string[] args)
{
//Direct call to the Log class from ConsoleApplication3
Log.Write();
//Indirect call to the Log class through a Class Library called ClassLibrary1
Class1.LogIt();
Console.ReadLine();
}
And the output is
When I'm developing a ConsoleApp there is no problem to use classes in my Main that I've created in separated files (Project Menu --> Add Class). But later, when I try to do it in WPF that class is not recognized. I have made sure that the namespace it's the same both in my "MainWindow.xaml.cs" and my Class Canal.cs. When I define that same class but inside MainWindow.xaml.cs everything works fine, but due to the extension of the code I prefer separate it.
MainWindow.xaml.cs:
//using
namespace Tcomp
{
public partial class MainWindow : Window
{
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{ //Stuff but I can't use class created outside of MainWindow.xaml.cs
}
}
}
Canal.cs
//using
namespace TComp
{
public class Canal
{ //some propreties here
}
}
Thanks.
Create the class inside a library project not on a console app. And on your WPF project, add a project reference to the library project you have created.
#mcxiand already answered your question. I would like to add another option: you can use a
public partial class MainWindow : Window
and add it to as many files as you want containing your code, thus there will be no need to create additional class library. The key word here is partial, which allows the code encapsulated in this class to spread over multiple files (.cs).
You must either instantiate the Canal class:
var myClass = new Canal();
and then you can use the properties from it. Make myClass a private member of your MainWindow.xaml.cs and you can access it anytime. Or the second way, make Canal class static and then you can access it from everywhere. Hope this helps.
I have two applications and 1 class for database process.
First Application is a Windows forms application like
public class Form1 :System.Windows.Forms.Form
{
public Form1()
{
}
}
And my second application is a Windows control library application like
public class MyControl :System.Windows.Forms.UserControl
{
public MyControl()
{
//
//Some Code
//
}
}
and my class is static class like
public static class Deneme
{
public static void Connect()
{
//
//SomeCode
//
}
public static void CreateTable(string SqlCommandP)
{
//
//Some Code
//
}
}
I compile the control library application and get a .dll file. After that, I'm adding this file to Windows application project toolbox. I can use this control.
My problem is that:
I am using static class in a Windows application and control library application maybe I will make another control libary application.
How can I use this static class from one project it have to be one in a project?
If the static class Deneme is to be shared between the 2 applications, you should place that static object in it's own library which both the WinForm project and the control project will reference.
Reference one project to the other? It seems like you're forgetting the Project Reference somewhere. Being more specific with the question might be helpful, though.