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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:49:01+00:00 2026-05-15T18:49:01+00:00

I know the title’s a bit wordy, but I don’t know how else to

  • 0

I know the title’s a bit wordy, but I don’t know how else to ask this question. This is basically the technique that I’ve been using to filter the types of objects you pass into an inherited class. Have a look at the code first and I’ll explain more…

public interface IProjectile {}
public interface IPaintBall : IProjectile {}
public interface IPotato : IProjectile {}

public class Prop
{
    public void Shoot(params IProjectile[] projectiles)
    {
        // logic goes here...
    }
}

public class Car : Prop
{
    public override void Shoot(params IPaintBalls[] paintBalls)
    {
        base.Shoot(paintBalls);
    }
}

See? I don’t want you to shoot my car with potatoes. You can only shoot it with paint-balls. So am I doing this the right way? Again, this gets way more complicated when the Prop class has like 100 functions that I also want to filter down to just paint-balls. I don’t want to write-out those 100+ functions for the Car class, right? I especially don’t want to write-out those 100+ functions for the 100+ Car classes that I’ll be writing.

Am I being clear enough here?

This is just an example. I’m not doing game programming or anything like that. I’m just trying to give you guys a really simple example to convey what I want here. Basically, I don’t want the code to compile if someone is trying to pass a potato to the Car’s Shoot() function.

  • 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-15T18:49:01+00:00Added an answer on May 15, 2026 at 6:49 pm

    What I would probably do is to make Prop accept a Generic Type. I.e:

    public class Prop<T> where T : IProjectile
    {
        public virtual void Shoot(params T[] projectiles)
        {
            // logic goes here... 
        }
    }
    
    public class Car : Prop<IPaintBall>
    {
        public override void Shoot(params IPaintBall[] projectiles)
        {
            base.Shoot(projectiles);
        }
    } 
    

    The methods are then exposed as PaintBalls for users of the Car class, i.e:

    Car car = new Car();
    car.Shoot(somePaintballs); // Shoot will only take IPaintBall.
    

    You can then also have a non generic Prop class:

    public class Prop : Prop<IProjectile>
    {
    }
    

    Where you can still use:

    public class Person : Prop
    {
    }
    

    Person can be shot with any IProjectile, including IPotato.

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

Sidebar

Related Questions

I know the title of this question is a bit confusing, but here it
I know the title of this question might be strange, but that was the
I know the title of the question is a bit odd. But I don't
I don't know which title I should use for this question. I have a
I know the title of this question is stupid, but I couldn't come up
I don’t know what title should I use, but please read this. I’m making
Sorry but I dont know what title I should give this question(Suggestion?) I have
I really didn't know what title to give this question, but I'll explain here:
I know the title is a bit messy, but I don't know how to
I know title is a bit confusing ;D but basically what I want to

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.