C
C#10mo ago
MorryMar

files

how do i create a new c# file to code in?
80 Replies
Angius
Angius10mo ago
You just... do? MyCoolNewFile.cs
MorryMar
MorryMarOP10mo ago
i just did 1
No description
MorryMar
MorryMarOP10mo ago
but why is there a error
Angius
Angius10mo ago
Well, what does it say?
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
MorryMar
MorryMarOP10mo ago
No description
MorryMar
MorryMarOP10mo ago
so for every code file i need to make a new project?
Angius
Angius10mo ago
What does the error say For every program you need a new project
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
MorryMar
MorryMarOP10mo ago
No description
Angius
Angius10mo ago
You can have many files within one project, yes
MorryMar
MorryMarOP10mo ago
how then
Angius
Angius10mo ago
But those files must be a part of the same program
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
Angius
Angius10mo ago
And only the entry file can have top-level code All other files must be classes (or structs or records or enums)
MorryMar
MorryMarOP10mo ago
oh well thats a bit sad
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
MorryMar
MorryMarOP10mo ago
ok thanks
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
MorryMar
MorryMarOP10mo ago
okok thanks
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
MorryMar
MorryMarOP10mo ago
but then how do i launch a specific code?
No description
MorryMar
MorryMarOP10mo ago
when i launch it launches the main 1 or do i have to invoke it in the main one?
Angius
Angius10mo ago
You launch the project That executes your entry file There can only ever be a single entry point in a project So... you don't
MorryMar
MorryMarOP10mo ago
well b r u h my brain is dying
Angius
Angius10mo ago
If you want to create a new program, create a new project You can use some tricks, of course
MorryMar
MorryMarOP10mo ago
like i wanted to make a folder inside it all the codes i have made within a course
Angius
Angius10mo ago
For example something like
// ProgramOne.cs
static class Foo
{
public static void Run()
{
// some code
}
}
// ProgramOne.cs
static class Foo
{
public static void Run()
{
// some code
}
}
// ProgramTwo.cs
static class Bar
{
public static void Run()
{
// some code
}
}
// ProgramTwo.cs
static class Bar
{
public static void Run()
{
// some code
}
}
// Program.cs
Console.WriteLine("Select program to run");
switch (Console.ReadLine())
{
case "one":
Foo.Run();
break;
case "two":
Bar.Run();
break;
default:
Console.WriteLine("No such program");
break;
}
// Program.cs
Console.WriteLine("Select program to run");
switch (Console.ReadLine())
{
case "one":
Foo.Run();
break;
case "two":
Bar.Run();
break;
default:
Console.WriteLine("No such program");
break;
}
z0mb
z0mb10mo ago
each "codes" probably equates to the need for an individual project for each, if they are all simple console applications. from what I am gathering.
Angius
Angius10mo ago
Yeah, most likely
MorryMar
MorryMarOP10mo ago
yea
Angius
Angius10mo ago
You can group multiple projects into a single solution if you want
MorryMar
MorryMarOP10mo ago
how o
Angius
Angius10mo ago
dotnet new sln -n MyCoolSolution dotnet sln MyCoolSolution.sln add MyCoolProject/MyCoolProject.csproj First command creates a solution file Second adds a project to the solution The structure would be like so
MyCoolThing
|— MyCoolThing.sln
|— ProjectOne
| |— ProjectOne.csproj
| \— Program.cs
|— ProjectTwo
| |— ProjectTwo.csproj
| \— Program.cs
MyCoolThing
|— MyCoolThing.sln
|— ProjectOne
| |— ProjectOne.csproj
| \— Program.cs
|— ProjectTwo
| |— ProjectTwo.csproj
| \— Program.cs
MorryMar
MorryMarOP10mo ago
o okokokok thanks
z0mb
z0mb10mo ago
To add onto that, when you try to run it you just need to specify which project you are trying to run, as there are obviously multiple.
MorryMar
MorryMarOP10mo ago
// Program.cs
Console.WriteLine("Select program to run");
switch (Console.ReadLine())
{
case "one":
Foo.Run();
break;
case "two":
Bar.Run();
break;
default:
Console.WriteLine("No such program");
break;
}
// Program.cs
Console.WriteLine("Select program to run");
switch (Console.ReadLine())
{
case "one":
Foo.Run();
break;
case "two":
Bar.Run();
break;
default:
Console.WriteLine("No such program");
break;
}
with this right?
Angius
Angius10mo ago
No This is a workaround if you wanted to keep just a single project With mutliple projects you... run the given project cd ProjectOne && dotnet run is the easiest IMHO
z0mb
z0mb10mo ago
agreed ^
Angius
Angius10mo ago
But you can dotnet run --project ProjectOne/ProjectOne.csproj From the solution level
z0mb
z0mb10mo ago
Alternatively if you have Visual Studio Community Edition or something of the likes you can easily select the startup project from a GUI. I don't typically use vscode otuside of front end stuff, but there's probably a way there as well
MorryMar
MorryMarOP10mo ago
man thats alot of stuff
MorryMar
MorryMarOP10mo ago
but now i get the idea
No description
MorryMar
MorryMarOP10mo ago
thanks :D
z0mb
z0mb10mo ago
No need to have Program2.cs and Program.cs. If you create two different csproj you can have Program.cs that is associated to each project like @ZZZZZZZZZZZZZZZZZZZZZZZZZ displayed This is a pretty standard approach and should be followed That way you don't end up with Program49.cs at some point, lol
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
MorryMar
MorryMarOP10mo ago
so like u dont have to create an extra file to put the same code ? but copy and paste it into another :Ok:
z0mb
z0mb10mo ago
I'm not sure I follow the question
MorryMar
MorryMarOP10mo ago
like u dont manually make a new file but the vsc does it for u man i suck at explaining stuff
z0mb
z0mb10mo ago
If you create a project within the sln, it should provide you with a Program.cs file tied to that project you created. There's two different approaches you can take. 1) One sln, multiple projects (each with their own Program.cs file). 2) One sln, one project, multiple classes, and your Program.Cs controls which one you wish to run.
MorryMar
MorryMarOP10mo ago
hm ok i think all of those methods come to me in the future right xD bc it will be hard to remember all of them
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
MorryMar
MorryMarOP10mo ago
yea but i think i understood enough to use them thanks for the help sorry im stupid lol
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
z0mb
z0mb10mo ago
So really at this point you just need to decide which approach you want to take, and if you have questions we can help with the decided upon approach. But we need to know which one you are wishing to proceed with
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
MorryMar
MorryMarOP10mo ago
the most useful to be honest to follow a course is the point 2
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
MorryMar
MorryMarOP10mo ago
to not get stuck in tutorial hell?
z0mb
z0mb10mo ago
If you wish to start with approach 2 that will work, so you have a sln created with a single project already?
MorryMar
MorryMarOP10mo ago
:kekw:
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
MorryMar
MorryMarOP10mo ago
if u mean like this yes
No description
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
MorryMar
MorryMarOP10mo ago
yea im tryna add new stuff to experiment or add what i have learnt from the course like make a shape and stuff
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
z0mb
z0mb10mo ago
So with this, you only need one Program.cs then you can create a new class that will be resposnbile for each lesson. So you may have: Program.cs Lesson1.cs Lesson2.cs Lesson3.cs etc... Or name them however you see fit It may make sense to put like lessons into folders as well, if you'd like
MorryMar
MorryMarOP10mo ago
wdym? like every cs file into a folder?
z0mb
z0mb10mo ago
Just providing options, but maybe something like this...
MyLessons
|— MyLessons.sln
|— Loops
| \— ForLoop.cs
| \— WhileLoop.cs
|— Files
| \— OpenFile.cs
| \— ReadFile.cs
MyLessons
|— MyLessons.sln
|— Loops
| \— ForLoop.cs
| \— WhileLoop.cs
|— Files
| \— OpenFile.cs
| \— ReadFile.cs
Or however your course is structured.
MorryMar
MorryMarOP10mo ago
Bro Code
YouTube
C# Full Course for free 🎮
C# tutorial beginners full course (C# for Unity) #C# #tutorial #Unity ⭐️Time Stamps⭐️ #1 (00:00:00) C# tutorial for beginners #2 (00:06:30) output 💬 #3 (00:10:48) variables ✖️ #4 (00:19:32) constants π #5 (00:20:35) type casting 💱 #6 (00:27:49) user input ⌨️ #7 (00:31:24) arithmetic operators 🧮 #8 (00:35:54) Math class 📏 #9 (0...
MorryMar
MorryMarOP10mo ago
hes teaching and using a different version
z0mb
z0mb10mo ago
Really how you organize it is up to you, I can't decide that for you, but just providing the option that you can create a folder to hold like items together. Ultimately each lesson is it's own .cs file and can be organized as you see fit
MorryMar
MorryMarOP10mo ago
okii
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
MorryMar
MorryMarOP10mo ago
after the one im currently watching sure
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
MorryMar
MorryMarOP10mo ago
bro code is good tho
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
MorryMar
MorryMarOP10mo ago
wdym whats .net
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server