c# - Why is the value of only this variable ignored when constructing an object with {}? -
i have apirequest class on need default value isinbackground variable.
apirequest class simplified :
public class apirequest { public string url; public bool isinbackground = true; }
when this, value of isinbackground ignored , set default :
apirequest rq = new apirequest{ url = "/user/suggest/" + pseudo, isinbackground = false };
then log value , 'true', url right value...
the way set 'false' do
rq.isinbackground = false;
then value indeed 'false'
so question why behave way ? because isinbackground has default value ?
public class apitest { public string url; public bool background = true; } public class testclass : monobehaviour { // use initialization void start () { apitest t = new apitest{ url = "coucou", background = false }; debug.log(" url: " + t.url + " background: " + t.background); } }
edit : after trying set default value 'false' , set variable 'true' problem doesn't occur...
edit show full code :
private void showpseudosuggestionsfor(string pseudo){ debug.log("get suggestions"); remoterequest r = remoterequest.getnewrequest("pseudosuggestions"); apirequest rq = new apirequest{ url = "/user/suggest/" + pseudo, successcallback = onsuggestionsloaded, successcallbackargs = new hashtable{ { "pseudo", pseudo } }, isinbackground = false }; //rq.isinbackground = false; debug.log("isinbackground : " + rq.isinbackground); r.setrequest(rq); debug.log("isinbackground : " + rq.isinbackground); r.sendrequest(); }
full apirequest class :
public class apirequest { public string url; /*!< url wish send request @ */ public httpmethod method = httpmethod.get; /*!< http method (get, post, put, delete) */ public dictionary<string, string> headers = new dictionary<string, string>(); /*!< headers, add 1 headers.add(<headername>, <headervalue>) */ public byte[] payload; /*!< if need send post or put data create wwwform, add fields need in , set payload <yourform>.data */ public bool resendonfailure = false; /*!< should resend request if fails ? */ public bool resendontimeout = false; /*!< should resend request if times out ? */ public bool isinbackground = true; /*!< should send request start , completed events */ public bool askbeforeresending = false; /*!< should ask user before resending request ? */ public rect askingpopuprect; /*!< size of popup ask user resend request */ public string askingpopuptitle; /*!< title resend request popup */ public string askingpopupmessage; /*!< message resend request popup */ public gui.windowfunction drawaskingpopup; /*!< function responsible drawing resend request popup */ public float timeout = 10f; /*!< time until request considered failure */ public int maxattempts = 1; /*!< maximum number of attempts before request resent, upon reaching it, no more attempts resend made */ public requestcallback successcallback; /*!< function called when server responded status code 2xx (success) */ public requestcallback failurecallback; /*!< other status code 2xx trigger function (failure) */ public hashtable failurecallbackargs; /*!< hashtable passed along 'successcallback' function mean provide arguments */ public hashtable successcallbackargs; /*!< hashtable passed along 'failurecallback' function mean provide arguments */ /** returns string representation of request human readable * \return string describing request parameters */ public override string tostring(){ string result = "request url: " + url + " method: " + method.tostring () + "\nheaders:\n"; foreach(keyvaluepair<string, string> kvp in headers){ result += kvp.key + " -> " + kvp.value + "\n"; } result += "params :\n"; result += "resendonfailure: " + resendonfailure + "\n"; result += "resendontimeout: " + resendontimeout + "\n"; result += "askbeforeresending: " + askbeforeresending + "\n"; result += "timeout: " + timeout + "\n"; result += "maxattempts: " + maxattempts + "\n"; result += "isinbackground: " + isinbackground + "\n"; return result; } }
this code fails in unity because use mono. bug found in using mono , has nothing c# language spoke44 pointed, solution use properties :
Comments
Post a Comment