C#C
C#11mo ago
114 replies
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.
Preview image
Was this page helpful?