?
Solved

Explanation of How Second Constructor Is Working and Why Is Needed

Posted on 2012-04-05
2
Medium Priority
?
336 Views
Last Modified: 2012-09-13
I am new to web development, and I was wondering if someone could explain to be what the purpose of the second constructor that is part of the partial BusMain class that is attached below. (I understand the purpose of the constructor. I was just wondering why you would need to have a second constructor with XML parameters in it.) Any help would be greatly appreciated.

 

//  Nancy Drew   January 12, 2023

//  Theoretical code to exemplify problem described in question

 

using System;

using System.Xml;

using System.Xml.Serialization;

using System.Collections;

using System.Collections.Generic;

 

namespace NS.Conn.LegalProject.Businesses.BUS_Entities

{

    [Serializable]

    public class BusMain:BusBase<BusMain>

    {

        #region Private Variables

 

        private bool legalDocumentationExists;

        private bool detailsExists;

        private bool nondetailsExists;

        private LegalDocumentation legalDocumentation;

        private List<detailsInformation> detailsInfos;

        private List<NondetailsInformation> nondetailsInfos;

      

 

        #endregion

 

        #region Constructors

 

        /// <summary>

        /// Default constructor.

        /// </summary>

        public BusMain()

        {

            this.nondetailsExists = false;

            this.detailsExists = false;

            this.legalDocumentationExists = false;

            this.legalDocumentation = new LegalDocumentation();

            this.detailsInfos = new List<detailsInformation>();

            this.nondetailsInfos = new List<NondetailsInformation>();

        }

 

        /// <summary>

        /// Constructor building the intstance from the Xml string.

        /// </summary>

        /// <param name="xmlString"></param>

        public BusMain(string xmlString)

            :this()

        {

            BusMain temp = this.FromXml(xmlString);

            this.NondetailsExists = temp.NondetailsExists;

            this.detailsExists = temp.detailsExists;

            this.LegalDocumentationExists = temp.LegalDocumentationExists;

            this.LegalDocumentation = temp.LegalDocumentation;

            this.detailsInfos = temp.detailsInfos;

            this.NondetailsInfos =temp.NondetailsInfos;

        }

 

        #endregion

 

        #region Public Properties

 

         public bool NondetailsExists

         {

             get{ return this.nondetailsExists;}

             set {this.nondetailsExists = value;}

         }

         public bool detailsExists

         {

             get{ return this.detailsExists;}

             set {this.detailsExists = value;}

         }

          public bool LegalDocumentationExists

         {

             get{ return this.legalDocumentationExists;}

             set {this.legalDocumentationExists = value;}

         }

        }

 

        public LegalDocumentation LegalDocumentation

        {

            get

            {

                return this.legalDocumentation;

            }

            set

            {

                this.legalDocumentation = value;

            }

        }

 

        public List<detailsInformation> detailsInfos

        {

            get

            {

                return this.detailsInfos;

            }

            set

            {

                if (value != null)

                {

                    this.detailsInfos = value;

                }

                else

                {

                    this.detailsInfos = new List<detailsInformation>();

                }

            }

 

        }

 

        public List<NondetailsInformation> NondetailsInfos

        {

            get

            {

                return this.nondetailsInfos;

            }

            set

            {

                if (value != null)

                {

                    this.nondetailsInfos = value;

                }

                else

                {

                    this.nondetailsInfos = new List<NondetailsInformation>();

                }

            }

        }

 

        #endregion

 

        #region Public Methods

 

        /// <summary>

        /// Checks if data in the instance is consistent with business rules.

        /// </summary>

        /// <returns></returns>

        public bool IsValid()

        {

            bool retValue = true;

 

            return retValue;

        }

 

        /// <summary>

        /// Checks if it has cover in NondetailsInformations collection.

        /// </summary>

        /// <returns></returns>

        public bool HasThisCover()

        {

            return this.HasCCover(CCovers.This);

        }

 

        /// <summary>

        /// Checks where it got c in NondetailsInformations collection.

        /// </summary>

        /// <returns></returns>

        public bool HasThatC()

        {

            return this.HasCObtainedThrough(CObtainedThrough.That);

        }

 

        #endregion

 

        #region Private Methods

 

        private bool HasCCover(CCovers cCover)

        {

            bool retValue = false;

            if (this.nondetailsInfos != null)

            {

                if (this.nondetailsInfos.Count > 0)

                {

                    foreach (NondetailsInformation nondetailsInformation in this.nondetailsInfos)

                    {

                        if ((nondetailsInformation.CCovers & cCover) == cCover)

                        {

                            retValue = true;

                            break;

                        }

                    }

                }

            }

            return retValue;

        }

 

        private bool HasCObtainedThrough(CObtainedThrough cObtainedThrough)

        {

            bool retValue = false;

            if (this.nondetailsInfos != null)

            {

                if (this.nondetailsInfos.Count > 0)

                {

                    foreach (NondetailsInformation nondetailsInfo in this.nondetailsInfos)

                    {

                        if (nondetailsInfo.CObtainedThrough == cObtainedThrough)

                        {

                            retValue = true;

                            break;

                        }

                    }

                }

            }

            return retValue;

        }

 

        #endregion

 

    }

}

Open in new window

0
Comment
Question by:thenthorn1010
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 37811948
The first constructor is creating an instance of the class with default values.
The second constructor is creating an instance of the class with the values taken from the xmlstring that is passed in.
0
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 2000 total points
ID: 37811974
The only purpose (I can think of) to the second constructor is to provide a way of initialising the class instance with values during instantation rather than having to set them after creation in the calling code.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question