Jones
Jones

Reputation: 13

c# obtaining property values from object

I am trying to use a method to create an object and return that object to main(). Once in main I want to be able to access the properties of that object such as print Holly.age. VS does not recognize the object Holly in main() to have any properties. Is anyone able to tell what I am doing wrong or let me know if it's not something possible in c#.

Thanks in advance to anyone who gives there time to help.

class program
{ 

    static void Main(string[] args)
    {
        object Holly = createobject(Holly, 21);
        int hollyage = Holly.age; // This Line is the problem Holly.age is not recognized 
        // as anything

    }

    public static object createobject(string name, int i)
    {
    Cat Holly = new Cat(name, i);
    return Holly;
    }

    public class Cat
    {
    public string name;
    public int age;
    public string Name
        {
            get { return this.name; }
            set { this.name = value; }
        }
    public int Age
        {
            get { return this.age; }
            set { this.age = value; }
        }
    public Cat(string name, int age)
        {
            this.name = name;
            this.age = age;
        }

}

Upvotes: 0

Views: 53

Answers (5)

Gilad Green
Gilad Green

Reputation: 37299

Your method createobject returns an object. So you must first cast it back into Cat before you can access Age:

int hollyage = ((Cat)Holly).Age;

In addition your call to the createobject is wrong. It requests a string. Change to:

createobject("Holly", 21);

The correct way to write your code would be:

static void Main(string[] args)
{
    //variable name in camelCase.
    //"Holly" is a string

    object holly = CreateObject("Holly", 21);

    //Casting from object to Cat so you can access Age property
    int hollyAge = ((Cat)holly).Age; 
}

//Function names EachWorkCapitalized
public static object CreateObject(string name, int i)
{
    Cat holly = new Cat(name, i);
    return holly;
}

public class Cat
{
    //If you implement your properties with the default get set behavior 
    //better use automatically implemented properties
    public string Name { get; set; }
    public int Age { get; set; }

    public Cat(string name, int age)
    {
        Name = name;
        Age = age;
    }
}

Upvotes: 1

Theo
Theo

Reputation: 885

You could always cast the object to the correct type to access it's members.

int hollyage = ((Cat)Holly).Age;

Upvotes: 0

Frits
Frits

Reputation: 314

The problem is that Holly is defined as an object class, and object classes do not have an age or Age property.

You should change the line as:

Cat Holly = createObject("Holly", 21) as Cat;

Then the intellisense will recognize the Holly variable as being of type Cat.

Upvotes: 0

uTeisT
uTeisT

Reputation: 2266

You don't actually need public static object createobject()

You can simply remove that and change your Main to:

    static void Main(string[] args)
    {
        Cat Holly = new Cat("Holly", 21);
        int hollyage = Holly.age; 
    }

Upvotes: 0

ChrisF
ChrisF

Reputation: 137128

VS does not recognize the object Holly in main() to have any properties

This is because you have declared Holly to be object rather than Cat, and similarly your create routine returns object rather than Cat.

To be able to use any of the properties of Cat you need to cast Holly back to Cat or better still, return Cat from your create routine. Unless you intend to extend your create routine to do more, you don't really need it and can simply do:

Cat Holly = new Cat("Holly", 21);

You also have both public fields and public properties in your Cat class. The fields should be made private, but that's not the cause of the problem.

Upvotes: 2

Related Questions