Help to import class.
Hi i'm a super noob and i need help. I had done my first console app with C#, so i wanna put my class in an external classfile and then import in my main. How to do it?
I'm using visual studio community editions.
27 Replies
using xxx.namesapce I think
inside your main
For this type of question, you can quickly ask chatGPT to create an example
I had tried but i failed, so this is why i' m asking here. I don't know how to make and where make a class file, set dependecy and how to import.
you don't have to import class files. as long as your class file is included in the project you can access it.
can you provide more info? perhaps a screenshot of your solution explorer?
for example, here is what it might look like:
in that screenshot there are two files:
Program.cs
and Class1.cs
. Both files in in the ConsoleApp3
project
Program.cs
can see Class1
from Class1.cs
. there is a using ConsoleApp3;
, but that isn't actually required
could have done ConsoleApp3.Class1 class1 = new();
instead of having using ConsoleApp3;
at the topThanks, 🥹 How can i create the classfile and put it in the same solutions.
a "solution" is a collection of "projects"
a "project" is a collection of files that are all intended to be built together
in order to add a class files to your project, just right click your project and select add and C# class
or you could simply add a file in the directory of your project
if you are using .NET (which you should be) then simply adding a
xxx.cs
file in the directory of your project will automatically be pulled into the buildLast question, my teacher had added the dipendancy, should i must do it?
It isn't clear what you are asking. Can you rephrase?
My teacher set a dipendancy somewhere in this "process", if i don't do it will i get some errors?
That isn't enough information for us to help you. You are mentioning that your teacher provided you a "dependency" but we need to know more about what your teacher provided you in order to help.
Did your teacher provided you a
Xxx.dll
file? (a file ending in ".dll")
Did your teacher provide you some C# code? In which case they probably just intend you to copy-paste the code into your own?
What did your teacher provide you and what are you trying to make?
feel free to share the instructions provided by your teacher (the assignment). we won't do your homework for you 😛 but that will help us guide you on what your teacher is intending for you to doHe right click on dipendancy and so something that i don't remember. I just skip ahead to much, maybe
Right-clicking on the dependencies in Visual Studio might lead to adding a dependency, yes
We have no way of knowing what that dependency is, though
Nor if it's really needed for your current project, or was it just your teacher showing you how to do it if need be
derpendency :trollface:
if i Right click on dependencies and choose add project reference, i don't see my file class. Anyway the program works without any errors.
You don't add new classes via dependencies
you don't have to add a reference to C# code files
So, yeah, no wonder you don't see it there
references are for when you want one project to use code from another project
This is how multiple classes in multiple files work in a single project
hmmm... im trying to upload an example.. but discord is failing to let me post screenshots at the monent..
Yes but i'm sure that he click on dependencies and set something. Anyway i'll check it in three days when i can see the lesson.
Sure, maybe he did add some dependency
But we have no way of knowing what dependency it was
Really thanks for the help
finally... damn discord is struggling today
anyways... above is an image/example of when you would use a "referece" (aka a "dependency").
Class1
is in the ClassLibrary1
project, but if we want to use Class1
from the ConsoleApp3
project, because it is in a seperate project we have to add a reference to ClassLibrary1
from ConsoleApp3