[英]using concatenation on c#
What is the correct way to concatenate these elements? 连接这些元素的正确方法是什么?
Environment: C#-WinForm-Desktop. 环境:C#-WinForm-Desktop。
In my app, I have a text file called config.ini
. 在我的应用程序中,我有一个名为
config.ini
的文本文件。 This file has values with this nomenclature: 此文件的值具有以下术语:
csv_month_hour=value
csv_month_hour =值
The data are thus: 因此,数据为:
[csv]
csv_01_01=value // 24 hours a day in January
csv_01_02=value
csv_01_03=value
.
csv_02_01=value // 24 hours a day in February
csv_02_02=value
csv_02_03=value
.
// and so on
My app has this method called: 我的应用程序具有以下方法:
private void getMonthHour()
{
DateTime Xdt = DateTime.Now;
int Xmonth = Xdt.Month;
int Xhour = Xdt.Hour;
if (File.Exists("config.ini"))
{
IniFile ini = new IniFile("config.ini");
m3h = Convert.ToDouble(confg.csv["csv_" + Xmonth.ToString + "_" + Xhour.ToString]);
}
}
This method is called every seconds in timer_tick
to check the month and time to use the corresponding value. 在
timer_tick
中timer_tick
调用一次此方法,以检查月份和时间以使用相应的值。
I need to know how to concatenate this: m3h = Convert.ToDouble(confg.csv["csv_"+Xmonth.ToString+"_"+Xhour.ToString]); 我需要知道如何将其连接:m3h = Convert.ToDouble(confg.csv [“ csv _” + Xmonth.ToString +“ _” + Xhour.ToString]);
An example of the code would be: 该代码的示例为:
if (Xmonth==1&&Xhour==1){m3h = Convert.ToDouble(confg.csv.csv_01_01);}
else if (Xmonth==1&&Xhour==2){m3h = Convert.ToDouble(confg.csv.csv_01_02);}
else if (Xmonth==1&&Xhour==3){m3h = Convert.ToDouble(confg.csv.csv_01_03);}
// csv_month_hour in this case 01 is January.
else if (Xmonth==2&&Xhour==1){m3h = Convert.ToDouble(confg.csv.csv_02_01);}
// And so on, until December (12).
Putting aside the invalid syntax/compiler error from your method calls in your code, I suspect you're building the string but can't get the leading 0
in front of digits 0-9. 从代码中的方法调用中排除无效的语法/编译器错误,我怀疑您正在构建字符串,但无法在数字0-9前面获得前导
0
。 So when Xmonth
and Xhour
are less than 10, your string is being built as "csv_1_1"
instead of your intended "csv_01_01"
. 因此,当
Xmonth
和Xhour
小于10时,您的字符串将构建为"csv_1_1"
而不是预期的"csv_01_01"
。
To instruct the ToString
method to include a leading zero when needed, you can supply a custom numeric format string "00"
(see the first entry in the link for "Zero placeholder"). 要指示
ToString
方法在需要时包括前导零,可以提供自定义数字格式字符串 "00"
(请参见链接中的“零占位符”的第一个条目)。
Try using Xmonth.ToString("00")
and Xhour.ToString("00")
: 尝试使用
Xmonth.ToString("00")
和Xhour.ToString("00")
:
Monitor.malyt.m3h = Convert.ToDouble(confg.csv["csv_" +
Xmonth.ToString("00") + "_" +
Xhour.ToString("00")]);
This will output the values of Xmonth
and Xhour
with a single leading zero where needed. 这将在需要时输出
Xmonth
和Xhour
的值,并带有一个前导零。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.