c# - Methods that return meaningful return values -


i work in c#, i've posted under c# although may question can answered in programming language.

sometimes create method such log user website. times return boolean method, causes problems because boolean return value doesn't convey context. if error occurs whilst logging user in have no way of knowing caused error.

here example of method use, change returns more 0 or 1.

public bool logintotwitter(string username, string password) {     // code log user in } 

the above method can return true or false. works because can call following:

if(http.logintotwitter(username, password)) {     // logged in } else {     // not logged in } 

but problem have no way of knowing why user wasn't logged in. number of reasons exist such as: wrong username/password combination, suspended account, account requires users attention etc. using following method, , logic, isn't possible know this.

what alternative approaches have?

you create , return enum expected loginresults.

public enum loginresult {    success,    accountsuspended,    wrongusername,    wrongpassword, } 

then return using enum type in method:

public loginresult logintotwitter(string username, string password) {     // code log user in } 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -