CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2013
    Posts
    2

    Instance Problem

    Hey all, I'm fairly new to C# and come from a Java background. In this project I cant seem to instantiate an object of the class Dog from the TestAnimal class in a separate file. Can anyone point me in the right direction?

    Thanks in advance!

    Dog Class
    Code:
    using System;
    
    namespace Application
    {
    	public class Animal
    	{
    		// Local variables
    		private string name;
    		private int age;
    		private string talk;
    
    		// Animal Constructor
    		public Animal(string name, int age, string talk)
    		{
    			setName(name);
    			setAge(age);
    			setTalk(talk);
    		}
    
    		// Setters
    		public void setName (string name)
    		{
    			this.name = name;
    		}
    		public void setAge (int age)
    		{
    			this.age = age;
    		}
    		public void setTalk (string talk)
    		{
    			this.talk = talk;
    		}
    
    		// Getters
    		public string getName()
    		{
    			return name;
    		}
    		public int getAge()
    		{
    			return age;
    		}
    		public string getTalk ()
    		{
    			return talk;
    		}
    	}
    
    	// Dog inherits from Animal
    	public class Dog : Animal
    	{
    		public Dog (string name, int age, string talk) : base(name,age,talk)
    		{
    		}
    
    		public void dogDetails()
    		{
    			Console.WriteLine("Name: " + getName() + "\n\nAge: " + getAge () + "\n\nRex: " + getTalk());
    		}
    	}
    }
    TestAnimal Class
    Code:
    using System;
    
    namespace Application
    {
    
    	public class TestAnimal
    	{
    		public static void Main(string[] args)
    		{
    			Dog d = new Dog("Rex", 3, "BARKS!!");    // error saying namespace Dog cannot be found.
    			d.dogDetails();
    			
    		}
    	}
    }

  2. #2
    Join Date
    Oct 2004
    Location
    Rocket City
    Posts
    220

    Re: Instance Problem

    Isn't 'Application' already a namespace used by VisualStudio?

  3. #3
    Join Date
    Sep 2013
    Posts
    2

    Re: Instance Problem

    Sorry I should have stated that I am coding on a Mac and using Mono Develop as my IDE for C#. And "Application" was the namespace generated automatically for me by Mono Develop.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Instance Problem

    Don't know how the Mono environment works, but is the file where the Animal and Dog classes are defined included in your project? In Visual Studio, when you create a new class it automatically includes the file in your project. Make sure that is the case in your environment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured