2016-09-06 8 views
4

私は初心者ですので、初心者です。 私はVisual Studioを使用しています。私のプログラムでは、設定でいくつかの変数を月単位で指定しています。 変数を呼び出すときに変数名に文字列を使用する方法はありますか?

JanuaryTotalAmount 

JanuarySpentAmount 

JanuaryGainedAmount 

FebruaryTotalAmount 

FebruarySpentAmount 

FebruaryGainedAmount 

ect... 

は、だから私のコードで私は彼らが私が持っている値割り当てるとき:彼らはちょうど合計を取得するために入力された値を足し

Properties.Settings.Default.JanuaryTotalAmount += EnteredAmount; 
Properties.Settings.Default.SpentAmount -= EnteredAmount; 

しかし、私は私のコードをすっきりを維持しようとしていたユーザーが、それは月の名前を変更します選択した月のオフに基づいて、道があった場合には思っていた...

ので

string month = txtBoxMonth.Text; 

Properties.Settings.Default."month"TotalAmount += TotalAmount 

これは、毎月の巨大なswitch文を作成しなくて済むようにします。 これを行う方法があるかどうかわかりませんが、どんな助けでも感謝しています。

+5

代わりに 'Dictionary'を使うとどうなりますか? – zerkms

+1

月名をキーとする辞書と列挙型。 –

+0

金額の種類は何ですか?それは 'int'、' float'、 'decimal'、...ですか? –

答えて

5

現在、これらの値は設定ファイル内に保存されています。あなたは、キーを経由して、あなたの設定にアクセスすることができます

:他の人が提案してきたように

public void GetMonthAmount(string month) 
{ 
    string keyName = month + "TotalAmount"; 
    object monthData = Properties.Settings.Default[keyName]; 
} 
+3

これは、賢明な方法でOPのコードを拡張する良い解決策です。しかし、OPは彼がプログラミングに慣れていないと言っているので、あなたはおそらくその関数を(ゲッターは 'void'を返すべきではありません)行うようにしなければなりません。また、セッターがいいだろう。 –

+0

皆さん、ありがとうございました。私はこれを受け取り、私がやっていたことに合うようにそれを少し修正することができました。彼らがお金の金額を表しているので、私は小数点以下を扱っています。だからこれは私がしたことです... ======================= ===================== 文字列キー=月+ "TotalAmount"; decimal tempDec = Convert.ToDecimal(Properties.Setting.Default [key]); tempDec + = Convert.ToDecimal( "EnteredAmount"); Properties.Settings.Default [key] = tempDec; //プロパティの設定を新しい値に設定します。 Properties.Setting.Default.Save(); – Travis

-1

1つのオプションは、Reflectionを試してみてください。反射を使用して、SetValueGetValueの関数を使用して名前でプロパティ値を設定/取得できます。
新しいものですので、もっと詳しく読む必要がありますので、参考のためにリンクを投稿してください。
http://www.tutorialspoint.com/csharp/csharp_reflection.htm
http://www.dotnetperls.com/reflection-property
https://msdn.microsoft.com/en-us/library/axt1ctd9(v=vs.110).aspx

C#コード例(objは、プロパティを持つオブジェクトです):

Type type = obj.GetType(); 
System.Reflection.PropertyInfo propertyInfo = type.GetProperty("JanuaryTotalAmount "); 
propertyInfo.SetValue(obj, valueToSet, null); 

だから今、あなたの必要性に従ってロジックを作成。

3

、あなたはまた、定義enumとして定義されたキーと、これらの値を保存するためにDictionary<>を使用することができます。ただし、直接このタイプの設定値を持つことができませんので、あなたはクラスでラップする必要があると思います:

public enum Month 
{ 
    January, 
    February, 
    // and so on... 
    December 
} 


public class Amounts 
{ 
    public Amounts() 
    { 
     Months = new Dictionary<Month, int>(); 
    } 

    public Dictionary<Month, int> Months { get; set; } 
} 

あなたは、あなたののそれぞれについて、あなたの設定に値を追加することができますを過ごし、量を獲得し、このようにそれらにアクセス:

Properties.Settings.Default.TotalAmounts = new Amounts(); 

Properties.Settings.Default.TotalAmounts.Months[Month.February] = 5; 
+0

良い解決策ですが、OPにAmounts:TotalAmounts、SpentAmounts、およびGainedAmountsの3つの辞書が必要になるでしょう。 –

+0

@ GoodNightNerdPride - それもうまくいく –

0

は、あなたの助けのために皆に感謝ので、これは私が提供する答えを把握することができたものです。そして、はい、私はお金の金額を表す小数でぶつかっていました。

string key = month + "TotalAmount"; 
decimal tempDec = Convert.ToDecimal(Properties.Setting.Default[key]); // Creates a decimal to store the setting variable in using the key to access the correct setting variable 
tempDec += Convert.ToDecimal("EnteredAmount"); // Adds the value of the Setting variable to the amount entered. 
Properties.Settings.Default[key] = tempDec; // Then sets the Setting variable to equal the temp variable. 
Properties.Setting.Default.Save(); 

これは素晴らしい動作をし、多くの部屋を節約します。

関連する問題