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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:22:04+00:00 2026-06-05T21:22:04+00:00

I have a few classes and I am having problems accessing properties defined in

  • 0

I have a few classes and I am having problems accessing properties defined in subclasses from other class methods.

I have a base class called Section and a few subclasses, e.g. SectionPlane : Section. In each subclass, a different set of fields and properties are defined (in SectionPlane, private field _t and public property t can be found, whereas in SectionExtruded : Section I have private field _A and public property ´A´).

Class Section

// General section object
public abstract class Section
{
    public Section()
    {}
}

Class Plane Section

// Section object representing a plane with thickness t
public class SectionPlane : Section
{
    private double _t;

    public SectionPlane(double t)
    {
        this.t = t;
    }

    public double t
    {
        get
        {
            return _t;
        }
        set
        {
            _t = value;
        }
    }
}

Class Extruded Section

// Section object of some geometry with cross section area A extruded along the element it is assigned to.
public class SectionExtruded : Section
{
    private double _A;

    public SectionExtruded(double A)
    {
        this.A = A;
    }

    public double A
    {
        get
        {
            return _A;
        }
        set
        {
            _A = value;
        }
    }
}

The problem occurs when I from any subclass of the class Element tries to access the properties, since theese are not set in the base class Section, e.g. in the element Solid2D : Element:

Class Element

public abstract class Element
{
    private Section _section;

    public Element(Section section)
    {
        this.section = section;
    }

    public Section section
        {
            get 
            {
                return _section;
            }
            set
            {
                _section = value;
            }
        }
    }
}

Class Solid 2D Element

// Solid2D elements can only have sections of type SectionPlane
public class Solid2D : Element
{
    public Solid2D(SectionPlane section)
        : base(section)
    {
    }

    public void Calc()
    {
        double t = section.t;    // This results in error 'Section' does not contain a definition for 't' and no extension method 't' accepting a first argument of type 'Section' could be found (are you missing a using directive or an assembly reference?)
    }
}

Bar Element

// Bar elements can only have sections of type SectionExtruded
public class Solid2D : Element
{
    public Solid2D(SectionExtruded section)
        : base(section)
    {
    }

    public void Calc()
    {
        double A = section.A;    // This results in error 'Section' does not contain a definition for 'A' and no extension method 'A' accepting a first argument of type 'Section' could be found (are you missing a using directive or an assembly reference?)
    }
}

Is there any way to access my property t without having to include it in the base class Section? This would be very helpfull since not all of the sections that I will use have the same properties.

  • 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-05T21:22:05+00:00Added an answer on June 5, 2026 at 9:22 pm

    Since you know that it can only be a SectionPlane you can cast it

    double t = ((SectionPlane)section).t;
    

    If you are not sure whether you have a section of the right type, you can use the as keyword

    double t = 0;
    var sectionPane = section as SectionPlane;
    if (sectionPane != null) {
        t = sectionPane.t;
    }
    

    as does not throw an exception if the section has another type, but returns null instead.

    Or you can simply test

    double t = 0;
    if(section is SectionPlane) {
        t = ((SectionPlane)section).t;
    }
    

    but this is less elegant than using as, since you have to test the type and then cast it; but casting does this test again internally.

    Using the new pattern matching introduced in C# 7.0 you can write:

    double t = 0;
    if(section is SectionPlane sp) {
        t = sp.t;
    }
    

    But if you have to perfrom such a test, the question is, whether your approach is right in the object-oriented sense. If you move the Calc-method to the abstract class Section and let each class perform its own calculation, then no type test or casting will be required.

    In Section:

    public abstract void Calc();
    

    In SectionPlane

    public override void Calc()
    {
        var x = t;
    }
    
    ...
    
    section.Calc();  // No casting or type test required.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few classes as shown here public class TrueFalseQuestion implements Question{ static{
I have a few different classes which origin is a another class. I have
After having written a few helper classes in magento, now i have this problem,
I am having problems getting the desired behavior out of these few classes and
I have a few classes which have a property which is unique to that
I have a few classes: Article ------- Content ID Magazine -------- Name Code And
I have a few classes in my current project where validation of Email/Website addresses
I have a few classes in a project that I inherited that are really
I have a few classes that perform background tasks that might raise exceptions. They
I have a data access library that has a few classes that all implement

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.