C
C#•2mo ago
CTMWood

Beginner problem with Classes

I am currently following the C# tutorial on w3schools.com and doing the exercises in Visual Studio at the same time so I can actually practice writing code a bit rather than just reading. The part of the tutorial I am stuck on is this bottom of this page on Classes: https://www.w3schools.com/cs/cs_classes_multi.php I have created 2 separate .cs files in Visual Studio. The first contains the following code: namespace ConsoleApp1 { class Program {
static void Main(string[] args) { Car myObj = new Car(); Console.WriteLine(myObj.color); } } } and the second file contains the following code: class Car { public string color = "red"; } The error I am receiving is "The type or namespace 'Car' could not be found (are you missing a using directive or an assembly referece?)" I believe I have copied the tutorial code correctly and am unclear on why it isn't doing what it 'should'. My current thinking is that it's something about VS rather than the code. Any help gratefully received.
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
60 Replies
CTMWood
CTMWoodOP•2mo ago
hiya
𓀠𓀒𓀡𓀬
first of all put public infront of a class name
CTMWood
CTMWoodOP•2mo ago
Do you mean so that my second file will be like this?: public class Car { public string color = "red"; } If yes, I have just tried this but am still getting the same error
𓀠𓀒𓀡𓀬
yes than it has to do with namespace
CTMWood
CTMWoodOP•2mo ago
thanks for the help btw 🙂
𓀠𓀒𓀡𓀬
np it has to do with namespace ConsoleApp1 you have to give car the namespace as in program.cs or remove it from program
CTMWood
CTMWoodOP•2mo ago
Ok, my second file now looks like this: namespace ConsoleApp1 { public class Car { public string color = "red"; } }
𓀠𓀒𓀡𓀬
ye or remove it from program
CTMWood
CTMWoodOP•2mo ago
lemme try running it ok, same error. I'll try your suggesting to 'remove it from program' now. Would you mind elaborating what you mean by this? you mean remove the namespace from the program file ?
𓀠𓀒𓀡𓀬
lets say namespace holds classes for example if i want too use a class in a other file i have to use using namespace in this case i would need a namespace for car and on top of program using namespaceofcar
CTMWood
CTMWoodOP•2mo ago
that makes sense
𓀠𓀒𓀡𓀬
like using system the class system has is Console and the function console has is WriteLine
CTMWood
CTMWoodOP•2mo ago
but my file doesn't have 'using system' in it so why do the System classes work?
𓀠𓀒𓀡𓀬
true you have to use System.Console.WriteLine() but with using System on top of your file you can do Console.WriteLine() so System is the namespace Console the class and WriteLine() the function
CTMWood
CTMWoodOP•2mo ago
ok, I think I understand. So I need to state what namespace my Car class is in on my second file. And then do a using directive of that namespace in my first file? this 'links' the two files?
CTMWood
CTMWoodOP•2mo ago
great, lemme give that a go 🙂
𓀠𓀒𓀡𓀬
but you dont need to do that tho its preference i almost never use namespaces but your supposed to use them they use them in programs they build
FusedQyou
FusedQyou•2mo ago
Namespaces are very important.
𓀠𓀒𓀡𓀬
such as System
CTMWood
CTMWoodOP•2mo ago
ok, my two files now look like this: using System; using CarSpace; namespace ConsoleApp1 { class Program {
static void Main(string[] args) { Car myObj = new Car(); Console.WriteLine(myObj.color); } } } and : namespace CarSpace {
public class Car { public string color = "red"; } } It's strange how the tutorial hasn't covered any of this yet, and the code in the tutorial just doesn't work....
FusedQyou
FusedQyou•2mo ago
When using different namespaces, you need to indicate in the file which ones to use since it otherwise becomes confusing what you refer to. Like the Console class, which you used to call the WriteLine method, comes from the System namespace. If you have two files, they both need a namespace. If one file uses a different namespace than the other, you must reference this using the using syntax at the top of your file. Alternatively, prefix the namespace behind the action you are undertaking, such as System.Console.WriteLine Accessibillity modifiers apply to this. If your class car comes from a different namespace, it must either be internal or public like you have now. When private or unspecified, you can't use it in other namespaces This means, if I'm correct, that if you were to rename namespace CarSpace to namespace ConsoleApp1, you can then keep the class private despite it being in a different file. This is because the class will be private to the scope of the namespace However, don't do this. The general rule is you use the same namespace as the order of your files. If the file is in the root of the project, assuming the project is called ConsoleApp1, the namespace is ConsoleApp1. If the file is in /models, the namespace is now ConsoleApp1.Models Don't misuse namespaces, don't ignore them for simplicity. If you just throw everything in a single namespace you have to start using quirky names to avoid conflicts which is a waste of time
CTMWood
CTMWoodOP•2mo ago
Thanks for this. I'm going to read it over a few times to fully understand. For now though, what do you think the error in my code is, because it's still giving the same error
FusedQyou
FusedQyou•2mo ago
In this case, I suggest you just put it in namespace ConsoleApp1, or you make a Models/ folder and change the namespace to ConsoleApp1.Models Which error? Can you share it?
CTMWood
CTMWoodOP•2mo ago
lemme try that
FusedQyou
FusedQyou•2mo ago
If you do, make sure to put using ConsoleApp1.Models at the top of your main file so the file uses the class correctly.
CTMWood
CTMWoodOP•2mo ago
I'll try your way without create new folders first Ok, here is the current state of my 2 files: Program.cs contains the following code: using System; namespace ConsoleApp1 { class Program {
static void Main(string[] args) { Car myObj = new Car(); Console.WriteLine(myObj.color); } } } and my second file called Class2.cs contains the following code: namespace ConsoleApp1 { public class Car { public string color = "red"; } } The error I am receiving is "The type or namespace name 'Car' could not be found (are you missing a using directive or and assembly reference?"
FusedQyou
FusedQyou•2mo ago
This is correct. It also works fine for me after copying your code Make sure that you didn't make a typo and the files are in the same project.
Pobiega
Pobiega•2mo ago
Can you show a screenshot of your "solution explorer" window in Visual Studio? because what you have should work
CTMWood
CTMWoodOP•2mo ago
Ah, I did suspect it was a VB thing rather than a code issue. Let me look for this solution explorer you refer to and screenshot it 🙂
FusedQyou
FusedQyou•2mo ago
VB? You mean VS? :catlaugh:
Pobiega
Pobiega•2mo ago
usually is docked on the right side of the screen, iirc
Pobiega
Pobiega•2mo ago
No description
Pobiega
Pobiega•2mo ago
example
CTMWood
CTMWoodOP•2mo ago
No description
Pobiega
Pobiega•2mo ago
Right so your second file isnt in the project its... somewhere else :p
CTMWood
CTMWoodOP•2mo ago
haha ok it looks like using this software is my issue. Could you reccomend any resources for intro to using VB?
Pobiega
Pobiega•2mo ago
rightclick on "ConsoleApp1" in the solution explorer and do "Add -> new Class"
CTMWood
CTMWoodOP•2mo ago
will do
Pobiega
Pobiega•2mo ago
No description
FusedQyou
FusedQyou•2mo ago
Is the Car file you made available if you check the project through file explorer? Generally it should show unless you explicitly deleted it from the project view. You can otherwise readd it with Add -> Existing item Again, VS and not VB, right? VB usually refers to Visual Basic which is something different. The editor you use is called Visual Studio
CTMWood
CTMWoodOP•2mo ago
sorry, yes, VS
FusedQyou
FusedQyou•2mo ago
And generally Visual Studio is a very good IDE. it would not have any issues that other IDEs do not have
CTMWood
CTMWoodOP•2mo ago
No description
FusedQyou
FusedQyou•2mo ago
Otherwise you can also use Visual Studio Code or Jetbrains Rider, but VS is the most user friendly And if you go into the folder?
CTMWood
CTMWoodOP•2mo ago
No description
FusedQyou
FusedQyou•2mo ago
So it's probably Class1 then If you readd it it should work Either click and hover the file manually into the VS list of files, or right click the project and do Add -> Existing Item I do suggest you name the files the same as the class that is contained inside, to avoid confusion
CTMWood
CTMWoodOP•2mo ago
I think that Class1.cs file is the file you just asked me to create, and it has nothing in it so I just move the code into there?
FusedQyou
FusedQyou•2mo ago
Then you likely deleted it by accident. Just delete the file then You can, just make sure to match the namespace in this case. Or reference the namespace with the using keyword like explained before
CTMWood
CTMWoodOP•2mo ago
I think I get it, I'm going to go try to get it working with what you've suggested. This solution explorer definitely feels like the piece of the puzzle I was missing. And I appreciate the tips on naming files too. Many thanks @FusedQyou @Pobiega@𓀠𓀒𓀡𓀬 Thank you all for your help on this one, much appreciated
ero
ero•2mo ago
i smell .net framework can you open the project properties? rigth click project, all the way at the bottom
CTMWood
CTMWoodOP•2mo ago
yes, I'm in the project properties now
ero
ero•2mo ago
does it say anything about your target framework?
CTMWood
CTMWoodOP•2mo ago
.NET 8.0
ero
ero•2mo ago
oh, well nevermind then
CTMWood
CTMWoodOP•2mo ago
Yeah I think this is solved. It's an issue with my files not being in the right place in the project folder. I just need to sort that and I'm sure it will work 🙂
ero
ero•2mo ago
i thought .net projects include all source files by default
CTMWood
CTMWoodOP•2mo ago
If I understand correctly, the issue was that I created 2 files, thinking they were in the same project. But by some newbie fuckery I've gone and put them in some random locations and they couldn't see each other. If I fix that I'm gravy But I will surely come back here if that still doesn't work 😄
FusedQyou
FusedQyou•2mo ago
Yes I believe so In .NET it is the reverse. Rather than indicating what to compile, you indicate what not to compile

Did you find this page helpful?