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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:03:31+00:00 2026-05-25T00:03:31+00:00

To start, some classes: public abstract class Component { GenericSystem mySystem; public Component() {

  • 0

To start, some classes:

public abstract class Component
{
    GenericSystem mySystem;

    public Component() { mySystem = null;}

    public void SetSystem(GenericSystem aSystem) { mySystem = aSystem; }
}

public class PhysicsComponent : Component
{
    int pos;    

    public PhysicsComponent(int x) : base() { pos = x; }
}


public abstract class GenericSystem : List<Component>
{
    public Type ComponentType;
    public GenericSystem(Type componentType)
    { ComponentType = componentType; }
    public void RegisterComponent(c)
    {
        Add(c);
        c.SetSystem(this);
    }
}

public class PhysicsSystem : GenericSystem
{
    public PhysicsSystem() : base(typeof(PhysicsComponent)) { }
}

public static GenericEngine
{
    List<GenericSystem> systems = new List<GenericSystem>();

    //... Code here that adds some GenericSystems to the systems ...

    public static void RegisterComponent(Component c)
    {
        foreach(GenericSystem aSystem in systems)
        {
            Type t = aSystem.ComponentType;
            //PROBLEM IS HERE
            t c_as_t = c as t;
            //
            if ( c_as_t != null)
                aSystem.RegisterComponent(c);
        }


    }

}

The error I get is “The type or namespace ‘t’ could not be found.”

I want each GenericSystem to have a Component type that it wants registered to it. This way, anything registering a new Component c simply calls GenericEngine.RegisterComponent(c) and all systems interested in that type of component register it.

Ideally, I’d like to have the code more along the lines of:

     //where T must be a child of Component
    public abstract class GenericSystem<T> : List<Component> { /... }
    public class PhysicsSystem : GenericSystem<PhysicsComponent>

I suspect this isn’t a terribly complicated question, and I’m missing something about how C# deals with types (or, more embarrassingly, generics in general) so if it’s an easy question, please just point me in the direction of some reading material. Thanks in advance!

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

    Local variable declarations and “as” do not work that way. “t” is an expression that at runtime evaluates to a reference to an object that represents a type. The local decl, and “as” are expecting a program fragment that at compile time names a type.

    You’re trying to put a cake onto a shelf of cake recipe books; though cakes and cake recipe books are intimately related, they are not the same kind of thing.

    If you want to determine if at runtime, the object c is of the type described by the t object, then you can call GetType on c and determine if the two types are either (1) equal, if you demand identity, or (2) compatible, if you merely require one to be compatible with the other.


    Ideally, I’d like to have the code more along the lines of:

     //where T must be a child of Component
    public abstract class GenericSystem<T> : List<Component>
    

    OK, then say that:

    public abstract class GenericSystem<T> : List<Component> where T : Component 
    

    Looking at your design, other things seem suspicious. Is a generic system actually a kind of list of components, or is it a thing that, among other things, contains a list of components? Use derivation to express “is a kind of” relationships. Use containment to express “container” relationships. A car is not a kind of list of wheels; a car has a list of wheels.

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

Sidebar

Related Questions

I have following classes: public abstract class CustomerBase { public long CustomerNumber { get;
I have some code which is effectively this: class dummie_type { public: int a;
Here is the question's plot: suppose I have some abstract classes for objects, let's
I will start by explaining my scenario in code: public class A { }
Lets say I have a set of model classes like this: public class Person
I have a Java class that looks like this: public class My_ABC { int
Let's say I have some classes defined as follows: class Security { Boolean AuthenticateUser(String
I am planning to start some Java mobile phone development. I downloaded the wireless
I have several init.d scripts that I'm using to start some daemons. Most of
I am going to start implementing some unit tests for a codebase that is

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.