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

  • Home
  • SEARCH
  • 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 499599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:00:07+00:00 2026-05-13T06:00:07+00:00

I have two classes an Arc class and a Line class public class Arc

  • 0

I have two classes an Arc class and a Line class

public class Arc
{
     protected double startx;
     protected double starty;
     protected double endx;
     protected double endy;
     protected double radius;

     public Arc(){}
}
public class Line
{
     protected double startx;
     protected double starty;
     protected double endx;
     protected double endy;
     protected double length;
     public Line(){}
}

But I want to store arcs and lines in the same list, so I tried an interface like this

public interface Entity
{
     double StartX();
     double StratY();
     double EndX();
     double EndY();
}

Then I added the appropriate methods to each class and added the code to use the interface. Now I can add both types of objects to a list, but I want to get the length from a line object and don’t want to add a length method to the arc or the interface. Is my only option to cast the line object back to a line object like this?

List<Entity> entities = new List<Entity>();
entities.Add(new Line(10,10,5,5));
Line myLine = (Line)Entities[0]
double length = myLine.Length();

*Assuming I have all the proper methods in the line class.
Or is there a better/different way to do this?

  • 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-13T06:00:07+00:00Added an answer on May 13, 2026 at 6:00 am

    Or is there a better/different way to
    do this?

    If your objects descend from a common class, then you can store them in the same collection. In order to do anything useful with your objects without throwing away type safety, you’d need to implement the visitor pattern:

    public interface EntityVisitor
    {
        void Visit(Arc arc);
        void Visit(Line line);
    }
    
    public abstract class Entity
    {
        public abstract void Accept(EntityVisitor visitor);
    }
    
    public class Arc : Entity
    {
        protected double startx;
        protected double starty;
        protected double endx;
        protected double endy;
        protected double radius;
    
        public override void Accept(EntityVisitor visitor)
        {
            visitor.Visit(this);
        }
    }
    
    public class Line : Entity
    {
        protected double startx;
        protected double starty;
        protected double endx;
        protected double endy;
        protected double length;
    
        public override void Accept(EntityVisitor visitor)
        {
            visitor.Visit(this);
        }
    }
    

    Once that’s in place, you create an instance of EntityVisitor whenever you need to do something useful with your list:

    class EntityTypeCounter : EntityVisitor
    {
        public int TotalLines { get; private set; }
        public int TotalArcs { get; private set; }
    
        #region EntityVisitor Members
    
        public void Visit(Arc arc) { TotalArcs++; }
        public void Visit(Line line) { TotalLines++; }
    
        #endregion
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            Entity[] entities = new Entity[] { new Arc(), new Line(), new Arc(), new Arc(), new Line() };
            EntityTypeCounter counter = entities.Aggregate(
                new EntityTypeCounter(),
                (acc, item) => { item.Accept(acc); return acc; });
    
            Console.WriteLine("TotalLines: {0}", counter.TotalLines);
            Console.WriteLine("TotalArcs: {0}", counter.TotalArcs);
        }
    }
    

    And for what its worth, if your open to trying new languages, then F#’s tagged unions + pattern matching are a handy alternative to the visitor pattern.

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

Sidebar

Ask A Question

Stats

  • Questions 261k
  • Answers 261k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Turns out you need to wrap your image in a… May 13, 2026 at 11:33 am
  • Editorial Team
    Editorial Team added an answer I just learned about show variables; Which will show you… May 13, 2026 at 11:33 am
  • Editorial Team
    Editorial Team added an answer [a-zA-Z0-9].*[a-zA-Z0-9] The easy way: At least two alnum's anywhere in… May 13, 2026 at 11:33 am

Related Questions

I have two classes and an interface like interface IVehicle { void Drive(); }
I have two classes in an owned one-to-many relationship. The parent is Map, and
In a WCF service, I have two classes with the [DataContract] attribute. One of
I have two classes that each need an instance of each other to function.
I have two classes.... Parcel and FundParcel...and I'm trying to convert an IEnumerable of

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.