none
collections and parametrized constructor RRS feed

  • Question

  • The parameterized constructor should also create List<Screen> that contains the number of instances equivalent to the number of screens and assign it to Screens property. anyone can tell me what that mean and how to do it?
    Tuesday, May 28, 2019 9:34 PM

All replies

  • Is this a question on a programming test?  What have you done so far?

    They want you to create a member variable of type List<Screen>, with a name that's up to you, and create a property "Screens" that refers to it.  Then, they want you to create a list in the constructor, then create a number Screen objects and store them in the list.  I'm guessing the number of screens is a parameter to the constructor.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Tuesday, May 28, 2019 11:23 PM
  • Hi JitenderSingh29011998,

    We usually using a parameterized constructor to  assign different values to each instance of the class.

    Then creates the instances of the classes and call the field value that is updated by constructors.

    The following example, created a parameterized constructor for the 'Screen' class that can be used to assign different values of length and height member variables for the 'Screen' class according to the instance to be created.

    namespace Test
    {
        class Screen
        {
            private double length;
            private double height;
    
    
            public Screen(double len, double hei)
            {  //Parameterized constructor
                Console.WriteLine("length = {0},Height={1}", len, hei);
                length = len;
                height = hei;
            }
    
    
            public double GetArea()
            {
                double area = length * height;
                Console.WriteLine("Screen's Area is : {0} ", area);
                return area;
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                List<Screen> screen = new List<Screen>();
                Screen s1 = new Screen(10, 20);
                Screen s2 = new Screen(12, 24);
                screen.Add(s1);
                screen.Add(s2);
                Console.WriteLine("Count : {0}", screen.Count);
    
                foreach (Screen  sc in screen)
                {
                    Console.WriteLine("{0}",sc.GetArea());
                }
    
                Console.ReadLine();
            }
        }
    }
    
    

    Hope this would be helpful.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 29, 2019 6:23 AM
    Moderator
  • This sounds like a homework assignment and we do not provide homework assignment answers in the forums. The most we can provide is guidance.

    Michael Taylor http://www.michaeltaylorp3.net

    Wednesday, May 29, 2019 1:35 PM
    Moderator
  • Hello,

    To learn about the Screen class and for initializers go here.

    Work through the task, if you get stuck come back with a decent code effort or discuss this with the instructor of the class you are taking.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, May 29, 2019 1:48 PM
    Moderator