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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:13:53+00:00 2026-06-05T02:13:53+00:00

This is my first time posting on Stack Overflow, so hopefully I did everything

  • 0

This is my first time posting on Stack Overflow, so hopefully I did everything right and you guys can help.

I’m wondering if in C# there’s a way to access a static variable belonging to a class, when given only the type of the class. For example:

public class Foo
{
    public static int bar = 0;
}

public class Main
{
    public void myFunc(Type givenType)
    {
        int tempInt = ??? // Get the value of the variable "bar" from "Foo"
        Debug.WriteLine("Bar is currently :" + tempInt);
    }
}

// I didn't run this code through a compiler, but its simple enough
// that hopefully you should get the idea...

It’s hard to describe the context of needing to know this, but I’m making a game in XNA and I’m trying to use reference counting to reduce the complexity of the design. I have objects in the game and power-ups that can apply an effect them (that stays on the objects). Power-ups can die but their effects can still linger on the objects, and I need to keep track of if any effects from a power-up are still lingering on objects (thus, reference counting). I plan to make a “PowerUpEffect” class (for each type of power-up) with a static integer saving the number of objects still affected by it, but the design of the rest of the game doesn’t work well with passing the PowerUpEffect all the way down to the object for it to call a method of the PowerUpEffect class.

I’m hoping to pass only the PowerUpEffect’s type (using something like “typeOf()”) and use that type to reference static variables belonging to those types, but I have no idea how to do it or if it’s even possible.

I’d be glad to even find work-arounds that don’t answer this questions directly but solve the problem in a simple and elegant design. =)

Help! (and thanks!)

  • 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-05T02:13:55+00:00Added an answer on June 5, 2026 at 2:13 am

    If you only have the Type handle, you can do this:

    var prop = givenType.GetProperty("bar");
    var value = prop.GetValue(null);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First time posting in Stack Overflow, hope I'm doing this right. I'm writing an
This is my first time posting to Stack Overflow. I'm trying to build a
First time posting here, will try to be succinct. This is a classic 'can't
Hey guys, this is my first time actually posting at stackflow but i've used
First time posting on stack overflow. I've spent hours scouring over many Google searches
this is the second time I am posting this on stack overflow forum, the
this is my first time posting my problem here, i hope i get help
this is my first time posting on StackOverflow but this site is awesome. Thanks
That's my first time posting on stackoverflow. I've been finding usefull answers on this
this is my first time posting here, I have a question which I have

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.