How can I do to remove the string s

T

Tony Johansson

Hello!

If I do the following I get compile error says It's not possible to implicit
convert the type string to bool
Label4.Text = "Browser support cookies : " + Request.Browser.Cookies ?
"true" : "false";

Can I write in another way so I can remove the s string in this code.


protected void Page_Load(object sender, EventArgs e)
{
string s;
Label1.Text = "Application path is : " + Request.Path;
Label2.Text = "OS platform is : " + Request.Browser.Platform;
Label3.Text = "Browser type is : " + Request.Browser.Type;
s = Request.Browser.Cookies ? "true" : "false";
Label4.Text = "Browser support cookies : " + s;
s = Request.Browser.VBScript ? "true" : "false";
Label5.Text = "Browser support VBScript : " + s;
s = Request.Browser.JavaScript ? "true" : "false";
Label6.Text = "Browser support JavaScript : " + s;
Label7.Text = "Browser is using :" + Request.HttpMethod + " data
transfer method";
}

//Tony
 
A

Arne Vajhøj

Hello!

If I do the following I get compile error says It's not possible to implicit
convert the type string to bool
Label4.Text = "Browser support cookies : " + Request.Browser.Cookies ?
"true" : "false";

Can I write in another way so I can remove the s string in this code.

Try:

Label4.Text = "Browser support cookies : " + (Request.Browser.Cookies ?
"true" : "false");

or maybe:

Label4.Text = "Browser support cookies : " + Request.Browser.Cookies;

Arne
 
T

Tony Johansson

I found it myself I just used Convert.ToString in this way.
Label4.Text = "Browser support cookies :" +
Convert.ToString(Request.Browser.Cookies ? true : false);

//Tony
 
A

Arne Vajhøj

I found it myself I just used Convert.ToString in this way.
Label4.Text = "Browser support cookies :" +
Convert.ToString(Request.Browser.Cookies ? true : false);

That code can be shortened!

Label4.Text = "Browser support cookies :" + Request.Browser.Cookies;

works the same way.

Arne
 
J

Jeff Johnson

Convert.ToString(Request.Browser.Cookies ? true : false);

Stop and think about this code:

Request.Browser.Cookies ? true : false

The Cookies property is a Boolean. You're testing it (for "trueness") and
returning...a Boolean! Just use the value itself instead of translating it
into...itself!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top