Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 551
  • Last Modified:

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
0
ianinspain
Asked:
ianinspain
  • 5
  • 4
  • 3
2 Solutions
 
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
 
AlexFMCommented:
Photofinish.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now