Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

[C#]Protection level & class'

Master Summoner
Joined
Oct 9, 2008
Messages
572
Reaction score
14
The thing is that I'm reading Big Brother's free e-book on C# for sharp kids, which I should understand more or less, but I'm headed into a problem that the book doesn't describe. I've created a class called Animal, just like in the book, and added some strings, integers and booleans, and then I tried to make another class, in a namespace, where I try to make a new Animal object, but I'm getting this error: "'Animal.kindOfAnimal' is inaccessable due to its protection level"

I tried to play around with it, but it doesn't seem to change, and yes I'm utterly new to C#.
Heres my source:
PHP:
using System;

    class Animal
    {
        string kindOfAnimal;
        string name;
        int numOfLegs;
        int heigth;
        int length;
        int age;
        bool hasTail;
    }


namespace progRun
{
    class run
    {
        public static void Main()
        {
            Animal Cat;
            Cat = new Animal();

            Cat.kindOfAnimal = "Cat";
        }
    }
}
 
Joined
May 17, 2007
Messages
2,474
Reaction score
681
Code:
using System;

namespace progRun
{
    public class run
    {
        public static void Main()
        {
            Animal Cat;
            Cat = new Animal();

            Cat.kindOfAnimal = "Cat";
        }
    }

    public class Animal
    {
        public string kindOfAnimal;
        public string name;
        public int numOfLegs;
        public int heigth;
        public int length;
        public int age;
        public bool hasTail;
    }
}
 
Master Summoner
Joined
Oct 9, 2008
Messages
572
Reaction score
14
Thank you very much, AJ.

A few questions:
1) Why come does the Main() method come before my Animal class?
2) I assume that in order to get 'information' from other methods, they have to be in same namespace?
3) Making the class public, will it make them able to be used together, but not if one of them is protected/private?

Thanks a bunch! :)
 
Joined
May 17, 2007
Messages
2,474
Reaction score
681
1. doesn't matter the order of your classes, as they are just for user readable purposes because they are compiled into native code
2.no they don't, you just need to import or use the namespace. if it's in the same namespace, you dont need to import/use
so you could make 2 classes - 1. Program.cs (or w/e), 2. Animal

and you can call from animal:
Code:
private Animal mAnimal = new Animal();
Code:
mAnimal.kindOfAnimal = "Cat";

etc etc

3. if you don't make the class public, some classes (may) give error saying class1 is less accessible than class2 etc

private = only for that class usage
protected = private, but can be accessed by other classes with privileges

 
Back
Top