简体   繁体   English

自定义日期时间结构

[英]A custom datetime struct

I am trying to develop a clock application with images for each digits from 0-9. 我正在尝试开发一个时钟应用程序,其中每个图像的数字从0到9。 Wrote a struct that gives me each digits every now and then. 编写了一个结构,该结构会不时地给我每个数字。 Following is the struct. 以下是该结构。

public struct TimeStruct
{
    public DateTime dt
    {
        get
        {
            return DateTime.Now;
        }
    }
    public int s
    {
        get
        {
            return dt.Second;
        }
    }
    public int s2
    {
        get
        {
            return s % 10;
        }

    }
    public int s1
    {
        get
        {
            return s / 10;
        }
    }
    public int m
    {
        get
        {
            return dt.Minute;
        }
    }
    public int m2
    {
        get
        {
            return m % 10;
        }
    }
    public int m1
    {
        get
        {
            return m / 10;
        }
    }
    public int h
    {
        get
        {
            return dt.Hour;
        }
    }
    public int h2
    {
        get
        {
            return h % 10;
        }
    }
    public int h1
    {
        get
        {
            return h / 10;
        }
    }
    public int d
    {
        get
        {
            return (int)dt.DayOfWeek;
        }
    }

}

Please guide me to modify this struct so that the prop s2 should be set only when s1 becomes 0. And the same with minutes. 请指导我修改此结构,以便仅在s1变为0时才设置prop s2。分钟也是如此。 Technology Used : Silverlight Platform : Windows Phone 7 使用的技术:Silverlight平台:Windows Phone 7

Was that a bad idea to use struct? 使用struct是个坏主意吗?

What do you mean by "prop s2 should be set only when s1 becomes 0" - what do you want it to do when s1 isn't 0? 您的意思是“仅当s1变为0时才应设置prop s2”-当s1 不为 0时您希望它做什么? Are you perhaps looking for nullable value types, where s1 would return the null value in some cases? 您是否正在寻找可空值类型,在某些情况下s1将返回空值?

I have to say, I think this is a pretty confusing type. 我不得不说,我认为这是一种令人困惑的类型。 It has no real state - it's effectively just a bunch of static properties. 它没有真实状态-实际上只是一堆静态属性。 Any reason for not implementing it as a bunch of static properties, eg in a CurrentDateTime class? 是否有理由不将其实现为一堆静态属性,例如在CurrentDateTime类中? Or just use DateTime.Now ? 或者只使用DateTime.Now Note that if you ask your struct for a bunch of values, one at a time, it may very well give you inconsistent results as time will pass. 请注意,如果您一次向结构体索要一堆值,那么随着时间的流逝,它很可能给您带来不一致的结果。 For example, suppose the time is 1:59:59 and you call s , then m , then h - you may end up getting 59, 59, 2 as the current time rolls over from 1:59:59 to 2:00:00 between the last two calls. 例如,假设时间为1:59:59,然后依次调用smh您可能最终得到59、59、2,因为当前时间从1:59:59转换为2:00:最近两个通话之间的00。 If you take the value of DateTime.Now just once and ask it for all its properties, you'll get a consistent view. 如果只取一次 DateTime.Now的值,并要求其提供所有属性,则将获得一致的视图。

Why re-invent the wheel ? 为什么要重新发明轮子? Use DateTime and TimeSpan . 使用DateTimeTimeSpan

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM