What project type to choose, VS
Hello, I'm very new to C#, currently studying it on school, and I can't choose the right project type for my work.
We have 12 different exercises that need to be done in one project, I've tried doing a Console App project but since I need multiple files, I need multiply for loops and mains and it doesn't let me do more than one.
I've tried a Class project as well but it doesn't debug because its a library.
What project should I choose?
20 Replies
Console
And if it has to be one project – which is an idiotic requirement – make some exercise selector or some such
I had the same requirement in many classes. I would typically put each excercice in a static class so it can be its own file. Later on when actual classes are involved, I would group then in folder where they're introduced. Then just have main call the right functions for the one to test.
How can I do that then? or how should I research it on google?
Right click the project>Add item>C# class
oh.
Then add the keyword
static
before class
. It should look something like this
Then put whatever you need in there. However to add instructions, you need a method. Then in Program.cs, you can run it by calling Run. Ex01.Run();
.
To run exercise 2: Ex02.Run();
and so on assuming you stick to that naming scheme for each class.
Alternatively if you don't mind having everything in one file, you can define the methods directly in Program.cs
As you mentionned, a project can only have one Main as it's the entry-point of the program. It's up to the Main to direct execution to the right exercise.so the Main is the one that says which program is being run at the time right
Yes. The system has to know what part of your code to run, so Main is the standard, defining the first instructions to run and go from there.
I think you have explained it before, but I couldn't understand it, I apologise for that.
Right now I have 2 files, the first being the 'Program.cs' and the second is the class I just created. At the moment the second is the one that executable. How can I make them both be executable at any time?
Main should remain in Program.cs under the Program class. Each exercise has a Run method that's called from Main.
So I would rename Ex02 to Program and create a new static class in a third file for holding Ex02
Then you can replace the body of Main to calls to what you want to test.
So on 'Program.cs' I should only have those "Ex01.Run();' and do my exercises on classes?
Yes
In that case though, you still need to rename the class in Program.cs to avoid a conflict with the Ex02 class in the new file.
How should my 'Program.cs' look like then? I have no idea what to type.
I'd prolly go for something more fancy myself
But this'll do
Thanks man.
That's another way that's dynamic at runtime.
No need for something fancy, just something that works properly that me and my teacher can understand.
It's working now, thanks @TheBoxyBear @ZZZZZZZZZZZZZZZZZZZZZZZZZ for the help, you guys saved me a couple hours maybe.
Sorry for the over explanation, have a great day/night.
:Ok:
Np
i work with vscode not visual studio and i'm wondering how to create a class in a dependent file from the cs program and use it's functions