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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:28:16+00:00 2026-05-24T01:28:16+00:00

I have a function that does a data lookup and returns a value: public

  • 0

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?

  • 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-24T01:28:19+00:00Added an answer on May 24, 2026 at 1:28 am

    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

    public class MeterValues
    {
        public static Dictionary<int, int> meterList =  new Dictionary<int, int>();
        public static int ReturnValue(int meterValue)
        {
            if (meterList.Count == 0)
                LoadValues();
            return meterList.ContainsKey(meterValue) ? meterList[meterValue] : dal.GetLastBalance(meterNumber);
        }
        public static void LoadValues()
       {
          string[] _cValues = ConfigurationManager.AppSettings["Meter.Values"].ToString().Split(',');
          foreach(string val in _cValues)
          {
            string[] _param = val.Split(':');
            meterList.Add(Convert.ToInt32(_param[0]), Convert.ToInt32(_param[1]);
          }
       }
    }
    

    So your code would look like this

    public int LastBalance(int meterNumber)
    {
        return MeterValues.ReturnValue(meterNumber);
     }
    

    Your config file should have key

    <add key="Meter.Values" value="121:12,999:0,837:12" />
    

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say we have a function that does not interfere with other data and runs
This is a C# console application. I have a function that does something like
I have a small JS function that does Ajax for me and another like
Note that this function does not have a { and } body. Just a
I have a function that gets x(a value) and xs(a list) and removes all
I have a function that takes, amongst others, a parameter declared as int privateCount
Let's say I have some data that I need to get from the server
I have a function that gives me the following warning: [DCC Warning] filename.pas(6939): W1035
I have a function that looks like this class NSNode { function insertAfter(NSNode $node)
I have a function that I use called sqlf(), it emulates prepared statements. For

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.