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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:33:27+00:00 2026-05-20T13:33:27+00:00

I doubt this is possible but what I would like to do is have

  • 0

I doubt this is possible but what I would like to do is have an argument’s type depend on the value of another argument. Because it is statically defined most of the time it should work when it can(in my case it will always be used statically).

e.g.,

suppose we had a function myFunc who’s first argument was an enum of type myEnum.

myFunc(myEnum.SomeValue);

But now suppose we wanted the second argument’s type to depend on the value of the first.

So

myFunc(myEnum.Value1, x)
myFunc(myEnum.Value2, y)

in this case x would have a type that is related to Value1 and y to Value2.

It should be perfectly valid for the compiler to directly determine the type of the 2nd argument if we could specify a map. We might need a new type to handle it but as long as the first argument is a compile time constant there should be no problem with it. One can use polymorphism to handle the various cases too or, in my case, using a factory.

The only type the compiler cannot deduce the type of the 2nd argument is when the first is not a compile time constant in which case a run time error can be thrown if the 2nd argument is not of the proper time.

The main point of doing this is to break a large enum into smaller ones that form a hierarchy. One could simply have a single large enum containing all the combinations but it gets out of control very quickly.

I seriously doubt C# supports such a feature but maybe there is a possible way to get it to work(using attributes or whatever)?


Example of behavior

    public enum Value1Types { Type1, Type2 }

    public enum Value2Types { TypeA, TypeB }

    public class FakeEnum
    {
        public class EnumTypes
        {
            public abstract class BaseEnum { public abstract int Value { get; } }
            public class Value1 : BaseEnum { public override int Value { get { return 1; } } }
            public class Value2 : BaseEnum { public override int Value { get { return 2; } } }
        }

        public static class EnumValues
        {
            // Singletons
            public static readonly EnumTypes.Value1 Value1 = new EnumTypes.Value1();
            public static readonly EnumTypes.Value2 Value2 = new EnumTypes.Value2();
        }

        public void AcceptValue(EnumTypes.Value1 typeController, Value1Types s) { Console.WriteLine("Value1 " + s.ToString()); }
        public void AcceptValue(EnumTypes.Value2 typeController, Value2Types s) { Console.WriteLine("Value2 " + s.ToString()); }

        public void Test()
        {
            // Notice how the same method's second arugment's type depends on the value of the first
            AcceptValue(EnumValues.Value1, Value1Types.Type1); // valid
            AcceptValue(EnumValues.Value2, Value2Types.TypeA); // valid
            //AcceptValue(EnumValues.Value1, Value2Types.TypeB); // Invalid, compile error
        }
    }
  • 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-20T13:33:28+00:00Added an answer on May 20, 2026 at 1:33 pm

    If you can replace the enums with explicit types you can make something like this.
    Personally I think it is to much work to get the last bit of compile time checking.

    public class FakeEnum
    {
        public class EnumTypes
        {
            public abstract class BaseEnum
            {
                public abstract int Value { get; }
            }
    
            public class Value1 : BaseEnum
            {
                public override int Value
                {
                    get { return 1; }
                }
            }
    
            public class Value2 : BaseEnum
            {
                public override int Value
                {
                    get { return 2; }
                }
            }
        }
        public static class EnumValues
        {
            // Singletons
            public static readonly EnumTypes.Value1 Value1 = new EnumTypes.Value1();
            public static readonly EnumTypes.Value2 Value2 = new EnumTypes.Value2();
        }
    
        public void AcceptValue(EnumTypes.Value1 typeController, string s)
        {
            Console.WriteLine("Value1 " + s);
        }
    
        public void AcceptValue(EnumTypes.Value2 typeController, int i)
        {
            Console.WriteLine("Value2 " + i);
        }
    
        public void Test()
        {
            AcceptValue(EnumValues.Value1, "with value1"); // valid
            AcceptValue(EnumValues.Value2, 1); // valid
            //AcceptValue(EnumValues.Value1, 4); // Invalid, compile error
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Doubt this is possible as I don't have a 1 page app, but pretty
I'm aware that questions like this have been asked before and I doubt it's
I got this doubt while writing some code. Is 'bool' a basic datatype defined
I would like to use Visual Studio 2008 to the greatest extent possible while
If I have an integer like this in several places in my code... int
I am almost sure that this is not possible, but I'm wondering if someone
I have a CSS3 button like this <button class="button brownb">My Button</button> CSS .brownb:active {
In my program I have currently a piece of code that looks like this
This is a question I probably should have asked sooner but didn't in my
This is something that made me doubt for a while so I thought it

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.