c# - Why I cannot set ANY value in IF condition? -


i have 2 static variables below

private static datetime _currentpollstartdate = datetime.minvalue; //as default private static datetime _currentpollenddate = datetime.minvalue; //as default 

in method, try set values:

public void processitems() {     var items = getitems();      //in here, reaches inside     if (items.hasitems)     {         //items[0].pollstartdate.hasvalue 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 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 if don't have problem stated above, why?

public void processitems() {     var items = getitems();      //in here, 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 declare variables not static solves problem even though use in first code. why can't use both static , if statement in first code?

edit: expected value: _currentpollstartdate -> 2013-04-18 10:03:03

result value: _currentpollstartdate -> 0001-01-01 00:00:00 (this not max value)

thank ken kin. latest comment, thought use of properties instead of simple declaration. instead of below:

private static datetime _currentpollstartdate = datetime.minvalue; //as default private static datetime _currentpollenddate = datetime.minvalue; //as default 

i did

private static datetime currentpollstartdate { set; get; } private static datetime currentpollenddate { set; get; }  

and worked.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -