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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:53:23+00:00 2026-05-15T22:53:23+00:00

This post contains a lot of code, but I would really appreciate it if

  • 0

This post contains a lot of code, but I would really appreciate it if you took the some to read and understand it…and hopefully come up with a solution

Let’s assume that I am structuring a network-game where the entities need to be drawn and some of them updated from the server accordingly.

The Drawable class is in charge of drawing the entities on-screen:

class Drawable
{
    public int ID { get; set; } // I put the ID here instead of having another 
                                // class that Drawable derives from, as to not 
                                // further complicate the example code.

    public void Draw() { }
}

The data that is received from the server implements IData with each concrete IData holding different properties. Let’s say we have the following data that the Player and Enemy will receive:

interface IData
{
    int ID { get; set; }
}

class PlayerData : IData
{
    public int ID { get; set; }
    public string Name { get; set; }
}

class EnemyData : IData
{
    public int ID { get; set; }
    public int Damage { get; set; }
}

The game entities that should be updateable from the server implement IUpdateable<T> where T is IData:

interface IUpdateable<T> where T : IData
{
    void Update(T data);
}

Our entities are thus Drawable and Updateable:

class Enemy : Drawable, IUpdateable<EnemyData>
{
    public void Update(EnemyData data) { }
}

class Player : Drawable, IUpdateable<PlayerData>
{
    public void Update(PlayerData data) {}
}

So that’s the basic structure of the game.


Now, I need to store a Dictionary of these Drawable and Updateable objects, storing the Drawable‘s ID as the key and a complex object that holds the Drawable and Updateable objects and their remote concrete IData:

class DataHolder<T, T1> where T:Drawable, IUpdateable<T1> where T1:IData
{
    public T Entity{ get; set;}
    public IData Data{ get; set;}

    public DataHolder(T entity, IData data)
    {
        Entity = entity;
        Data = data;
    }
}

As an example, say I currently have the following entities:

var p1 = new Player();
var p1Data = new PlayerData();
var e1 = new Enemy();
var e1Data = new EnemyData();

var playerData = new DataHolder<Player, PlayerData>(p1, p1Data);
var enemyData = new DataHolder<Enemy, EnemyData>(e1, e1Data);

I now need to have a Dictionary that holds the entities’ ID as a key (p1.ID and e1.ID) and their DataHolder (playerData and enemyData) and their value.

Something like the following (the below code just shows what I want to do and thus it doesn’t not compile):

 Dictionary<int, DataHolder> list = new Dictionary<int, DataHolder>();
 list.Add(p1.ID, playerData);
 list.Add(e1.ID, enemyData);

How do I construct such a Dictionary?

[Update]

As regards usage, I will then need to be able to do the following:

 foreach (var value in list.Values)
 {
     var entity = value.Entity;
     entity.Update(value.Data);
 }

I have also tried to change the design of DataHolder to the following:

class DataHolder<T> where T:Drawable, IUpdateable<IData>
{
    public T Entity{ get; set;}
    public IData Data{ get; set;}

    public DataHolder(T entity, IData data)
    {
        Entity = entity;
        Data = data;
    }
}

Then I tried something like the following:

var playerData = new DataHolder<Player>(p1, p1Data);  //error

But that throws a compile-time error:

The type ‘Player’ must be convertible
to ‘IUpdateable<IData>’ in order
to use it as parameter ‘T’ in the
generic class ‘DataHolder<T>’

For what reason is this thrown? Player implements IUpdateable<PlayerData> and PlayerData implements IData. Is this an issue with variance? And is there any way around it ?

  • 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-15T22:53:23+00:00Added an answer on May 15, 2026 at 10:53 pm

    Make your DataHolder class implement or inherit a non-generic class or interface, then make a dictionary of that (non-generic) type.

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

Sidebar

Related Questions

I just read this post about why new-line warnings exist, but to be honest
I this post , I've seen this: class MonitorObjectString: public MonitorObject { // some
I have read this post about how to test private methods. I usually do
This post reference to the One Definition Rule. Wikipedia is pretty bad on explaining
So this post talked about how to actually implement url rewriting in an ASP.NET
From this post . One obvious problem is scalability/performance. What are the other problems
Background this post explains how one can consume extension methods in Powershell http://community.bartdesmet.net/blogs/bart/archive/2007/09/06/extension-methods-in-windows-powershell.aspx Compare
Reading this post has left me wondering; are nightly builds ever better for a
I just finished reading this post: https://developer.yahoo.com/performance/rules.html#flush and have already implemented a flush after
Taking advice from this post , I purchased a copy of 'The C Programming

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.