Solved

Explanation of How Second Constructor Is Working and Why Is Needed

Posted on 2012-04-05
2
322 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
XSLT Display Label Name 1 22
PowerShell script to remove string in xml file 3 24
C# winforms programmitically move panels 6 29
insert value of checklistbox checked 4 27
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
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.

832 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