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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:17:24+00:00 2026-05-28T00:17:24+00:00

I have 3 different type of shape diagrams say Rectangle , Cube , Circle

  • 0

I have 3 different type of shape diagrams say Rectangle, Cube, Circle, I want to define classes for them such that

  • All classes would have Title & Color property/method,

  • Circle & Rectangle would have additional method Area,

  • Similarly Cube would have Volume method rather than Area method.

There is a method in which I get the reference of of ‘object’, following is sample method

public void ShapeClicked(object obj)
{
// Check the type of obj & type cast it accordingly & call the method on that object
    object obj = new Circle();

    if (obj is Circle)
    {
        Circle circleObj  = (Circle)obj;
        circleObj.GetArea();
    }
    else if (obj is Rectangle)
    {
        Rectangle rectangleObj = (Rectangle)obj;
        rectangleObj.GetArea();
    }
    else if (obj is Cube)
    {
        Cube cubeObj = (Cube)obj;
        cubeObj.GetVolume();
    }
}

How can I design my classes such that in ‘ShapeClicked(object obj)’ method

  • a) I need not to check the type every time (should happen at run time), (This is important for me because I have many methods like ‘ShapeClicked’ which requires me checking the type)

  • b) if object is of Circle then it would get only Area method, similarly for Cube object it would get only Volume method.

I can think of two approaches

  • Put all the methods & properties in base class & only override required methods in derive class. Create a Factory method/class which would return the reference of Base class. With this approach my ‘b’ requirement does not meet

  • Put only common methods/property (like color) in base class & add additional methods in derive class. This does not solve #a 🙁

Can anybody suggest me some solution which solves both #a & #b

  • 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-28T00:17:25+00:00Added an answer on May 28, 2026 at 12:17 am

    Introduce a new interface.

    public interface IMeasurable
    {
        someReturnType GetMeasure();
    }
    
    public class Circle : IMeasurable
    {
        //some other methods
    
        public someReturnType GetMeasure() { return GetArea(); }
    }
    
    public class Cube : IMeasurable
    {
        //some other methods
    
        public someReturnType GetMeasure() { return GetVolume(); }
    }
    

    Update

    Your method would look like:

    public void ShapeClicked(object obj)
    {
        var measurable = obj as IMeasurable;
        if (measurable == null)
            throw new InvalidOperationException(string.Format("We can only work with measurable types, which {0} is not.", obj.GetType());
    
        var measure = measurable.GetMeasure();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imagine I have abstract base class Shape , with derived classes Circle and Rectangle
I have binary images and I want to convert them into a different type
I have several different content type nodes (videos, image galleries, stories...) that I would
I have an arraylist that gets different type of values in it, 1st value->
I have a C++ template class that gets instantiated with 3 different type parameters.
I have an array of different type objects and I use a BinaryWriter to
I have different urls that points to the same code www.url1.com www.url2.com I need
I have several small classes that are all peers of each other declared and
I have created different shapes like circle/rect etc in my program using BufferedPaintDC on
I have two C# (3.5) types that are different from each other. I do

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.