C
C#•9mo ago
Cloudheart

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
salty_pepper
salty_pepper•9mo ago
using xxx.namesapce I think inside your main For this type of question, you can quickly ask chatGPT to create an example
Cloudheart
CloudheartOP•9mo ago
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.
ZacharyPatten
ZacharyPatten•9mo ago
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?
ZacharyPatten
ZacharyPatten•9mo ago
for example, here is what it might look like:
No description
ZacharyPatten
ZacharyPatten•9mo ago
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 top
Cloudheart
CloudheartOP•9mo ago
Thanks, 🥹 How can i create the classfile and put it in the same solutions.
ZacharyPatten
ZacharyPatten•9mo ago
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
ZacharyPatten
ZacharyPatten•9mo ago
No description
ZacharyPatten
ZacharyPatten•9mo ago
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 build
Cloudheart
CloudheartOP•9mo ago
Last question, my teacher had added the dipendancy, should i must do it?
ZacharyPatten
ZacharyPatten•9mo ago
It isn't clear what you are asking. Can you rephrase?
Cloudheart
CloudheartOP•9mo ago
My teacher set a dipendancy somewhere in this "process", if i don't do it will i get some errors?
ZacharyPatten
ZacharyPatten•9mo ago
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 do
Cloudheart
CloudheartOP•9mo ago
He right click on dipendancy and so something that i don't remember. I just skip ahead to much, maybe
Angius
Angius•9mo ago
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
ZacharyPatten
ZacharyPatten•9mo ago
derpendency :trollface:
Cloudheart
CloudheartOP•9mo ago
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.
Angius
Angius•9mo ago
You don't add new classes via dependencies
ZacharyPatten
ZacharyPatten•9mo ago
you don't have to add a reference to C# code files
Angius
Angius•9mo ago
So, yeah, no wonder you don't see it there
ZacharyPatten
ZacharyPatten•9mo ago
references are for when you want one project to use code from another project
Angius
Angius•9mo ago
// Foo.cs
namespace MyApp;

public class Foo
{
}
// Foo.cs
namespace MyApp;

public class Foo
{
}
// Things/Baz.cs
namespace MyApp.Things;

public class Baz
{
}
// Things/Baz.cs
namespace MyApp.Things;

public class Baz
{
}
// Bar.cs
namespace MyApp;

using MyApp.Things;
// we need to import the namespace

public class Bar
{
public Foo Thingamajig { get; set; }
// ^ same namespace, so no need to do anything special

public Baz AnotherThingy { get; set; }
// ^ different namespace, so we need the `using`
}
// Bar.cs
namespace MyApp;

using MyApp.Things;
// we need to import the namespace

public class Bar
{
public Foo Thingamajig { get; set; }
// ^ same namespace, so no need to do anything special

public Baz AnotherThingy { get; set; }
// ^ different namespace, so we need the `using`
}
This is how multiple classes in multiple files work in a single project
ZacharyPatten
ZacharyPatten•9mo ago
hmmm... im trying to upload an example.. but discord is failing to let me post screenshots at the monent..
Cloudheart
CloudheartOP•9mo ago
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.
Angius
Angius•9mo ago
Sure, maybe he did add some dependency But we have no way of knowing what dependency it was
Cloudheart
CloudheartOP•9mo ago
Really thanks for the help
ZacharyPatten
ZacharyPatten•9mo ago
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
Want results from more Discord servers?
Add your server