If I have

class Class1
    {
        string message { get; set; }
        int age { get; set; }

        public Class1(string msg, int ageof)
        {
            message = msg;

            age = ageof;
        
        }

and am passing the parameters from another class in to the constructor...

class Program
{
    static void Main()
    {

        Class1 type = new Class1("HELLO",17) ;  
    }
}

Could anyone tell me how I could then make "message" and "age" hold the values passed in from class program...

I understand why the values do not hold but do not know how (if I could) use the ref or out keyword in these circumstances..

Basically I want the values to hold in my two variables then be able to write the values of them from class program...

Just a little practice that I am doing...thanks

Recommended Answers

All 2 Replies

They do hold the values, so I'm not sure what you are asking here. You might want to put the keyword 'public' in front of your property statements (lines 3 and 4) so they are accessible from outside the class.

You could do things like this:

class Program
{
    static void Main()
    {

        Class1 type = new Class1("HELLO",17) ;
        // here do things like
        Console.WriteLine(type.message);
        Console.WriteLine(type.age);
        // or if its birthday:
       type.age = 18; //etc

    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.