开发者

A custom datetime struct

开发者 https://www.devze.com 2023-01-06 23:59 出处:网络
I am trying to develop a clock application with images for each digits from 0-9. Wrote a struct that gives me each digits every now and then. Following is the struct.

I am trying to develop a clock application with images for each digits from 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. Technology Used : Silverlight Platform : Windows Phone 7

Was that a bad idea to use 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? Are you perhaps looking for nullable value types, where s1 would return the null value in some cases?

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, e.g. in a CurrentDateTime class? Or just use 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. If you take the value of DateTime.Now just once and ask it for all its properties, you'll get a consistent view.


Why re-invent the wheel ? Use DateTime and TimeSpan.

0

精彩评论

暂无评论...
验证码 换一张
取 消