C
C#2y ago
shawski.

✅ splitting project into multiple files

ive never done this before so im not sure where to begin. i am working on something and would like to split some features into multiple projects, i just dont know how. thank you!
35 Replies
Pobiega
Pobiega2y ago
Your title and your text dont seem to align. The title speaks of splitting a single project into multiple files. The text speaks of splitting features into multiple projects. What exactly are you trying to do, and what is your current codebase like?
shawski.
shawski.OP2y ago
i guess splitting it into features is what i want to do i would like to have some features split into multiple cs files so everything isnt all cluttered
Pobiega
Pobiega2y ago
Right. How do you define a feature, currently?
shawski.
shawski.OP2y ago
i just have one cs file with all my code i am gonna start working on another thing but would preferably do it somewhere else, while still being part of the same project
Pobiega
Pobiega2y ago
Okay Can you show me your current code?
shawski.
shawski.OP2y ago
Pobiega
Pobiega2y ago
oh, so its all just one giant method?
shawski.
shawski.OP2y ago
for the time being, yeah
Pobiega
Pobiega2y ago
Okay. So step 1, lets split this ping thing into its a new method in a new class so we separate our entrypoint from the ping
shawski.
shawski.OP2y ago
good idea
Pobiega
Pobiega2y ago
So make a new .cs file, maybe "PingCommand.cs"?
shawski.
shawski.OP2y ago
alr done
Pobiega
Pobiega2y ago
ok, and was there also a class created in that file?
shawski.
shawski.OP2y ago
yes
Pobiega
Pobiega2y ago
great. so lets make a new method in that class to host our code public static void Run(string[] args) should do the trick
shawski.
shawski.OP2y ago
alright done
Pobiega
Pobiega2y ago
now, lets cut all our code from GodyPing.cs public static void Main method into your new method
shawski.
shawski.OP2y ago
done
Pobiega
Pobiega2y ago
finally, lets make Main call the new method PingCommand.Run(args);
shawski.
shawski.OP2y ago
done
Pobiega
Pobiega2y ago
try running your program
shawski.
shawski.OP2y ago
Pobiega
Pobiega2y ago
okay, lets see the build errors
shawski.
shawski.OP2y ago
ahh i think i know why do i have to paste all the libraries into that new file? it is telling me it doesnt know what 'Ping' is
Pobiega
Pobiega2y ago
you mean the using ... statements? yeah
shawski.
shawski.OP2y ago
yes alr
1>------ Build started: Project: GodyPing, Configuration: Debug ------
1>C:\Users\sean\Documents\development\GodyPing.cs(11,13,11,20): error CS0103: The name 'PingCMD' does not exist in the current context

1>C:\Users\sean\Documents\development\GodyPing.cs(11,25,11,29): error CS0103: The name 'args' does not exist in the current context

1>C:\Users\sean\Documents\development\GodyPing.cs(9,27,9,31): warning CS1998: This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

1>C:\Users\sean\Documents\development\PingCMD.cs(39,21,39,25): error CS1061: 'Ping' does not contain a definition for 'Send' and no accessible extension method 'Send' accepting a first argument of type 'Ping' could be found (are you missing a using directive or an assembly reference?)
1>C:\Users\sean\Documents\development\PingCMD.cs(47,13,47,17): error CS1061: 'Ping' does not contain a definition for 'Send' and no accessible extension method 'Send' accepting a first argument of type 'Ping' could be found (are you missing a using directive or an assembly reference?)

1>C:\Users\sean\Documents\development\PingCMD.cs(48,106,48,115): warning CS8602: Dereference of a possibly null reference.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
1>------ Build started: Project: GodyPing, Configuration: Debug ------
1>C:\Users\sean\Documents\development\GodyPing.cs(11,13,11,20): error CS0103: The name 'PingCMD' does not exist in the current context

1>C:\Users\sean\Documents\development\GodyPing.cs(11,25,11,29): error CS0103: The name 'args' does not exist in the current context

1>C:\Users\sean\Documents\development\GodyPing.cs(9,27,9,31): warning CS1998: This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

1>C:\Users\sean\Documents\development\PingCMD.cs(39,21,39,25): error CS1061: 'Ping' does not contain a definition for 'Send' and no accessible extension method 'Send' accepting a first argument of type 'Ping' could be found (are you missing a using directive or an assembly reference?)
1>C:\Users\sean\Documents\development\PingCMD.cs(47,13,47,17): error CS1061: 'Ping' does not contain a definition for 'Send' and no accessible extension method 'Send' accepting a first argument of type 'Ping' could be found (are you missing a using directive or an assembly reference?)

1>C:\Users\sean\Documents\development\PingCMD.cs(48,106,48,115): warning CS8602: Dereference of a possibly null reference.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
i see a few errors i can fix
Pobiega
Pobiega2y ago
Looks like your main method was async in your original file so you'll need to make the new one async too public async Task Run(string[] args)
shawski.
shawski.OP2y ago
shawski.
shawski.OP2y ago
still saying 'Ping' doesn't contain a definition for 'Send'
Pobiega
Pobiega2y ago
then Pingisnt the same ping as it was before Its a bit hard to troubleshoot this without actually seeing the code. Do you want to screenshare?
shawski.
shawski.OP2y ago
sure, what call do you want to get in to?
Pobiega
Pobiega2y ago
#dev-vc-1
shawski.
shawski.OP2y ago
ty 😄
Pobiega
Pobiega2y ago
You can /close the thread if you don't have any further questions.
shawski.
shawski.OP2y ago
it says i dont have permission
Want results from more Discord servers?
Add your server