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 9113691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:01:18+00:00 2026-06-17T04:01:18+00:00

As part of my code I have created the following struct: struct ItemInformation {

  • 0

As part of my code I have created the following struct:

struct ItemInformation
{
    public int ItemNumber;
    public double HighPrice, LowPrice, MedianPrice;

    public ItemInformation(int number, double highP, double lowP) : this()
    {
        this.ItemNumber = number;
        this.HighPrice = highP;
        this.LowPrice = lowP;
        this.MedianPrice = CalculateMedian(highP, lowP);
    }

    private double CalculateMedian(double high, double low)
    {
        return 0.5 * (high + low);
    }
}

I know the struct contains four pieces of data but I want to be able to only specify three as the fourth one, MedianPrice, is completely determined by the high and low prices. So I want to use

ItemInformation jacket;

as well as

ItemInformation jacket = new ItemInformation(1234, 145.70, 73.20)

to create instances of the structure, but I cannot seem to get this to work. All the items in the struct are set correctly except for the MedianPrice. This may be a mutability issue but I am not really changing values here. I am simply setting one value based on the other two and I want to create instances of the struct using only three inputs. Does anyone have any suggestions as to how I can do this? Any advice would be greatly appreciated. Thank you.

  • 1 1 Answer
  • 0 Views
  • 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-06-17T04:01:19+00:00Added an answer on June 17, 2026 at 4:01 am

    You should make the struct immutable by making all instance fields readonly. Also, the MedianPrice could be a property, and it needs no data in the struct value:

    struct ItemInformation
    {
      public readonly int ItemNumber;
      public readonly double HighPrice, LowPrice;  // no MedianPrice here
    
      public ItemInformation(int number, double highP, double lowP) // no need to chain this()
      {
        this.ItemNumber = number;
        this.HighPrice = highP;
        this.LowPrice = lowP;
      }
    
      public double MedianPrice
      {
        get
        {
          return 0.5 * (HighPrice + LowPrice);
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created the following code as part of an excercise in a Ruby
I have created a class to manage a SQLite database. Part of its code
I currently have the following code. struct qnode { char *path; struct qnode *next;
I have created following layout.which has two relative layouts and one scrollview ,following code
I have the following class created: Public Class Candidate Public Account As String Public
I have this part of code in my functions.php: function cc_admin_enqueue_scripts($hook) { $file_dir=get_bloginfo('template_directory'); wp_enqueue_script('media-upload');
Suppose I have a Markdown string: * Bullet has *bold [code]*test[/code] part*. *Another\nbold* item.
In my code I have to divs with different names, except the last part
i have this simple question please. I have this part of code which sets
I'm stuck on one thing i can't get solved. I have part of code,

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.