开发者

best practice - string comparison

I have a lookup table with an id and a text value.

What is the best way to check if a value is x or y? Hardcoding the string? Using an "enum"?

EDIT: What I mean is, is it ok to just do:

if (VALUE开发者_运维问答_FROM_DATABASE == "value x")
then do this

What if the values in the database change in the future? I guess there might not be a way around that and we'll always have to change the code when that happens?


just use simple if:

if(value=="myString") {}  

I do not see here a reason for Enum


if(String.Compare(string, compareString,true) == 0)
{

}


If you have a lookup table and you are reading the values from a table, etc you cannot use switch statement since it must be a constant.


I tend to put a SystemId value on any lookup table on which change the way the program works, and then create a matching enum to reference it in code (although if I'm using NHibernate, I just use the enum). If I were to be completely paranoid strict, I'd create a check constraint and a unique on the SystemId column too to stop anyone adding anything to it without really having to try hard.


it depends on how many different values you are comparing with. If one or two, then go a head and use an if statement and the equal sign.

Do not use lots of if statements. It might be better to use a switch statement then or a Dictionary to find acceptable values.


There is an Equals method on strings that you should use for comparison so something like

if(value.Equals(stringToCompareTo)){}

There is an overload to that method which takes a value from the StringComparison enum so if you wanted to ignore the case in your comparison you would do something like

if(value.Equals(stringToCompareTo,StringComparison.OrdinalIgnoreCase)){}


On the enum question (after your edit)

Possibly you mean that your database lookup table has, say, enum-style values "In store|Out of stock|Unavailable". If you want to use enums in your code, you have to jump through some hoops:

  1. Create an extra field in the table, this is a sticky field and shouldn't change
  2. The other fields (description / name) are allowed to change
  3. Create an enum, create appropriate methods in your DAO to make sure that any getter/setter only accepts that enum 3a. Same is true for other fields in other tables that are referenced to this field
  4. Raise exception when a value outside the reach of the enum is stored or found
  5. This is quite some work to do by hand, if this pattern comes along more often, automate it with code creation.

Original answer, possibly void after edit of OP:

Something like:

// get your value from the database (don't know what you use now):
string value = lookupTable.GetById(12345).Text;
if(value == "x" || value == "y") 
{
    // do your thing
} 

But then I'm assuming a lot about your current code, of which you don't show much.

EDIT: you ask whether you should change your code when your database's value changes. No, not needed. Each time your run your code, you retrieve the value from your database, and there won't be a problem.


store that string on the configuration

if (VALUE_FROM_DATABASE == ValueFromConfig) //...then do this

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜