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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:32:38+00:00 2026-06-14T16:32:38+00:00

Is there a way to access a class’s static methods/variables from an instance variable?

  • 0

Is there a way to access a class’s static methods/variables from an instance variable? I’ve tried searching for an answer, but my searches only find why you can’t access an instance method/variable within a static method. I get why static can’t access instance, but I don’t get instance can’t access static.

Here’s my situation: I’m a student making a top-down shooter game in XNA and I’m trying to use static Texture2Ds for each game object. I have a class GameObject that lays the basics for every other class, with two other main classes GameBot and Projectile that lays the basics for bots and projectiles respectively. My problem also has to do with this inheritance. I have all the collision code inside the GameBot and Projectile classes, and other classes like PlayerShip/EnemyShip or Cannonball/Missile inherit from them.

The problem I’m having is that I want to access a class method/variable from an instance variable of which I don’t know the class. What I mean is, I pass my method a GameBot variable, but it could be either PlayerShip, EnemyShip, or any other child of GameBot, and each has different static texture data.

class GameBot : GameObject
{
    static protected Texture2D texture;
    static internal Color[] textureData;

    //etc...

    internal bool DidHitEnemy(GameBot enemyGameBot)
    {
        //Here, I want to access enemyGameBot.textureData
        //to do pixel-by-pixel collision
        //but A) enemyGameBot.textureData doesn't work
        //and B) enemyGameBot's class could be any child of GameBot
        //so I can't just use GameBot.textureData
    }

    static internal virtual Color[] GetTextureData()
    {
        return textureData;
        //I even thought about coding this function in each child
        //but I can't access it anyway
    }
}

This game is kind of an exercise in inheritance. I want to try to keep as much code as possible in classes higher up in the hierarchy, and only code the essential differences in each class. The reason I decided on static textures is so I can keep an array of Projectile in each GameBot, but be able to modify on-the-fly which Projectile (Cannonball, Missile, etc) is in some spot in that array. Without static projectiles, I’d have to assign the sprite every time I switch Projectiles. There reason I want one array of Projectile is so I can easily add another Projectile without having to add code everywhere.

Is there a way of accessing a static method/variable from an instance variable? If not, any suggestions of another way to keep the collision code as general as possible?

  • 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-06-14T16:32:40+00:00Added an answer on June 14, 2026 at 4:32 pm

    An easy way to access this from the instance would be something like this…

    public Color[] GetTextureData()
    {        
        //note that `GameBot.` isn't required but I find it helpful to locate static 
        //calls versus `this.` for instance methods
        return GameBot.GetTextureDataInternal(); 
    }
    
    static internal Color[] GetTextureDataInternal()
    {
        return textureData;
    }
    

    … you could then just call .GetTextureData() from your external variable instead of having to manage/maintain the static call separately.

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

Sidebar

Related Questions

If there any way to access outer class fields from inner class instance, EXCEPT
Is there a way to access the local variable of a function from a
Is there any way to access let bound fields from a static member? The
I'm wondering if there is a way to access R class attribute from native
Is there any way to access the Windows Event Log from a java class.
Is there any way to access a variable in all of my methods in
just a very simple thing, is there a way to access outer class from
Is there some way to access a class variable in the constructor? var Parent
Is there a way to access one instance of a class inside functions in
Is there a way to access a method from an other class without creating

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.