Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;

JanuaryTotalAmount

JanuarySpentAmount

JanuaryGainedAmount

FebruaryTotalAmount

FebruarySpentAmount

FebruaryGainedAmount

ect...

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...

So

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.

like image 398
Travis Avatar asked Mar 11 '23 05:03

Travis


1 Answers

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];
}
like image 152
plusheen Avatar answered May 02 '23 00:05

plusheen