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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:01:46+00:00 2026-05-14T06:01:46+00:00

The values in a file are read as string and can be double, string

  • 0

The values in a file are read as string and can be double, string or int or maybe even lists. An example file:

DatabaseName=SomeBase
Classes=11;12;13
IntValue=3        //this is required!
DoubleValue=4.0

I was thinking something like this:

class ConfigValues
{
    private static SomeObject _utiObject;
    private static string _cfgFileName = "\\SomeSettings.cfg";
    private static Dictionary<string, Type> _settingNamesAndTypes = 
         new Dictionary<string, Type>();
    private static Dictionary<string, object> _settings = new Dictionary<string, object>();
    private static string _directory = string.Empty;
    const string _impossibleDefaultValue = "987ABC654DEF321GHI";

    public static T GetConfigValue<T>(string cfgName)
    {
        object value;
        if (_settings.TryGetValue(cfgName, out value))
            return (T)value;
        else
            return default(T);
    }

    public static bool LoadConfig(Dictionary<string, Type> reqSettings, 
          Dictionary<string, Type> optSettings,
          Dictionary<string, object> optDefaultValues, out string errorMsg)
    {
        errorMsg = string.Empty;

        try
        {
            _utiObject = new SomeObject(new string[] { "-c", CfgFileNameAndPath });
        }
        catch (Exception e)
        {
            errorMsg = string.Format("Unable to read {0}. Exception: {1}", 
              CfgFileNameAndPath, e.Message);
            return false;
        }

        foreach (KeyValuePair<string, Type> kVPair in reqSettings)
        {
            if (!ReadCheckAndStore(kVPair, null, out errorMsg))
                return false;

            _settingNamesAndTypes.Add(kVPair.Key, kVPair.Value);

        }
        foreach (KeyValuePair<string, Type> kVPair in optSettings)
        {
            if (!ReadCheckAndStore(kVPair, optDefaultValues[kVPair.Key], out errorMsg))
                return false;

            _settingNamesAndTypes.Add(kVPair.Key, kVPair.Value);
        }
        return true;
    }

    private static bool ReadCheckAndStore(KeyValuePair<string, Type> kVPair, object defaultValue, out string errorMsg)
    {
        errorMsg = string.Empty;
        string usedDefaultValue, value = string.Empty;

        /* required setting */
        if (defaultValue == null)
            usedDefaultValue = _impossibleDefaultValue;
        else
            usedDefaultValue = defaultValue.ToString();

        //all string parameters below
        _utiObject.GetConfigValue(kVPair.Key, usedDefaultValue, ref value);
        if (_impossibleDefaultValue == value)
        {
            errorMsg = string.Format("Required configuration setting {0} was not" +
               "found in {1}", kVPair.Key, CfgFileNameAndPath);
            return false;
        }
        Type type = kVPair.Value;

        _settings[kVPair.Key] = Convert.ChangeType(value, type);

        return true;
    }
}

PS. Additional issue is default values for optional settings. It’s not elegant to pass them to LoadConfig in separate Dictionary, but that is an other issue…

  • 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-14T06:01:46+00:00Added an answer on May 14, 2026 at 6:01 am

    The only way I can think of doing this is to have Dictionary<String,Object> and then cast the Object to the appropriate type.

    Your underlying problem is how to dynamically specify the type:
    Dynamically specify the type in C#

    Turns out that type casting (actually unboxing) in C# has very little overhead:
    C# performance analysis- how to count CPU cycles?


    Update:
    Here is how you do the casting:

    Dictionary<String,Object> parameters = new Dictionary<String,Object>();
    
    // cast a string
    parameters.Add("DatabaseName", "SomeBase");
    
    // cast a list
    parameters.Add("Classes", new List<int> { int.Parse("11"), int.Parse("12"), int.Parse("13") });
    
    // cast an integer
    parameters.Add("IntValue", int.Parse("3"));
    
    // cast a double
    parameters.Add("DoubleValue", Double.Parse("4.0"));
    

    Then when you want to use the values you just do the unboxing:

    int intValue = (int)parameters["IntValue"];
    Double doubleValue = (Double)parameters["DoubleValue"];
    List<int> classes = (List<int>)parameters["Classes"];
    // etc...
    

    As I mentioned before: after doing performance testing I found that unboxing has negligent overhead so you should not see any noticeable performance issues.


    Update 2.0:

    I’m guessing you want to automatically convert the type without having to explicitly specify it when you’re adding it into the _settings dictionary. It should work if your dictionary’s value is an Object:

    Type t = typeof(double);
    Object val = Convert.ChangeType("2.0", t);
    // you still need to unbox the value to use it
    double actual = (double)val;
    

    So your example should work:

    _settings[kVPair.Key] = Convert.ChangeType(value, type);
    

    You just need to unbox the value with the correct type when you’re going to use it.

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

Sidebar

Ask A Question

Stats

  • Questions 498k
  • Answers 498k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Assuming that you have NSURL named something like myURLWithSymlinks, you… May 16, 2026 at 12:23 pm
  • Editorial Team
    Editorial Team added an answer Using NOW() in the query would provide this. else{ //If… May 16, 2026 at 12:23 pm
  • Editorial Team
    Editorial Team added an answer Msmq transactions do not guarantee that the receiver has received… May 16, 2026 at 12:23 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I download one program that read file and then parse double values from String
I'm trying to load some decimal values from a file but I can't work
I'm writing a simple import application and need to read a CSV file, show
I have the following string in a file called mib.txt: [name=1.3.6.1.2.1.1.5.0, value=myrouter.ad.local (OCTET STRING)]
I've written my own code to parse an .obj model file - essentially just
let's hope I can make this non-sujective Here's the thing: Sometimes, on fixed-typed languages,
I've got a CSV file with a format that looks like this: FieldName1, FieldName2,
i am facing one problem. i want to save settings in app.config file i
In my app, i have an import option, to read info from a .csv
My input file is going to be something like this key value key value

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.