Click here to Skip to main content
15,891,025 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i am facing some difficulties in passing a string in classlibrary class,such that i have to pass the parameters and return true if 1 and 0 for false
Posted
Comments
Wendelius 5-Sep-11 8:19am    
Could you post the code and clarify the problem.
Prerak Patel 5-Sep-11 8:19am    
Not clear. What do you mean by passing string. Is it a method parameter or a public variable?
BillWoodruff 6-Sep-11 9:25am    
The wording of this question does not permit a solution: interpreted literally, it would imply a function that took a string, and if that string converted to #1 it would return boolean 'true, and if it converted to boolean 'false would return #zero.

Based on the question, if input is 1 then output will be true otherwise false.

C#
static bool ConvertToBoolean(string dataToParse)
{
    List<string> booleanList = new List<string> { "1", "0" };
    return string.IsNullOrEmpty(dataToParse) ? false : booleanList.Contains(dataToParse);
}


Updated:

C#
static bool ConvertToBoolean(string dataToParse)
{
    string trueString = "1";
    return string.IsNullOrEmpty(dataToParse) ? false : dataToParse == trueString;
}



Hope it helps.
:)
 
Share this answer
 
v3
Comments
GParkings 6-Sep-11 4:36am    
The above code will not perform the behaviour you state. Instead it will return true for values of either "1" or "0" and false for everything else.

I believe what you were trying to achieve here was along the lines of

static bool ConvertToBoolean(string dataToParse)
{
return string.IsNullOrEmpty(dataToParse) ? false : dataToParse == "1";
}
Mohammad A Rahman 6-Sep-11 7:00am    
:( You are absolutely right. Result of long day at work. I updated as you suggested. Thank you.
GParkings 6-Sep-11 7:15am    
no worries, i'll upvote it now its fixed
Mohammad A Rahman 6-Sep-11 7:37am    
Thank you :)
C#
int x = Convert.ToInt32(Convert.ToBoolean("true"));
int y = Convert.ToInt32(Convert.ToBoolean("false"));


After running the code above, x will be 1, and y will be 0.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900