Solved

Explanation of How Second Constructor Is Working and Why Is Needed

Posted on 2012-04-05
2
325 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
  • 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 500 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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
JSON  parse help 7 40
need to convert function to c# 5 22
JQuery Detecting page post back 6 3
Wordpress Hacked 1 12
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

790 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