Convert a bool to an int and then to a string

Hi there,

I got the following statement

myQuery = myQuery + "flg=" + Server.UrlEncode( Convert.ToString( Convert.ToInt32( frontLineGolfCheckBox.Checked ) ) );                        

Basically it takes a bool (.checked of an asp.net checkbox) and want it to convert to a int (1 or 0) and then convert this to a string for including on my above myQuery string variable

Got it to work, the above does work BUT it seems pretty long and remember reading somewhere that the Convert class was quite resource hungry but I can't CAST from a bool to an int so i presume the above is the best method,

Anybody done this before or have info? Is there a better way?

Ian
ianinspainAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AlexFMCommented:
Maybe this performs better:

myQuery = myQuery + "flg=" + Server.UrlEncode( Convert.ToString( frontLineGolfCheckBox.Checked ? 1 : 0 )

or:

myQuery = myQuery + "flg=" + Server.UrlEncode( frontLineGolfCheckBox.Checked ? "1" :" 0" )
0
skytrueCommented:
You can use this one:
myQuery = myQuery + "flg=" + Server.UrlEncode(frontLineGolfCheckBox.Checked?"1":"0");
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AlexFMCommented:
Photofinish.
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

ianinspainAuthor Commented:
Cool, I am going to test it now.

I never seen that (coming from VB background)... so what exactly is this

variable?"1":"0"

Is it like a IIF? What is the name of the function?? Can it be only used on bool? hence first part true and second part false??

Does anyone have a document explaining it, msdn  or similar???

I
0
skytrueCommented:
It is <expression>?<value to return if expression is true>:<value to return if expression is false>
0
skytrueCommented:
You can do something like (a<b)?"a is less than b":((a == b)?"a equals to b":"a is greater than b")
0
ianinspainAuthor Commented:
ahh perfect yep... just like the double IIF ... cool..

Ok I am going split the point ... because as AlexFM said that was a photofinish for the first answer (check the time, wow!)....

Splitting points, much appreciated

Ian
0
AlexFMCommented:
Yes, ? : operator in C# is like VB IIF.
0
ianinspainAuthor Commented:
wow! Skytrue,

that is extra cool, so its even better the IIF

You don't have the command syntax do you on a document like msdn or similar so I check all the syntax... it seems a really powerful command.

I do have my trusty c# Bible here, do you know what the actual name of the command is called so i can maybe look it up

0
skytrueCommented:
heheheheh, you won't believe it, it is called ?: Operator in the MSDN. ;-)
0
ianinspainAuthor Commented:
ah ah ah.. okey dokey, I should have guessed... I will take a look.. Thanks for your help .....

Thanks AlexFM too

0
skytrueCommented:
Welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.