views:

376

answers:

5
+1  Q: 

C# Console Problem

Consider this class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Game.Items
{
    class Item
    {
        private string name;
        public string Name
        {
            get { return this.name; }
        }

        private string description;
        public string Description
        {
            get { return this.description; }
        }

        public Item(string name, string description)
        {
            this.name = name;
            this.description = description;
        }

        public override string ToString()
        {
            return this.name;
        }
    }
}

I then create a new object like this:

Item item1 = new Item("Item1", "Description...");

Now the problem, I cannot access properties of the object with the getter method, i.e. this doesn't work:

Console.WriteLine(item1.Name);
Console.WriteLine(item1.Description);
Console.ReadLine();

By "doesn't work" I mean when I click on "Start debugging", the console window appears but nothing is there, it's blank. However, this works fine:

Console.WriteLine(item1); // ToString()
Console.ReadLine();

What am I doing wrong?

Richard

+3  A: 

Works fine for me:

using System;

namespace Application
{
    class Test
    {
        static void Main()
        {
            Item item1 = new Item("Item1", "Description...");
            Console.WriteLine(item1.Name);
            Console.WriteLine(item1.Description);
        }
    }
}

(With your class there as well.)

When you say "this doesn't work" what exactly goes wrong?

Jon Skeet
Well, when I click "Start debugging", the console window appears but it is blank. Nothing is there.Thew two commands:Console.WriteLine(item1.Name);Console.WriteLine(item1.Description);They do nothing.Only this works (ToString()):Console.WriteLine(item1);
Richard Knop
Please add this information to your question (or someone with enough rep will sooner or later I guess ; (also try including the actual code and class/file/namespace structure used)
Oskar Duveborn
Given that my short but complete program above works fine, it sounds like you're doing something strange - please post a short but complete program which shows this odd behaviour.
Jon Skeet
A: 

Are you working your Main class in a project different than the Project where "Item" is contained? if so, then

  • Reference the other project
  • Mark your Item class as public

    public class Item { // your code }

Or you can try to access this way to see if the problem is related with namespaces

Console.WriteLine(Application.item1.Name);
Console.WriteLine(Application.item1.Description);

Also, put a pause after the execution, or else your console window will go without showing you anything

Console.ReadLine();

Check also the using clauses, according to last comment, you then have to use a using clause at the top of the file where you are trying to use the Item class

using Game;
Jhonny D. Cano -Leftware-
About the only thing I could think of as well, declaring the class itself public if used outside of the project namespace...
Oskar Duveborn
I have added the using clause and it still doesn't work :(
Richard Knop
are you getting an error?
Jhonny D. Cano -Leftware-
No, no error, no warnings.
Richard Knop
A: 

Both class Item and the main application class are in the same project.

Edit: yes of course I have Console.ReadLine(); there.

Richard Knop
Check if they are in the same namespace... namespace Application
Jhonny D. Cano -Leftware-
A: 

One more thing, the Item class is in the namespace Game.items (my project is called Game), could that be a problem?

Richard Knop
Yeah, you then have to use a using clause at the top of the file where you are trying to use the Item class using Game;
Jhonny D. Cano -Leftware-
A: 

Ok guys, thanks for your help, I have resolved the problem finally, my namespaces where messed up. Now it works :)

Richard Knop
You should accept the answer that helped you
Jhonny D. Cano -Leftware-