0

I have 2 static variables like below

private static DateTime _currentPollStartDate = DateTime.MinValue; //As Default
private static DateTime _currentPollEndDate = DateTime.MinValue; //As Default

In a method, I try to set the values:

public void ProcessItems()
{
    var Items = GetItems();

    //In here, it reaches inside
    if (Items.HasItems)
    {
        //Items[0].PollStartDate.HasValue is TRUE
        //I can NOT set either Items[0].PollStartDate.Value or DateTime.MaxValue
        _currentPollStartDate = Items[0].PollStartDate.HasValue ? Items[0].PollStartDate.Value : DateTime.MaxValue;

        //Items[0].PollEndDate.HasValue is TRUE
        //I can NOT set either Items[0].PollEndDate.Value or DateTime.MaxValue
        _currentPollEndDate = Items[0].PollEndDate.HasValue ? Items[0].PollEndDate.Value : DateTime.MaxValue;
    }

    //...
}

But when I do this with IF I don't have the problem as stated above, why?

public void ProcessItems()
{
    var Items = GetItems();

    //In here, it reaches inside
    if (Items.HasItems)
    {
        if (Items[0].PollStartDate.HasValue)
            _currentPollStartDate = Items[0].PollStartDate.Value;
        if (Items[0].PollEndDate.HasValue)
            _currentPollEndDate = Items[0].PollEndDate.Value;
    }

    //...
}

Also, when I declare the variables not static this also solves my problem even though I use it like in my first code. But why can't I use both static and if statement as in my first code?

Edit: Expected Value: something like _currentPollStartDate -> 2013-04-18 10:03:03

Result Value: _currentPollStartDate -> 0001-01-01 00:00:00 (This is not even MAX value)

4

3 回答 3

3

感谢肯金。根据他的最新评论,我想使用属性而不是简单的声明。所以代替下面的:

private static DateTime _currentPollStartDate = DateTime.MinValue; //As Default
private static DateTime _currentPollEndDate = DateTime.MinValue; //As Default

我做了

private static DateTime CurrentPollStartDate { set; get; }
private static DateTime CurrentPollEndDate { set; get; } 

它奏效了。

于 2013-04-18T09:09:30.340 回答
1

static 的意思是“每个班级只有一次”。因此,您的类的所有实例都将使用您定义为静态的相同(按单词的确切含义)变量。要使您的代码正常工作,请使用:

YourClassName._currentPollStartDate = Items[0].PollStartDate.HasValue ? Items[0].PollStartDate.Value : DateTime.MaxValue;

我不知道您是否真的希望所有民意调查的开始和结束日期都相同,这意味着最新的民意调查会更改所有旧民意调查的开始和结束日期。

于 2013-04-18T08:42:41.863 回答
0

检查 and 的类型Items[0].PollStartDate.ValueDateTime.MaxValue当它们与三元运算符一起使用时,两个分支必须是相同的类型。

booland bool?, or intandint?不是同一类型。


更新:

我毫无问题地声明了以下内容:

class MyClassName {
    private static DateTime _currentPollStartDate=DateTime.MinValue; //As Default
    private static DateTime _currentPollEndDate=DateTime.MinValue; //As Default

    public void ProcessItems() {
        var Items=GetItems();

        //In here, it reaches inside
        if(Items.HasItems) {
            //Items[0].PollStartDate.HasValue is TRUE
            //I can NOT set either Items[0].PollStartDate.Value or DateTime.MaxValue
            _currentPollStartDate=Items[0].PollStartDate.HasValue?Items[0].PollStartDate.Value:DateTime.MaxValue;

            //Items[0].PollEndDate.HasValue is TRUE
            //I can NOT set either Items[0].PollEndDate.Value or DateTime.MaxValue
            _currentPollEndDate=Items[0].PollEndDate.HasValue?Items[0].PollEndDate.Value:DateTime.MaxValue;
        }

        //...
    }

    public void _ProcessItems() {
        var Items=GetItems();

        //In here, it reaches inside
        if(Items.HasItems) {
            if(Items[0].PollStartDate.HasValue)
                _currentPollStartDate=Items[0].PollStartDate.Value;
            if(Items[0].PollEndDate.HasValue)
                _currentPollEndDate=Items[0].PollEndDate.Value;
        }

        //...
    }

    Items GetItems() {
        return new Items();
    }
}

class Items: List<Item> {
    public bool HasItems {
        get;
        set;
    }
}

class Item {
    public DateTime? PollStartDate {
        get;
        set;
    }

    public DateTime? PollEndDate {
        get;
        set;
    }
}
于 2013-04-18T08:42:02.480 回答