In Visual Studio 2010 Professional, I am testing out a very simple C# class named “Cat” with a basic driver class.
It works when I put the Cat class and the Program class in the same file under the same namespace.
However, when I try to separate the “Cat” class into it’s own file (cat.cs) and I also put everything within the HelloWorld namespace in that file, then I get a bunch of errors saying :
Error 1 The type or namespace name ‘Cat’ could not be found (are you missing a using directive or an assembly reference?)
This same approach, however, works in Java. You can just make sure a class is in the same package as the other files and it automatically “finds” the class when you run the program. What am I doing wrong?? I ‘added’ the Cat class to the project in VS2010, but it can’t seem to find the Cat class when I take it out of the first C# file.
HERE IS THE APPROACH THAT WORKS: (both classes dumped into the same file):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Cat frisky = new Cat("frisker", 40);
Console.WriteLine(frisky);
Console.ReadLine();
}
}
class Cat
{
private string myname;
public Cat() { }
public Cat(string name, int weight)
{
myname = name;
}
public override string ToString()
{
return ("This cat is named "+myname);
}
}
}