Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
i would like to use GreaterThanorEqual and Equal to and LessThanorEqual in ENUM which mean my ENUM will be like below

C#
enum StringCompare { strA_is_less_than_strB <= -1, strAequals_strB = 0, strA_is_greater_than_strB >= 1 }


shall i use something like or is there is any other way to use tlike this?
Posted
Comments
DamithSL 16-Jun-14 1:54am    
what is the purpose of defining enum like this?
Gold$Coin 16-Jun-14 2:07am    
so that i can use the ENUM if some result exceeds certain value. as like string Compare as like above example. there was a requirement in my project to dispaly a value like this in datagrid i like to do in this way but ENUM wont support for this so i would like to know if any other possible way to resolve this?

You are trying to use an enum in a way it has not been made for; each element of the enum has to have an unique, constant value.

What you need is a function returning a value of your enum according to the input. Something like:
C#
enum StringCompare { strA_is_less_than_strB, strAequals_strB, strA_is_greater_than_strB }

public StringCompare GetStringCompare(string lhs, string rhs) {
   // Here you have to define what you mean by string comparison
   // Two strings can be equal or not, but how do you decide whether a string is greater, or lesser, than another one?
   // From their sizes? From the value of the bytes they are composed of?
   return
      (lhs < rhs) ? StringCompare.strA_is_less_than_strB :
      (lhs > rhs) ? StringCompare.strA_is_greater_than_strB :
      StringCompare.strAequals_strB;
}
 
Share this answer
 
Comments
Gold$Coin 16-Jun-14 4:55am    
Thanks phil looks like it would be a correct solution for this. thanks for all :)
You may use like the following:

C#
enum StringCompare { strA_is_less_than_strB = -1, strAequals_strB , strA_is_greater_than_strB  }
 
Share this answer
 
Comments
Gold$Coin 16-Jun-14 2:08am    
what if the given value decresses to -2 so enum wont parse correct
You try it in a strange way. Look at the functions Enum.IsDefined or Enum.GetValues. Unfortunately, there is no Enum.TryParse function, but you could write an Extension method for that.
 
Share this answer
 
Comments
Gold$Coin 16-Jun-14 4:54am    
Hope i have told i don't want the exact way. i have asked is there is some other way to achieve my task?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900