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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:35:01+00:00 2026-06-01T08:35:01+00:00

I am playing around with trying to make a simple Roguelike game to learn

  • 0

I am playing around with trying to make a simple Roguelike game to learn C# a bit better. I am trying to make a general method that I can give it an Enum as an argument, and it will return how many elements are in that Enum as an int. I need to make it as general as possible, because I will have several different classes calling the method.

I have searched around for the last hour or so, but I couldn’t find any resources here or otherwise that quite answered my question… I’m still at a beginner-intermediate stage for C#, so I am still learning all the syntax for things, but here is what I have so far:

// Type of element
public enum ELEMENT
{
    FIRE, WATER, AIR, EARTH
}


// Counts how many different members exist in the enum type
public int countElements(Enum e)
{
    return Enum.GetNames(e.GetType()).Length;
}


// Call above function
public void foo()
{
    int num = countElements(ELEMENT);
}

It compiles with the error “Argument 1: Cannot convert from ‘System.Type’ to ‘System.Enum'”. I kind of see why it won’t work but I just need some direction to set everything up correctly.

Thanks!

PS: Is it possible to change the contents of an enum at runtime? While the program is executing?

  • 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-01T08:35:02+00:00Added an answer on June 1, 2026 at 8:35 am

    Try this:

    public int countElements(Type type)
    {
        if (!type.IsEnum)
            throw new InvalidOperationException();
    
        return Enum.GetNames(type).Length;
    }
    
    public void foo()
    {
        int num = countElements(typeof(ELEMENT));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am just playing around and trying to make a very simple CMS. Right
Ive been playing around with delegates trying to learn and I ran into one
I am trying to learn Ruby on Rails by playing around with stuff and
I am just playing around to understand smart pointers and trying to make mine
I am trying to make a checkbox smaller. I have tried playing around with
I'm playing around with ASP.NET MVC3 and I'm trying to do a simple Sign
Playing around with Timers, and trying to make them behave right when app is
I am trying to make a simple text-based game in Python. The idea of
Playing around with Lucene. Trying to make my changes visible for other reading threads.
I am playing around with SVN trying to get a better feel for how

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.