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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:28:18+00:00 2026-05-11T13:28:18+00:00

This time I have problem with virtual fields. I have core class for my

  • 0

This time I have problem with virtual fields.

I have core class for my game objects. This class contains a field with Model class object. Model’s object contains values such as position etc.

Now – while drawing I need to read position of each object from it’s model. The problem starts when instead of default model class I’m using derived. Example:

abstract class GenericGameObject { public DefaultGameObjectModel Model = new DefaultGameObjectModel(); } class Missile : GenericGameObject { public new MissileModel Model = new MissileModel(); }  class DefaultGameObjectModel { public Vector2 Position = new Vector2(){X=0}; } class MissileModel : DefaultGameObjectModel { }  Missile m = new Missile(); m.Model.Position.X = 10; // NOT OK! ((GenericGameObject)m).Model.Position.X == 0 

I tried to make Model defined as virtual property instead of field, but this fails because derived properties have to be of same type as their base. Casting is futile because there will be many other model types. What can I do if I want to read a value from derived class, not from base?

I asked this question already but the answer didn’t brought any solution. Explaination:

  • to use interface IGameObjectModel

    Concept is good, but I have to enforce fields. Interfaces can’t define fields so I have to define property. But then I can’t do IGameObjectModel.Position.X=10 because Position is not a field.

  • to make GenericGameObject a generic type such as GenericGameObject and Missile a type derived from GenericGameObject I couldn’t then cast a missile to GenericGameObject and generally store those object on same list. Of course I could make main base type which those two could inherit from, but then I wouldn’t have access to Model field.

  • to make model a property instead of field. It is impossible to change property type in derived class.

Whad can I do?

  • 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. 2026-05-11T13:28:19+00:00Added an answer on May 11, 2026 at 1:28 pm

    In this case your best approach would be to assign the value of your parent field to be an instance of your derived class, then either cast it back to your derived class or hold on to a reference of your derived class (probably better).

    Or you could go down this road, which I like the best…

    abstract class GenericGameObject {     public DefaultGameObjectModel Model     {         get { return ModelInternal; }     }      protected abstract DefaultGameObjectModel ModelInternal { get; } }  class Missile : GenericGameObject {     private MissileModel model = new MissileModel();      public override DefaultGameObjectModel ModelInternal     {         get { return model; }     }      public new MissileModel Model     {         get { return model; }         set { model = value; }     } }  class DefaultGameObjectModel { public Vector2 Position = new Vector2(){X=0}; } class MissileModel : DefaultGameObjectModel { }  Missile m = new Missile(); m.Model.Position.X = 10; 

    This solution gives you access to your base model instance from the context of the base class, while giving you access to your concrete model instance from the inherited class.

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

Sidebar

Ask A Question

Stats

  • Questions 121k
  • Answers 121k
  • 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 You've done most of the work here! Just add a… May 12, 2026 at 12:26 am
  • Editorial Team
    Editorial Team added an answer Assembly assembly = typeof(DataSet).Assembly; // etc foreach (Type type in… May 12, 2026 at 12:26 am
  • Editorial Team
    Editorial Team added an answer Neve mind! I found the answer after browsing the docs… May 12, 2026 at 12:26 am

Related Questions

I have a simple Waiver model, and I would like to make a query
I have an ASP.NET web application that calls a .NET DLL, that in turn
I have an Ubuntu Server (with all of LAMP and Subversion installed). I use

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.