Jump to content
  • Advertisement
Sign in to follow this  
kanato

[.net] generic constraint?

This topic is 4353 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is it possible to constrain a generic type parameter to be an enumeration? The closest I can find is the struct constraint, but I want to be able to compare a generic type instance to zero. What I'm doing is creating a list of items each which has a unique identifier which is a member of an enum. I want to be able to call a function on that list and have it find the one specified, unless the specified id. value is zero, in which case I want it to execute some default behavior. I had this all working without using generics just using different classes with a bunch of duplicated code. I need to do this with a couple more and I would like to avoid duplicating code. I'm open to alternate solutions/suggestions, if anybody has any ideas.

Share this post


Link to post
Share on other sites
Advertisement
Well, you can use structs to kind of "mimick" enums by hiding the constructor and having static properties that return an instance of the struct. For example:



public struct EnumLOL
{
private int number;

private EnumLOL( int num )
{
number = num;
}

public static EnumLOL ROFL
{
get { return new EnumLOL( 0 ); }
}

public static EnumLOL WTF
{
get { return new EnumLOL( 1 ); }
}

public static EnumLOL OMG
{
get { return new EnumLOL( 2 ); }
}
}



And then, if you need to compare to zero, just overload object.Equals( object ), like so:


public override bool Equals( object obj )
{
Type objtype = obj.GetType( );
int num;

if( objtype == typeof( int ) )
{
num = (int)obj;
}
else if( objtype == typeof( EnumLOL ) )
{
num = ( (EnumLOL)obj ).number;
}

return this.number == num;
}



That's kind of hacky, but it does what you want (I think...).

Also, I know you from GameFAQs GD&P.

Share this post


Link to post
Share on other sites
Yes, I guess that seems to be a reasonable solution. I guess the constraint could be something like where T : struct, IEquatable<T>, IEquatable<int>.

What's your gamefaq's handle? I just started coming here recently. I like the more specialized boards that are here.

Share this post


Link to post
Share on other sites
Quote:
Original post by kanato
Yes, I guess that seems to be a reasonable solution. I guess the constraint could be something like where T : struct, IEquatable<T>, IEquatable<int>.

What's your gamefaq's handle? I just started coming here recently. I like the more specialized boards that are here.


I'm mrCube on there. You've helped me out literally every time I had a .net related question :D

I've been coming here for a while, but I usually lurk, don't post much.

Share this post


Link to post
Share on other sites
oh ok, I definitely know who you are :) I didn't recognize nsto119 from GameFAQs, but I thought I should know you if you post anything about .NET over there, since that's mostly all I pay attention to these days.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!