Travis Travis - 5 months ago 21
C# Question

Is there a way to use a string in a variable's name when calling it?

I am new to programming so excuse my newbieness.
I'm using Visual Studio and in my program I have some variables in the Settings that are named by months;








So in my code when I assign them values I have:

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

They just add up values that are entered to get a total.

But I was trying to keep my code neater and was wondering if there was a way, based off of the month the user selects it will change the month name...


string month = txtBoxMonth.Text;

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

That will then keep me from having to create a gigantic switch statement for every month.
I don't know if there is a way to do that or not, but any help is apprecieated.


You mention that you are currently storing these values within your settings file.

You can access your settings via key:

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