Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello to all.

I want to create dynamic string that i can control from my app.config file

the idea is function with some given information:

C#
data(hello,big,world);

public static void data(string a, string b, string c)
{
var appSettings = ConfigurationManager.AppSettings;

string ValueA =
string ValueB =
string ValueC =

MessageBox.Show(ValueA +" "+ ValueB +" "+ ValueC);
}



<pre lang="xml">
<add key="ValueA" value="c"/>
<add key="ValueB" value="b"/>
<add key="ValueC" value="a"/></pre>

Whats i want to get as an output is:
world big hello

someone have an idea how can i achieve it ?

Thank you
Posted

Use a dictionary (key-value pairs) to keep the values than read them back according to app.config file;
C#
public static void data(string a, string b, string c)
{
  Dictionary<string, string> oDic = new Dictionary<string, string> ( );

  oDic.Add ( "a", a );
  oDic.Add ( "b", b );
  oDic.Add ( "c", c );

  string ValueA = oDic[ ConfigurationManager.AppSettings[ "ValueA" ] ];
  string ValueB = oDic[ ConfigurationManager.AppSettings[ "ValueB" ] ];
  string ValueC = oDic[ ConfigurationManager.AppSettings[ "ValueC" ] ];
 
  MessageBox.Show( ValueA + " " + ValueB + " " + ValueC );
}
 
Share this answer
 
v2
Comments
[no name] 7-Jan-15 8:53am    
its get me an error on not assigning the second value to the dictionary after i wrote that is a a string and assigned to id a.ToString() the program working.

Thank you a lot!
Kornfeld Eliyahu Peter 7-Jan-15 8:57am    
The editor messed with my code :-(...Please check the updated answer...
[no name] 8-Jan-15 1:04am    
yes that what i did, thank you.
You can create an ApplictionSetting using the Visual Studio Designer of Type 'HashTable if you need to have several key-value pairs of Type 'string or any other Type.

1. go into your Project's Properties folder in the Property Broweser, and double-click Settings.Settings: this will bring up the Settings Designer.

2. create a new setting by clicking in one of the blank rows of the Settings display Grid.

3. click on the 'Type menu-option to open the Type Browser: click 'Browse: open mscorlib/Collections.Generic and selct 'HashTable.

4. Set the setting scope to 'User, and give the new Setting an appropriate name; we'll use "HashSetting" for this example (no quotes).

In code, to use the Hash Setting:
C#
private Hashtable Hash;

private void btbAddToHash_Click(object sender, EventArgs e)
{
    Hash = new Hashtable
    {
        {"string1", "outcome1"},
        {"string2", "outcome2"}
    };

    // assign the current HashTable to the Setting's HashTable
    Properties.Settings.Default.HashSetting = Hash;

    // save the modified Setting(s)
    Properties.Settings.Default.Save();
}

// get a HashValue using its index
private string GetHashValue(string hashKey)
{
    return (string) Properties.Settings.Default.Hash1[hashKey];
}

// test in some method or EventHandler:
// assumes 'btnAddToHash was clicked before calling this

// test 1
string test1 = (string)Properties.Settings.Default.Hash1["string1"];

// test 2
string test2 = GetHashValue("string2");

Console.WriteLine("{0} : {1}", test1, test2);
 
Share this answer
 
Comments
[no name] 8-Jan-15 1:06am    
Thank you.

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