0

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

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by ddanbe
0

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.

0

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

    }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.