Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4244204
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:40:40+00:00 2026-05-21T03:40:40+00:00

Good morning! I’m writing a class for drawing histograms and, for user’s convenience, I’ve

  • 0

Good morning! I’m writing a class for drawing histograms and, for user’s convenience, I’ve decided to add a few convenience constructors.

However, as soon as I recently switched to .NET code contracts from DevLabs, I want to make full use of preconditions for protection against my own (or someone’s) stupidity.

    public HistoGrapher(IList<string> points, IList<T> values)
        : this(points.Select((point, pointIndex) => new KeyValuePair<string, T>(point, values[pointIndex])))
    {
        Contract.Requires<ArgumentNullException>(points != null, "points");
        Contract.Requires<ArgumentNullException>(values != null, "values");
        Contract.Requires<ArgumentException>(points.Count == values.Count, "The lengths of the lists should be equal.");
    }

    public HistoGrapher(IEnumerable<KeyValuePair<string, T>> pointValuePairs)
    {
        // useful code goes here
    }

There is a thing that confuses me. I don’t want the first constructor to ever call the second one if the contract is broken; however, it is supposed that a call to this(...) will be performed before executing the body of the first constructor.

Will this code work as I want? I haven’t tried yet.
And if not, is there a capability of solving such an issue?

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T03:40:40+00:00Added an answer on May 21, 2026 at 3:40 am

    And if not, is there a capability of
    solving such an issue?

    Since the constructor body is only executed after calling the other constructor I don’t think your current approach can work. I would recommend factoring out the common code into a separate method i.e. Init() that you can then call from both constructors, that would keep your code DRY and solve your problem:

    public class HistoGrapher
    {
    
        public HistoGrapher(IList<string> points, IList<T> values)
        {
            Contract.Requires<ArgumentNullException>(points != null, "points");
            Contract.Requires<ArgumentNullException>(values != null, "values");
            Contract.Requires<ArgumentException>(points.Count == values.Count, "The lengths of the lists should be equal.");
    
            var pointValuePairs = points.Select((point, pointIndex) => new KeyValuePair<string, T>(point, values[pointIndex]))
            Init(pointValuePairs);
        }
    
        public HistoGrapher(IEnumerable<KeyValuePair<string, T>> pointValuePairs)
        {
            Init(pointValuePairs);
        }
    
        private void Init(IEnumerable<KeyValuePair<string, T>> pointValuePairs)
        {
             // useful code goes here
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good morning, I am about to start writing an Excel add-in for Excel 2002.
good morning, I was trying the SharedPreferences Class and I have created two classes,
Good Morning! Given: public class FooClass { public void FooMethod() { using (var myEntity
Good morning I have been running a few tests over the past couple of
Good morning I am learning Java and trying to understand the access to class
Good morning, Suppose I have a class public class Class { int something; int[]
Good morning, I have mysql queries where I would like to calculate percentage of
Good morning, I am currently developing a java web application that exposes a web
Good Morning, Is it possible to map assembly requests to the version within another
Good Morning. I have two tables, and one references the other. When I insert

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.