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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:45:34+00:00 2026-05-18T02:45:34+00:00

I am having the following problem: public class A { public A(X, Y, Z)

  • 0

I am having the following problem:

public class A {
    public A(X, Y, Z) {
    ...
    }
}

public class B : A {
    public B(X, Y) : base(X, Y) {
        //i want to instantiate Z here and only then pass it to the base class!
    }
}

How can I solve this problem? Is there a way?

  • 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-05-18T02:45:35+00:00Added an answer on May 18, 2026 at 2:45 am

    The common solution is to call a static method belonging to the type that can calculate the value of the parameter to be passed to the base constructor.

    For example:

    public B(int x, int y)
        : base(x, y, CalculateZ(x, y))
    {
    
    }
    
    // You can make this parameterless if it does not depend on X and Y
    private static int CalculateZ(int x, int y)
    {
       //Calculate it here.
    
        int exampleZ = x + y;
    
        return exampleZ;
    }
    

    Do note that CalculateZ cannot be an instance method, because the this reference is not available in constructor initializers.

    From the language-specification 10.11.1 Constructor initializers:

    An instance constructor initializer
    cannot access the instance being
    created. Therefore it is a
    compile-time error to reference this
    in an argument expression of the
    constructor initializer, as is it a
    compile-time error for an argument
    expression to reference any instance
    member through a simple-name.

    EDIT: Changed ‘instance’ to ‘static’ in the description.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this base class having the following interface: abstract class Base { abstract
I'm having following problem. I should convert a control to a certain type, this
I'm having the following problem and wondered whether anyone could see why this is
I´m having the following problem: I have 2 classes (A and B). The class
i am trying to XML serialize a class object but having the following problem:
I have been having the following problem, i think it's probably due to the
I'm having the following problem using the Visual Studio 2010 Team System Beta 1:
I've been having the following problem with my GoDaddy's server. I am using JSPs
I'm having the following problem: When I got two labels into each other: <Label
I'm having the following problem when using GWT and request factory. I am working

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.