Click here to Skip to main content
15,888,330 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Guys,
Sorry just started object orientated and I am stuck on the below not printing out the items in the list created.

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lecture01
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Reservation> reservations = new List<Reservation>()
            {
                new Reservation (23,"Steve", "Jobs", 34,61,47),
                new Reservation (54,"Keith", "Elliot", 45,80,23)
            };

            foreach (Reservation r in reservations)
            {
               Console.WriteLine(r);
               Console.Read();
            }

        }
    }



    public class Reservation
    {
        int ResNumber;
        string FirstName;
        string LastName;
        int RoomNum;
        int Rate;
        //DateTime CheckIn;
        int Duration;




        public int resNumber
        {
            get { return ResNumber; }
            set { ResNumber = value; }
        }
        public string firstName
        {
            get { return FirstName; }

        }
        public string lastName

        {
            get { return LastName; }
        }
        public int roomNum
        {
            get { return RoomNum; }
            set { RoomNum = value; }
        }
        public int rate
        {
            get { return Rate; }
            set { Rate = value; }
        }
        public int duration
        {
            get { return Duration; }
            set { Duration = value; }
        }


        public Reservation(int ResNumber, string FirstName, string LastName, int RoomNum, int Rate, int Duration)
        {
            this.ResNumber = ResNumber;
            this.FirstName = FirstName;
            this.LastName = LastName;
            this.RoomNum = RoomNum;
            this.Rate = Rate;
            this.Duration = Duration;

        }



    }
}
Posted
Comments
ZurdoDev 10-Aug-15 7:30am    
Console.WriteLine(r.Property1); Tell it what part of r you want to write out.

There are two methods to make it work. First one as Ryan has mentioned in his comment to your question:

C#
            List<reservation> reservations = new List<reservation>()
            {
                new Reservation (23,"Steve", "Jobs", 34,61,47),
                new Reservation (54,"Keith", "Elliot", 45,80,23)
            };
 
            foreach (Reservation r in reservations)
            {
               // considering first parameter was for Age member
               Console.WriteLine(r.Age); 
               Console.Read();
            }
</reservation></reservation>


However, there is another method to overcome this. That is by overriding the ToString function of the object. Each time you try to print the object the ToString function is called, in which. The only string representation of the object is its full type name. For example, your Program object if printed

C#
Console.WriteLine(new Program()); 


Would print Lecture01.Program. That is the string representation of your objects. To overcome this, you need to override the base ToString function and return your own string for each of the object instance.

Remember: ToString is an instance function.

C#
class Reservation {
   // Define members
   int ResNumber;
   string FirstName;
   string LastName;
   int RoomNum;
   int Rate;
   //DateTime CheckIn;
   int Duration;
}


Now the function that gets called can be editted out,

C#
public override string ToString() {
   return string.Format("Property1: {0}, Property2: {1}, Property3: {2}, Property4: {3}", Property1, Property2, Property3, Property4); 
}

// For keeping things simple I did not write all the lengthy function. 


Remember: string.Format is better than concatenating multiple strings.

In case where you need to get only one property, you can always call a member by object.Membername. However, in cases where you want to print the entire object. Then this would be a lot helpful as every object would be converted to this representation underground.
 
Share this answer
 
v2
Comments
ZurdoDev 10-Aug-15 8:26am    
Well said. +5
Afzaal Ahmad Zeeshan 10-Aug-15 8:31am    
Thank you Ryan.
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace Lecture01
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<reservation> reservations = new List<reservation>()
            {
                new Reservation (23,"Steve", "Jobs", 34,61,47),
                new Reservation (54,"Keith", "Elliot", 45,80,23)
            };

                foreach (Reservation r in reservations)
                {
                    Console.WriteLine("Reservation Number:{0}",r.resNumber);
                    Console.WriteLine("First Name:{0}", r.firstName);
                    Console.WriteLine("Last Name:{0}", r.lastName);
                    Console.WriteLine("Room Number:{0}", r.roomNum);
                    Console.WriteLine("Rate:{0}", r.rate);
                    Console.WriteLine("Duration:{0}", r.duration);
                    Console.Read();
                }

            }
        }



        public class Reservation
        {
            int ResNumber;
            string FirstName;
            string LastName;
            int RoomNum;
            int Rate;
            //DateTime CheckIn;
            int Duration;




            public int resNumber
            {
                get { return ResNumber; }
                set { ResNumber = value; }
            }
            public string firstName
            {
                get { return FirstName; }

            }
            public string lastName
            {
                get { return LastName; }
            }
            public int roomNum
            {
                get { return RoomNum; }
                set { RoomNum = value; }
            }
            public int rate
            {
                get { return Rate; }
                set { Rate = value; }
            }
            public int duration
            {
                get { return Duration; }
                set { Duration = value; }
            }


            public Reservation(int ResNumber, string FirstName, string LastName, int RoomNum, int Rate, int Duration)
            {
                this.ResNumber = ResNumber;
                this.FirstName = FirstName;
                this.LastName = LastName;
                this.RoomNum = RoomNum;
                this.Rate = Rate;
                this.Duration = Duration;

            }



        }
    }
}
 
Share this answer
 
v2
Comments
Member 11898426 10-Aug-15 7:57am    
Thank you so much for the help.
Does not seem to print the list out on the console though?
sreeyush sudhakaran 11-Aug-15 1:05am    
What error it is showing?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900