C# - 获取行号和字符串 [英] C# - Getting a line number and the string

查看:81
本文介绍了C# - 获取行号和字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码



this is my code

int i = 0;
   foreach (string item in Properties.Settings.Default.DatesEventNames)
   {
       //add the headings
       if ((i++ % 3) == 0)
       {
           //add label
           Label neweventlabel = new Label();
           neweventlabel.Content = item;
           SP1.Children.Add(neweventlabel);

           DateTime yourDateTime = DateTime.Parse(Properties.Settings.Default.DatesEventNames[i]);
           DateTime yourDateTime2 = DateTime.Parse(Properties.Settings.Default.DatesEventNames[i + 2]);

           //add progressbar
           ProgressBar pb = new ProgressBar();
           pb.Maximum = (yourDateTime - yourDateTime2).TotalDays;
           pb.Value = (yourDateTime - DateTime.Now.Date).TotalDays;
           pb.Height = 20;
           SP1.Children.Add(pb);
       }
   }





这是stringcollection中的样子





this is what is looks like in the stringcollection

event1<br />
10/01/2013<br />
13/01/2013<br />
event3<br />
12/01/2013<br />
14/01/2013





此代码当前的代码是在stringcollection中抓取每三个字符串所以它看起来像这样





what this code dose at the moment is grabs every third string in stringcollection so it looks like this

event1<br />
event2





但我想在它们之间输出一个进度条,所以它看起来像这样





but i want to out a progressbar inbetween them so it looks like this

event1<br />
|-----progressbar-----|<br />
event2<br />
|-----progressbar-----|





现在我试图将值提供给进度条。例如,我想要来自event1的这些价值





right now im trying to give the values to the progressbars. for example I want these vales from event1

Start Date - 10/01/2013<br />
End Date - 13/01/2013<br />
Todays Date - ??/??/????





所以它会有进度条





so for the progress bar it will have

The maximum = (start date to end date)<br />
The Value = (todays date to the end date)





我正在使用trubble获取日期中字符串的约定值



所以我们就这行代码说吧



Im having trubble getting the values for the convention in the strings in the dates

So lets say on this line of code

int i = 0;
          foreach (string item in Properties.Settings.Default.DatesEventNames)
          {
              //add the headings
              if ((i++ % 3) == 0)
              {
}

}





让我们说它落在event2上,我希望它得到字符串行号只能说它的第3行,然后我希望它下降1行号,这将使它成为第4行,第4行将是日期。这将给我价值。我希望这可以解释它,请重新阅读这个问题,以防你错过了什么。



Summery

问题:在字符串中查找日期时间的值收藏

问题:你能帮我找到这个问题的答案吗?



lets say it lands on event2, I want it to get that strings line number lets just say its line 3, then I want it to go down 1 line number, which will make it line 4, and in line 4 would be the date. and this will give me the values. I hope this explains it, please re-read this question in-case you miss something.

Summery
Problem: Finding the values for the datetime in the string collection
Question: Can you help me find the answer to this problem?

推荐答案

当然,这个问题并不难解决。但为什么?你是从错误的一方来解决这个问题的。



这是现在典型的初学者错误之一:尝试使用字符串表示数据代替数据本身,然后不得不处理相关的问题,花费你的时间,值得更好地使用。



如果你想收集一些字符串和时间的东西数据,创建一个包含字符串,时间和其他成员的类或结构。制作此类物品的集合。大多数集合类型都是通用的,因为.NET v.2.0,所以集合与项目类型无关。



所有的痛苦都会减少到几行非常清楚可读代码。



-SA
Of course, this problem is not hard to solve. But why? You are coming to the problem from a wrong side.

This is one of the typical beginners'' mistakes these days: an attempt to work with string representation of data instead of data itself, and then having to deal with related problems, spending your time which deserves better use.

If you want to have a collection of something with string and time data, create a class or a structure with string, time and other members. Make a collection of items of this type. Most collection types are generic, since .NET v.2.0, so collections are agnostic to the item types.

All your suffering will be reduced to few line of very clear and readable code.

—SA


这篇关于C# - 获取行号和字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆