1

I have Two classes. Test.cs, Entity.cs.

I have put this two files in one folder "CommandTest".

*Test.cs Class

namespace Demo { public class Test { public static Entity entity= new Entity(); public static void Main(string[] args) { Console.WriteLine("Demo"); } 

*Entity.cs class

namespace Demo { public class Entity { Console.WriteLine("entity"); } } 

When I am Trying to run it through "Visual Studio Command Prompt". It shows Error,

Test.cs(10,28): error CS0234: The type or namespace name 'Entity' does not exist in the namespace 'Demo' (are you missing an assembly reference?)

I am not getting why it is shows error. Because both class have same namespace. How can I run it through Command Prompt.

Thanks.

7
  • 4
    What command are you running on the command line? And I assume you are trying to compile it, not run it? Likely you just didn't tell the compiler about Entity.cs. It needs to be explicitly told about all necessary files, it won't just search a directory (as far as I am aware). Commented Aug 28, 2014 at 10:52
  • 6
    Console.WriteLine("entity"); needs to be inside a method. Commented Aug 28, 2014 at 10:52
  • 2
    Can I ask why you aren't writing a unit test class instead? (judging by your filename, that is what you are looking to do) Commented Aug 28, 2014 at 10:53
  • Sorry, I am trying to compile it. csc Test.cs for Compile. Then it shows error. Commented Aug 28, 2014 at 10:55
  • 1
    @user3582190: You now have answers that explain how to add the other file. Any reason you aren't just using visual studio to do your compiling for you? If it is because you are only compiling some classes in your project for testing then you might want to look into how to do proper "unit testing" in your project. Commented Aug 28, 2014 at 11:03

2 Answers 2

2

You should use all files when calling csc. It doesn't try to find the code files itself. Try this:

csc /out:Test.exe Test.cs Entity.cs 

Or, maybe easier:

csc /out:Test.exe *.cs 

Also, read the related MSDN article.

Don't forget to add this code block in a method too:

namespace Demo { public class Entity { public void SomeMethod() /* here */ { Console.WriteLine("entity"); } } } 
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks its worked. It is for compile. Can you tell me how to run. I also want to pass string as argument to Main method.
Just run Test.exe argument1 argument2.
class Program { static void Main(string[] args) { Entity entity = new Entity(); System.Runtime.Remoting.ObjectHandle objectHandler = Activator.CreateInstance(null, "ConsoleApplication1.Employee"); object obj = objectHandler.Unwrap(); Console.WriteLine(obj); System.IO.File.WriteAllText(@"D:\Reshma\Command\Demo\Text.cs", obj.ToString()); }
What is your question?
I want to run two files through command prompt with pass argument as a class name to Main method. With this third class file is also available in same folder.
|
1

you need to provide name of all classes explicitly. so in your case you will execute

csc.exe /out:ExecutableName.exe Entity.cs Test.cs 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.