在C#中为变量名添加一个数字 [英] Adding a number to variable names in C#
问题描述
我的问题类似于这里的一个:
我如何在C#中动态地命名变量?
但是它有点不同,所以我想知道它是否可能。
我正在尝试从.settings文件读取一堆字符串。
我有他们都命名为Time1,Time2,Time3等...
我希望用户能够添加更多的时间到文件,所以可能有100以上。
我在设置文件中有一个大小,可以跟踪时间变量的数量。
我想写一些将在所有时间字符串中读取的内容。我认为用100个时间变量预先填写.settings文件将是愚蠢的,所以我知道他们在那里,然后手动阅读每个。
所以我想知道是否有一种方法我可以读取Timei或Time + i,其中我是一个整数,我可以放入一个循环,将找到它们。
(请注意,数据是字符串不是时间,它将被转换为星期几)
如:(天数来自ApplicationSettingsBase [aka file add new Settings1.settings]
public static int AvDaysIndex = Days.Default.Size; //这个数目包含天数
public static DayOfWeek [ ] AvailableDays = new DayOfWeek [AvDaysIndex]; //这是我想在所有变量中读取的地方Aka Time1 Time2 Times3
public ReadInDays()//从.settings读取文件
{
如果(AvDaysIndex> 0)//确定有几天读取
{
int I = 0;
//我可以手动执行以下
可用日期[I ++] = Days.Default.Time1;
可用日期[I ++] = Days.Default.Time2;
可用日期[I ++] = Days.Default.Time3; // etc ...
//有没有办法做这样的
为(int i = 0; i< AvDaysIndex; i ++)//每次读取
{
可用日期[i] = Days.Default.Time + i; //我将被添加到变量名称以找到它?
//或类似
的可用日期可用日期[i] = Days.Default.Time(I.tostring())
}
}
}
希望所有这些至少使我清楚我想要做什么。
编辑 - 我开始认为我的问题实际上是与.settings文件。如果我只是从另一个文件类型中读取值,其中值没有名称,我可以很容易地读取它们,即使文件中有可变数量的元素。
$ b $ (int i = 0; i<索引; i ++)
{$($)
AvailableDays [i] = getFromFile(_Days.Default.Properties [Time+(i + 1).ToString()]。DefaultValue.ToString());
AvailableTimes [i] = Convert.ToDateTime(_Times.Default.Properties [Time+(i + 1).ToString()]。DefaultValue);
}
这一切都在弄清楚如何从.settings文件读入,而不是直接读取Days.Default.Time1;我必须从Days.Default.Properties执行一般的查找,然后我可以创建一个动态名称并找到它。你们可能试图告诉我怎么做,我只是不明白。
再次感谢所有帮助的人。
如前所述,哈希表或字典可能会为您服务。如果你去字典路由,你可以在类上创建一个string / int索引器,你可以稍微改变你的代码:
示例索引器类:
public class Default
{
private Dictionary< int,DayOfWeek> _values = new Dictionary< int,DayOfWeek>();
public DayOfWeek this [int index]
{
get
{
if(_values.ContainsKey(index))
return _values [指数];
else
返回null;
}
set
{
_values [index] = value;
}
}
}
原始:
AvailableDays [i] = Days.Default.Time(I.tostring())
pre>
将成为:
AvailableDays [i] = Days。 Default.Time [I];
反射一直是一个选项,我在Windows控制台应用程序中有一个例子: / p>
public class Default
{
public int Time1 {get;组; }
public int Time2 {get;组; }
public int Time3 {get;组;
}
class程序
{
static void Main(string [] args)
{
默认d = new Default();
类型t = d.GetType();
foreach(var info in t.GetProperties())
{
//设置值
info.SetValue(d,1);
}
foreach(var info in t.GetProperties())
{
// GET VALUE
Console.WriteLine(Property:{0 },info.Name);
Console.WriteLine(Value:{0},info.GetValue(d));
}
//或只有一个属性
Console.WriteLine(Time1属性值:{0},t.GetProperty(Time1)。GetValue(d) );
Console.ReadLine(); //处理后暂停控制台
}
}
在您使用反射的示例中:
Days.Default.GetType()。GetProperty Time+ I.ToString())。GetValue(Days.Default)as DayOfWeek;
My question is sort of like the one found here: How do I name variables dynamically in C#? However its a bit different so I'm wondering if its possible.
I'm trying to read in a bunch of strings from a .settings file. I have them all named Time1, Time2,Time3 etc... I want the User to be able to add more Times to the file so there could be Time100 or more. I have a Size in the settings file that will keep track of the amount of Time Variables. I want to write something that will read in all of the Time strings. I think it would be silly to pre-fill the .settings file with 100 Time Variables so I know they are there and then manually read in each one.
So I'm wondering if there is a way where I can read in Timei or Time+i where I is an integer that I can put in a loop that will find them all. (note that the data is string not time, its just going to be converted to days of the week) Such as: (Days is from ApplicationSettingsBase [aka file add new Settings1.settings]public static int AvDaysIndex = Days.Default.Size; //This holds the number of items in Days public static DayOfWeek[] AvailableDays = new DayOfWeek[AvDaysIndex]; //This is where I wants to read in all the variables Aka Time1 Time2 Times3 public ReadInDays() //Reads in from the .settings File { if(AvDaysIndex>0) // Makes sure there is something in Days to read { int I=0; //I can Manually do the following AvailableDays[I++] = Days.Default.Time1; AvailableDays[I++] = Days.Default.Time2; AvailableDays[I++] = Days.Default.Time3; //etc... //Is there a way to do something like this for (int i = 0; i < AvDaysIndex; i++) //reads in each time { AvailableDays[i] = Days.Default.Time +i;//where I would be added to the variable name to find it? //Or something like AvailableDays[i] = Days.Default.Time(I.tostring()) } } }
Hopefully all that at least makes it clear what I'm trying to do.
Edit - I'm starting to think my issue is actually with the .settings file. and that if I just read values in from another file type where the values don't have names I can easily read them in even though there is a variable number of elements in the file.
Solution -
for (int i = 0; i < Index; i++) { AvailableDays[i] = getFromFile(_Days.Default.Properties["Time" + (i+1).ToString()].DefaultValue.ToString()); AvailableTimes[i] = Convert.ToDateTime(_Times.Default.Properties["Time" + (i + 1).ToString()].DefaultValue); }
It was all in figuring out how to read in from the .settings file and instead of reading it in directly aka Days.Default.Time1; I had to to do a generic lookup from Days.Default.Properties and then I could create a dynamic name and find it. You guys probably were trying to tell me how to do this, I just didn't understand.
Thanks again to all those that helped.
解决方案As already mentioned a hashtable or a dictionary would probably serve you best. If you go the dictionary route you can create a string/int indexer on the class and you would be able to alter your code slightly:
http://msdn.microsoft.com/en-us/library/2549tw02%28v=vs.80%29.aspx - Example of creating indexer on a class:
Example Indexer Class:
public class Default { private Dictionary<int, DayOfWeek> _values = new Dictionary<int,DayOfWeek>(); public DayOfWeek this[int index] { get { if (_values.ContainsKey(index)) return _values[index]; else return null; } set { _values[index] = value; } } }
Original:
AvailableDays[i] = Days.Default.Time(I.tostring())
Would become:
AvailableDays[i] = Days.Default.Time[I];
Reflection is always an option too and i have an example below that is in a Windows Console Application:
public class Default { public int Time1 { get; set; } public int Time2 { get; set; } public int Time3 { get; set; } } class Program { static void Main(string[] args) { Default d = new Default(); Type t = d.GetType(); foreach (var info in t.GetProperties()) { //SET VALUE info.SetValue(d, 1); } foreach (var info in t.GetProperties()) { //GET VALUE Console.WriteLine("Property: {0}", info.Name); Console.WriteLine("Value: {0}", info.GetValue(d)); } //OR JUST ONE PROPERTY Console.WriteLine("Time1 Property Value: {0}", t.GetProperty("Time1").GetValue(d)); Console.ReadLine();//PAUSE THE CONSOLE AFTER PROCESSING } }
In your example using reflection:
Days.Default.GetType().GetProperty("Time" + I.ToString()).GetValue(Days.Default) as DayOfWeek;
这篇关于在C#中为变量名添加一个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!