Simple C# question

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);
        }


    }
}





Works fine when I separate out the Cat class. Show your Cat.cs file.

Sorry for delayed response. Here is my Cat.cs file when the Cat class is separated out into it’s own file:




using System;

namespace HelloWorld
{

    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);
        }


    }

}







Try “public class Cat” .

with public class Cat, same error. I wonder if I incorrectly “added” Cat.cs to the project somehow. Could this be an error with how I’m using VS2010 ?

Screenshot

Wha’?! Move Cat.cs to “Hello World” project. Why did you put it in solution items?!

I thought I was adding it to the project… moved it and it works. Sorry for the newbie-ness.