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.
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.
60 Replies
hi
hiya
first of all put public infront of a class name
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
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
Ok, my second file now looks like this:
namespace ConsoleApp1
{
public class Car
{
public string color = "red";
}
}
ye
or remove it from program
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
that makes sense
like using system
the class system has
is Console
and the function console has is WriteLine
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
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?
yes
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
Namespaces are very important.
such as System
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....
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....
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 timeThanks 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
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?lemme try that
If you do, make sure to put
using ConsoleApp1.Models
at the top of your main file so the file uses the class correctly.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?"
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?"
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.
Can you show a screenshot of your "solution explorer" window in Visual Studio?
because what you have should work
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 🙂
VB? You mean VS? :catlaugh:
usually is docked on the right side of the screen, iirc

example

Right so your second file isnt in the project
its... somewhere else :p
haha ok
it looks like using this software is my issue. Could you reccomend any resources for intro to using VB?
rightclick on "ConsoleApp1" in the solution explorer and do "Add -> new Class"
will do

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 Studiosorry, yes, VS
And generally Visual Studio is a very good IDE. it would not have any issues that other IDEs do not have

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?

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 confusionI 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?
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 beforeI 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
i smell .net framework
can you open the project properties?
rigth click project, all the way at the bottom
yes, I'm in the project properties now
does it say anything about your target framework?
.NET 8.0
oh, well nevermind then
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 🙂
i thought .net projects include all source files by default
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 😄
Yes
I believe so
In .NET it is the reverse. Rather than indicating what to compile, you indicate what not to compile