I have a function that does a data lookup and returns a value:
public int LastBalance(int meterNumber)
{
// Return value from data access layer
return dal.GetLastBalance(meterNumber);
}
For a specific meterNumber value I want to return 0 (zero).
Is this OK?
public int LastBalance(int meterNumber)
{
if(meterNumber = 999)
{
return 0;
}
else
{
// Return value from data access layer
return dal.GetLastBalance(meterNumber);
}
}
Is there a way I can do this without hardcoding a value (999 in this case)? What if in the future I have other “magic numbers” that I need to return specific values for?
You can use your web.config or app.config to define these values, then read those values from config file and return them, example
Create custom class which will read values from config and return you real value
So your code would look like this
Your config file should have key
so for value 123 it will return 12 for value 999 will return 0, and for any other value that is not in the list will call your dal method.
This can be also don via DataBase, store values and just append them in LoadValues() method