C
C#3mo ago
noobii

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
Angius
Angius3mo ago
Console And if it has to be one project – which is an idiotic requirement – make some exercise selector or some such
TheBoxyBear
TheBoxyBear3mo ago
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.
noobii
noobiiOP3mo ago
How can I do that then? or how should I research it on google?
TheBoxyBear
TheBoxyBear3mo ago
Right click the project>Add item>C# class
noobii
noobiiOP3mo ago
oh.
TheBoxyBear
TheBoxyBear3mo ago
Then add the keyword static before class. It should look something like this
namespace MyProject;

static class Ex01
{

}
namespace MyProject;

static class Ex01
{

}
Then put whatever you need in there. However to add instructions, you need a method.
namespace MyProject;

static class Ex01
{
public void Run()
{
// Code here
}
}
namespace MyProject;

static class Ex01
{
public void Run()
{
// Code here
}
}
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
using System;
// Other usings

void Ex01()
{
// Ex 1 code
}

void Ex02()
{
// Ex 2 code
}

// Uncomment based on which one to run
//Ex01();
//Ex02();
using System;
// Other usings

void Ex01()
{
// Ex 1 code
}

void Ex02()
{
// Ex 2 code
}

// Uncomment based on which one to run
//Ex01();
//Ex02();
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.
noobii
noobiiOP3mo ago
so the Main is the one that says which program is being run at the time right
TheBoxyBear
TheBoxyBear3mo ago
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.
noobii
noobiiOP3mo ago
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?
No description
No description
TheBoxyBear
TheBoxyBear3mo ago
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.
Ex01.Run();
Ex02.Run();
// ...
Ex01.Run();
Ex02.Run();
// ...
noobii
noobiiOP3mo ago
So on 'Program.cs' I should only have those "Ex01.Run();' and do my exercises on classes?
TheBoxyBear
TheBoxyBear3mo ago
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.
noobii
noobiiOP3mo ago
How should my 'Program.cs' look like then? I have no idea what to type.
Angius
Angius3mo ago
// Program.cs
Console.WriteLine("Pick exercise");
switch (Console.ReadLine())
{
case "1":
ExOne.Run();
break;
case "2":
ExTwo.Run();
break;
//...
default:
Console.WriteLine("No such exercise");
break;
}
// Program.cs
Console.WriteLine("Pick exercise");
switch (Console.ReadLine())
{
case "1":
ExOne.Run();
break;
case "2":
ExTwo.Run();
break;
//...
default:
Console.WriteLine("No such exercise");
break;
}
// ExOne.cs
public static class ExOne
{
public static void Run()
{
// exercise one goes here
}
}
// ExOne.cs
public static class ExOne
{
public static void Run()
{
// exercise one goes here
}
}
// ExTwo.cs
public static class ExTwo
{
public static void Run()
{
// exercise two goes here
}
}
// ExTwo.cs
public static class ExTwo
{
public static void Run()
{
// exercise two goes here
}
}
I'd prolly go for something more fancy myself But this'll do
noobii
noobiiOP3mo ago
Thanks man.
TheBoxyBear
TheBoxyBear3mo ago
That's another way that's dynamic at runtime.
noobii
noobiiOP3mo ago
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.
Angius
Angius3mo ago
:Ok:
TheBoxyBear
TheBoxyBear3mo ago
Np
MOUDJOU
MOUDJOU3mo ago
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
Want results from more Discord servers?
Add your server